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
# 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.request :url_encoded

    f.use Errors::RequestError
    f.adapter Faraday.default_adapter
  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



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

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



35
36
37
38
39
40
# File 'lib/klaviyo/client.rb', line 35

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