class ConstructorIO::Client

Attributes

local_configuration[RW]

Public Class Methods

new(config = nil) click to toggle source
# File lib/constructorio/client.rb, line 8
def initialize(config = nil)
  @local_configuration = config || ConstructorIO::Configuration.new(
    api_token: ConstructorIO.configuration.api_token,
    api_url: ConstructorIO.configuration.api_url || "https://ac.cnstrc.com/",
    autocomplete_key: ConstructorIO.configuration.autocomplete_key
  )
end

Public Instance Methods

add(params) click to toggle source
# File lib/constructorio/client.rb, line 16
def add(params)
  call_api("item", "post", params)
end
add_batch(params) click to toggle source
# File lib/constructorio/client.rb, line 48
def add_batch(params)
  call_api("batch_items", "post", params)
end
add_or_update(params) click to toggle source
# File lib/constructorio/client.rb, line 20
def add_or_update(params)
  call_api("item", "put", params, {force: 1})
end
add_or_update_batch(params) click to toggle source
# File lib/constructorio/client.rb, line 52
def add_or_update_batch(params)
  call_api("batch_items", "put", params, {force: 1})
end
modify(params) click to toggle source
# File lib/constructorio/client.rb, line 28
def modify(params)
  call_api("item", "put", params)
end
remove(params) click to toggle source
# File lib/constructorio/client.rb, line 24
def remove(params)
  call_api("item", "delete", params)
end
remove_batch(params) click to toggle source
# File lib/constructorio/client.rb, line 56
def remove_batch(params)
  call_api("batch_items", "delete", params)
end
track_click_through(params) click to toggle source
# File lib/constructorio/client.rb, line 36
def track_click_through(params)
  call_api("click_through", "post", params)
end
track_conversion(params) click to toggle source
# File lib/constructorio/client.rb, line 40
def track_conversion(params)
  call_api("conversion", "post", params)
end
verify() click to toggle source
# File lib/constructorio/client.rb, line 44
def verify
  call_api("verify", "get")
end

Private Instance Methods

call_api(path, method, params = {}, additional_query_params = {}) click to toggle source
# File lib/constructorio/client.rb, line 62
def call_api(path, method, params = {}, additional_query_params = {})
  api_token = self.local_configuration.api_token
  api_url = self.local_configuration.api_url
  autocomplete_key = self.local_configuration.autocomplete_key
  @http_client ||= Faraday.new(url: api_url)
  @http_client.basic_auth(api_token, '')

  send_request(path, method, @http_client, params, autocomplete_key, additional_query_params)
end
send_request(path, method, http_client, params, autocomplete_key, additional_query_params) click to toggle source
# File lib/constructorio/client.rb, line 72
def send_request(path, method, http_client, params, autocomplete_key, additional_query_params)
  query_params = additional_query_params ? "&#{URI.encode_www_form(additional_query_params)}" : ""
  response = http_client.send(method) do |request|
    request.url "/v1/#{path}?autocomplete_key=#{autocomplete_key}#{query_params}"
    request.headers['Content-Type'] = 'application/json'
    request.body = params.to_json
  end
  response
end