class Salesmachine::Api::Client

Public Class Methods

new(attrs = {}) click to toggle source

public: Creates a new client

attrs - Hash

:api_key         - String of your project's api_key
:max_queue_size - Fixnum of the max calls to remain queued (optional)
:on_error       - Proc which handles error calls from the API
# File lib/salesmachine/api/client.rb, line 18
def initialize(attrs = {})
  symbolize_keys! attrs

  @queue = Queue.new
  @api_key = attrs[:api_key]
  @max_queue_size = attrs[:max_queue_size] || Config::Queue::MAX_SIZE
  @options = attrs
  @worker_mutex = Mutex.new
  @worker = Worker.new @queue, @api_key, @options

  check_api_key!

  at_exit { @worker_thread && @worker_thread[:should_exit] = true }
end

Public Instance Methods

account(attrs) click to toggle source
# File lib/salesmachine/api/client.rb, line 116
def account(attrs)
  symbolize_keys! attrs
  fail ArgumentError, 'Must supply a contact_uid' unless attrs[:account_uid]

  account_uid = attrs[:account_uid]
  params = attrs[:params] || {}
  created_at = attrs[:created_at] || Time.new

  fail ArgumentError, 'Params must be a hash' unless params.is_a? Hash
  isoify_dates! params

  check_timestamp! created_at

  enqueue(account_uid: account_uid,
          params: params,
          created_at: datetime_in_iso8601(created_at),
          method: 'account')
end
contact(attrs) click to toggle source
# File lib/salesmachine/api/client.rb, line 98
def contact(attrs)
  symbolize_keys! attrs
  check_contact_id! attrs

  params = attrs[:params] || {}
  created_at = attrs[:created_at] || Time.new

  check_timestamp! created_at

  fail ArgumentError, 'Must supply params as a hash' unless params.is_a? Hash
  isoify_dates! params

  enqueue(contact_uid: attrs[:contact_uid],
          params: params,
          created_at: datetime_in_iso8601(created_at),
          method: 'contact')
end
email(attrs) click to toggle source

public: Send an email

attrs - Hash

# File lib/salesmachine/api/client.rb, line 74
def email(attrs)
  symbolize_keys! attrs
  check_contact_id! attrs

  email = attrs[:email]
  params = attrs[:params] || {}
  created_at = attrs[:created_at] || Time.new

  check_timestamp! created_at

  if email.nil? || email.empty?
    fail ArgumentError, 'Must supply email template as a non-empty string'
  end

  fail ArgumentError, 'Params must be a Hash' unless params.is_a? Hash
  isoify_dates! params

  enqueue(email: email,
          contact_uid: attrs[:contact_uid],
          params: params,
          created_at: datetime_in_iso8601(created_at),
          method: 'email')
end
flush() click to toggle source

public: Synchronously waits until the worker has flushed the queue.

Use only for scripts which are not long-running, and will
specifically exit
# File lib/salesmachine/api/client.rb, line 37
def flush
  while !@queue.empty? || @worker.is_requesting?
    ensure_worker_running
    sleep(0.1)
  end
end
pageview(attrs) click to toggle source
# File lib/salesmachine/api/client.rb, line 135
def pageview(attrs)
  symbolize_keys! attrs
  check_contact_id! attrs

  params = attrs[:params] || {}
  created_at = attrs[:created_at] || Time.new

  fail ArgumentError, '.params must be a hash' unless params.is_a? Hash
  isoify_dates! params

  check_timestamp! created_at

  enqueue(contact_uid: attrs[:contact_uid],
          event: 'pageview',
          params: attrs[:params],
          created_at: datetime_in_iso8601(created_at),
          method: 'event')
end
queued_messages() click to toggle source

public: Returns the number of queued messages

returns Fixnum of messages in the queue

# File lib/salesmachine/api/client.rb, line 157
def queued_messages
  @queue.length
end
track(attrs) click to toggle source

public: Tracks an event

attrs - Hash

# File lib/salesmachine/api/client.rb, line 47
def track(attrs)
  symbolize_keys! attrs
  check_contact_id! attrs

  event = attrs[:event]
  params = attrs[:params] || {}
  created_at = attrs[:created_at] || Time.new

  check_timestamp! created_at

  if event.nil? || event.empty?
    fail ArgumentError, 'Must supply event as a non-empty string'
  end

  fail ArgumentError, 'Params must be a Hash' unless params.is_a? Hash
  isoify_dates! params

  enqueue(event: event,
          contact_uid: attrs[:contact_uid],
          params: params,
          created_at: datetime_in_iso8601(created_at),
          method: 'event')
end

Private Instance Methods

add_context(context) click to toggle source

private: Adds contextual information to the call

context - Hash of call context

# File lib/salesmachine/api/client.rb, line 190
def add_context(context)
  context[:library] =  { name: 'salesmachine-ruby', version: Salesmachine::Api::VERSION.to_s }
end
check_api_key!() click to toggle source

private: Checks that the api_key is properly initialized

# File lib/salesmachine/api/client.rb, line 195
def check_api_key!
  fail ArgumentError, 'Api key must be initialized' if @api_key.nil?
end
check_contact_id!(attrs) click to toggle source
# File lib/salesmachine/api/client.rb, line 204
def check_contact_id!(attrs)
  fail ArgumentError, 'Must supply a contact_uid' unless attrs[:contact_uid]
end
check_presence!(obj, name) click to toggle source

private: Ensures that a string is non-empty

obj - String|Number that must be non-blank name - Name of the validated value

# File lib/salesmachine/api/client.rb, line 181
def check_presence!(obj, name)
  if obj.nil? || (obj.is_a?(String) && obj.empty?)
    fail ArgumentError, "#{name} must be given"
  end
end
check_timestamp!(timestamp) click to toggle source

private: Checks the timstamp option to make sure it is a Time.

# File lib/salesmachine/api/client.rb, line 200
def check_timestamp!(timestamp)
  fail ArgumentError, 'Timestamp must be a Time' unless timestamp.is_a? Time
end
enqueue(action) click to toggle source

private: Enqueues the action.

returns Boolean of whether the item was added to the queue.

# File lib/salesmachine/api/client.rb, line 166
def enqueue(action)
  # add our request id for tracing purposes
  action[:messageId] = uid
  unless queue_full = @queue.length >= @max_queue_size
    ensure_worker_running
    @queue << action
  end
  !queue_full
end
ensure_worker_running() click to toggle source
# File lib/salesmachine/api/client.rb, line 208
def ensure_worker_running
  return if worker_running?
  @worker_mutex.synchronize do
    return if worker_running?
    @worker_thread = Thread.new do
      @worker.run
    end
  end
end
worker_running?() click to toggle source
# File lib/salesmachine/api/client.rb, line 218
def worker_running?
  @worker_thread && @worker_thread.alive?
end