Class: CleverTap::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/clever_tap/client.rb

Constant Summary collapse

DOMAIN =
'https://api.clevertap.com'.freeze
API_VERSION =
1
HTTP_PATH =
'upload'.freeze
DEFAULT_SUCCESS =
proc { |r| r.to_s }
DEFAULT_FAILURE =
proc { |r| r.to_s }
ACCOUNT_HEADER =
'X-CleverTap-Account-Id'.freeze
PASSCODE_HEADER =
'X-CleverTap-Passcode'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id = nil, passcode = nil, &configure) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
# File 'lib/clever_tap/client.rb', line 22

def initialize( = nil, passcode = nil, &configure)
  @account_id = ()
  @passcode = assign_passcode(passcode)
  @configure = configure || proc {}
  @on_success = DEFAULT_SUCCESS
  @on_failure = DEFAULT_FAILURE
end

Instance Attribute Details

#account_idObject

Returns the value of attribute account_id.



20
21
22
# File 'lib/clever_tap/client.rb', line 20

def 
  @account_id
end

#configureObject

Returns the value of attribute configure.



20
21
22
# File 'lib/clever_tap/client.rb', line 20

def configure
  @configure
end

#on_failureObject

Returns the value of attribute on_failure.



20
21
22
# File 'lib/clever_tap/client.rb', line 20

def on_failure
  @on_failure
end

#on_successObject

Returns the value of attribute on_success.



20
21
22
# File 'lib/clever_tap/client.rb', line 20

def on_success
  @on_success
end

#passcodeObject

Returns the value of attribute passcode.



20
21
22
# File 'lib/clever_tap/client.rb', line 20

def passcode
  @passcode
end

Instance Method Details

#connectionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# 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.adapter.nil?

    config.headers['Content-Type'] = 'application/json'
    config.headers[ACCOUNT_HEADER] = 
    config.headers[PASSCODE_HEADER] = passcode
  end
end

#get(*args, &block) ⇒ Object



48
49
50
# File 'lib/clever_tap/client.rb', line 48

def get(*args, &block)
  connection.get(*args, &block)
end

#on_failed_upload(&block) ⇒ Object



56
57
58
# File 'lib/clever_tap/client.rb', line 56

def on_failed_upload(&block)
  @on_failure = block
end

#on_successful_upload(&block) ⇒ Object



52
53
54
# File 'lib/clever_tap/client.rb', line 52

def on_successful_upload(&block)
  @on_success = block
end

#post(*args, &block) ⇒ Object



44
45
46
# File 'lib/clever_tap/client.rb', line 44

def post(*args, &block)
  connection.post(*args, &block)
end

#upload(records, dry_run: 0) ⇒ Object



60
61
62
63
64
65
66
67
68
69
# 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