Class: Klaviyo::Client

Inherits:
Object
  • Object
show all
Extended by:
ClientDSL
Defined in:
lib/klaviyo/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClientDSL

define_api_method

Constructor Details

#initialize(api_key, token, req_options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/klaviyo/client.rb', line 10

def initialize(api_key, token, req_options = {})
  @api_key = api_key
  @token = token
  raise Errors::AuthenticationError, 'No API key provided.' unless @api_key
  raise Errors::AuthenticationError, 'No Token provided.' unless @token

  @conn = Faraday.new(default_req_options.merge(req_options)) do |f|
    f.headers['Accept'] = 'application/json'
    f.request  :url_encoded
    f.response :logger
    f.response :json, content_type: 'application/json'
    f.adapter Faraday.default_adapter
    f.use Errors::RequestError
  end
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/klaviyo/client.rb', line 9

def api_key
  @api_key
end

#connObject (readonly)

Returns the value of attribute conn.



9
10
11
# File 'lib/klaviyo/client.rb', line 9

def conn
  @conn
end

#tokenObject (readonly)

Returns the value of attribute token.



9
10
11
# File 'lib/klaviyo/client.rb', line 9

def token
  @token
end

Instance Method Details

#build_params(params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/klaviyo/client.rb', line 26

def build_params(params = {})
  options = params.merge(
    api_key: api_key,
    token: token
  )

  {
    data: Base64.strict_encode64(JSON.generate(options))
  }
end

#invoke(resource, method, options = {}) ⇒ Object



37
38
39
40
41
42
# File 'lib/klaviyo/client.rb', line 37

def invoke(resource, method, options = {})
  constantize("klaviyo/#{resource}").send(
    method,
    options.merge(client: self)
  )
end