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/ct_sendyr/client.rb, line 5
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/ct_sendyr/client.rb, line 90
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/ct_sendyr/client.rb, line 12
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/ct_sendyr/client.rb, line 44
          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/ct_sendyr/client.rb, line 28
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/ct_sendyr/client.rb, line 69
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/ct_sendyr/client.rb, line 124
def clean_body(result)
        result.body.strip.chomp.downcase
end
noop() click to toggle source
# File lib/ct_sendyr/client.rb, line 147
def noop
        :noop
end
post_to(path, params) click to toggle source
# File lib/ct_sendyr/client.rb, line 116
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/ct_sendyr/client.rb, line 108
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/ct_sendyr/client.rb, line 133
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/ct_sendyr/client.rb, line 128
def respond_with_success(result, value = nil)
        @last_result = result
        value.nil? ? true : value
end
underscore(word) click to toggle source
# File lib/ct_sendyr/client.rb, line 138
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/ct_sendyr/client.rb, line 120
def url_for(path)
        return File.join(@base_uri, path)
end