class CleverTap::Client
Constants
- ACCOUNT_HEADER
- API_VERSION
- DEFAULT_FAILURE
- DEFAULT_SUCCESS
- DOMAIN
- HTTP_PATH
- PASSCODE_HEADER
Attributes
account_id[RW]
configure[RW]
on_failure[RW]
on_success[RW]
passcode[RW]
Public Class Methods
new(account_id = nil, passcode = nil, &configure)
click to toggle source
# File lib/clever_tap/client.rb, line 22 def initialize(account_id = nil, passcode = nil, &configure) @account_id = assign_account_id(account_id) @passcode = assign_passcode(passcode) @configure = configure || proc {} @on_success = DEFAULT_SUCCESS @on_failure = DEFAULT_FAILURE end
Public Instance Methods
connection()
click to toggle source
# File lib/clever_tap/client.rb, line 30 def connection # TODO: pass the config to a block @connection ||= Faraday.new("#{DOMAIN}/#{API_VERSION}") do |config| configure.call(config) # NOTE: set adapter only if there isn't one set config.adapter :net_http if config.builder.handlers.empty? config.headers['Content-Type'] = 'application/json' config.headers[ACCOUNT_HEADER] = account_id config.headers[PASSCODE_HEADER] = passcode end end
get(*args, &block)
click to toggle source
# File lib/clever_tap/client.rb, line 48 def get(*args, &block) connection.get(*args, &block) end
on_failed_upload(&block)
click to toggle source
# File lib/clever_tap/client.rb, line 56 def on_failed_upload(&block) @on_failure = block end
on_successful_upload(&block)
click to toggle source
# File lib/clever_tap/client.rb, line 52 def on_successful_upload(&block) @on_success = block end
post(*args, &block)
click to toggle source
# File lib/clever_tap/client.rb, line 44 def post(*args, &block) connection.post(*args, &block) end
upload(records, dry_run: 0)
click to toggle source
# File lib/clever_tap/client.rb, line 60 def upload(records, dry_run: 0) payload = ensure_array(records) entity = determine_type(payload) all_responses = [] batched_upload(entity, payload, dry_run) do |response| all_responses << response end all_responses end
Private Instance Methods
assign_account_id(account_id)
click to toggle source
# File lib/clever_tap/client.rb, line 105 def assign_account_id(account_id) account_id || CleverTap.account_id || raise('Clever Tap `account_id` missing') end
assign_passcode(passcode)
click to toggle source
# File lib/clever_tap/client.rb, line 109 def assign_passcode(passcode) passcode || CleverTap.account_passcode || raise('Clever Tap `passcode` missing') end
batched_upload(entity, payload, dry_run) { |clevertap_response| ... }
click to toggle source
# File lib/clever_tap/client.rb, line 73 def batched_upload(entity, payload, dry_run) payload.each_slice(entity.upload_limit) do |group| response = post(HTTP_PATH, request_body(group)) do |request| request.params.merge!(dryRun: dry_run) end clevertap_response = Response.new(response) if clevertap_response.success @on_success.call(clevertap_response) else @on_failure.call(clevertap_response) end yield(clevertap_response) if block_given? end end
determine_type(records)
click to toggle source
# File lib/clever_tap/client.rb, line 95 def determine_type(records) types = records.map(&:class).uniq raise NotConsistentArrayError unless types.one? types.first end
ensure_array(records)
click to toggle source
# File lib/clever_tap/client.rb, line 101 def ensure_array(records) Array(records) end
request_body(records)
click to toggle source
# File lib/clever_tap/client.rb, line 91 def request_body(records) { 'd' => records.map(&:to_h) }.to_json end