Class: Salesmachine::Api::Client
- Inherits:
-
Object
- Object
- Salesmachine::Api::Client
- Includes:
- Utils
- Defined in:
- lib/salesmachine/api/client.rb
Constant Summary
Constants included from Utils
Utils::UTC_OFFSET_WITHOUT_COLON, Utils::UTC_OFFSET_WITH_COLON
Instance Method Summary collapse
- #account(attrs) ⇒ Object
- #contact(attrs) ⇒ Object
-
#email(attrs) ⇒ Object
public: Send an email.
-
#flush ⇒ Object
public: Synchronously waits until the worker has flushed the queue.
-
#initialize(attrs = {}) ⇒ Client
constructor
public: Creates a new client.
- #pageview(attrs) ⇒ Object
-
#queued_messages ⇒ Object
public: Returns the number of queued messages.
-
#track(attrs) ⇒ Object
public: Tracks an event.
Methods included from Utils
#date_in_iso8601, #datetime_in_iso8601, #formatted_offset, #isoify_dates, #isoify_dates!, #seconds_to_utc_offset, #stringify_keys, #symbolize_keys, #symbolize_keys!, #time_in_iso8601, #uid
Constructor Details
#initialize(attrs = {}) ⇒ Client
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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# 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 |
Instance Method Details
#account(attrs) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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 created_at enqueue(account_uid: account_uid, params: params, created_at: datetime_in_iso8601(created_at), method: 'account') end |
#contact(attrs) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# 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 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) ⇒ Object
public: Send an email
attrs - Hash
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# 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 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 ⇒ Object
public: Synchronously waits until the worker has flushed the queue.
Use only for scripts which are not long-running, and will
specifically exit
37 38 39 40 41 42 |
# 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) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# 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 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 ⇒ Object
public: Returns the number of queued messages
returns Fixnum of messages in the queue
157 158 159 |
# File 'lib/salesmachine/api/client.rb', line 157 def @queue.length end |
#track(attrs) ⇒ Object
public: Tracks an event
attrs - Hash
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# 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 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 |