class Sendyr::Client
Attributes
api_key[R]
base_uri[R]
last_result[R]
list_id[R]
Public Class Methods
new(list_id = nil)
click to toggle source
# File lib/sendyr/client.rb, line 7 def initialize(list_id = nil) @list_id = list_id @api_key = Sendyr.configuration.api_key @base_uri = Sendyr.configuration.url @noop = Sendyr.configuration.noop || false end
Public Instance Methods
active_subscriber_count(opts = {})
click to toggle source
# File lib/sendyr/client.rb, line 92 def active_subscriber_count(opts = {}) return noop if @noop opts = {api_key: @api_key, list_id: @list_id}.merge(opts) raise_if_missing_arg([:list_id, :api_key], opts) path = '/api/subscribers/active-subscriber-count.php' result = post_to(path, opts) cleaned_body = clean_body(result) if result.success? && !!(cleaned_body =~ /^[-+]?[0-9]+$/) respond_with_success(result, cleaned_body.to_i) else respond_with_failure(result) end end
subscribe(opts = {})
click to toggle source
# File lib/sendyr/client.rb, line 14 def subscribe(opts = {}) return noop if @noop opts = {boolean: true, list: @list_id}.merge(opts) raise_if_missing_arg([:email, :list], opts) path = '/subscribe' result = post_to(path, opts) if result.success? && %w(true 1).include?(clean_body(result)) respond_with_success(result) else respond_with_failure(result) end end
subscription_status(opts = {})
click to toggle source
# File lib/sendyr/client.rb, line 46 def subscription_status(opts = {}) return noop if @noop opts = {api_key: @api_key, list_id: @list_id}.merge(opts) raise_if_missing_arg([:api_key, :email, :list_id, :api_key], opts) path = '/api/subscribers/subscription-status.php' result = post_to(path, opts) success_messages = { "subscribed" => :subscribed, "unsubscribed" => :unsubscribed, "unconfirmed" => :unconfirmed, "bounced" => :bounced, "soft bounced" => :soft_bounced, "complained" => :complained, "email does not exist in list" => :not_in_list } cleaned_body = clean_body(result) if result.success? && success_messages.keys.include?(cleaned_body) respond_with_success(result, success_messages[cleaned_body]) else respond_with_failure(result, underscore(cleaned_body).to_sym) end end
unsubscribe(opts = {})
click to toggle source
# File lib/sendyr/client.rb, line 30 def unsubscribe(opts = {}) return noop if @noop opts = {boolean: true, list: @list_id}.merge(opts) raise_if_missing_arg([:email, :list], opts) path = '/unsubscribe' result = post_to(path, opts) if result.success? && %w(true 1).include?(clean_body(result)) respond_with_success(result) else respond_with_failure(result) end end
update_subscription(email, opts = {})
click to toggle source
# File lib/sendyr/client.rb, line 71 def update_subscription(email, opts = {}) return noop if @noop status = subscription_status(email: email) return false if status == :not_in_list # Trying to change the email address? # Need to unsubscribe and subscribe again. if (!opts[:email].nil? && opts[:email] != email) && [:subscribed, :unconfirmed, :bounced, :soft_bounced].include?(status) unsubscribe(email: email) end unless [:complained, :unsubscribed].include?(status) subscribe({email: email}.merge(opts)) == true else false end end
Private Instance Methods
clean_body(result)
click to toggle source
# File lib/sendyr/client.rb, line 126 def clean_body(result) result.body.strip.chomp.downcase end
noop()
click to toggle source
# File lib/sendyr/client.rb, line 149 def noop :noop end
post_to(path, params)
click to toggle source
# File lib/sendyr/client.rb, line 118 def post_to(path, params) Faraday.post(url_for(path), params) end
raise_if_missing_arg(mandatory_fields, opts)
click to toggle source
# File lib/sendyr/client.rb, line 110 def raise_if_missing_arg(mandatory_fields, opts) mandatory_fields.each do |key| if opts[key].nil? || opts[key].to_s.strip == '' raise ArgumentError.new("You must specify :#{key}.") end end; nil end
respond_with_failure(result, value = nil)
click to toggle source
# File lib/sendyr/client.rb, line 135 def respond_with_failure(result, value = nil) @last_result = result value.nil? ? false : value end
respond_with_success(result, value = nil)
click to toggle source
# File lib/sendyr/client.rb, line 130 def respond_with_success(result, value = nil) @last_result = result value.nil? ? true : value end
underscore(word)
click to toggle source
# File lib/sendyr/client.rb, line 140 def underscore(word) word.gsub(/::/, '/'). gsub(/\s/, '_'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end
url_for(path)
click to toggle source
# File lib/sendyr/client.rb, line 122 def url_for(path) URI.join(@base_uri, path).to_s end