class ShopSensor::Client

Constants

API_HOST
API_ROOT_PATH
API_VERSION

Attributes

configuration[RW]

Public Class Methods

new() click to toggle source
# File lib/shop_sensor/client.rb, line 11
def initialize
  @configuration = ShopSensor.configuration.clone
end

Public Instance Methods

configure(&block) click to toggle source
# File lib/shop_sensor/client.rb, line 31
def configure &block
  @connection = nil
  @configuration.configure &block
end
get(endpoint, options={}) click to toggle source
# File lib/shop_sensor/client.rb, line 15
def get endpoint, options={}
  response = self.request endpoint, options
  JSON.parse response.body
end
product(prod_id) click to toggle source
# File lib/shop_sensor/client.rb, line 27
def product prod_id
  self.get "/products/#{prod_id}"
end
request(endpoint, options={}) click to toggle source
# File lib/shop_sensor/client.rb, line 20
def request endpoint, options={}
  raise ShopSensor::MissingConfiguration, :api_key unless @configuration.api_key
  options[:format] ||= 'json'
  path = File.join API_ROOT_PATH, API_VERSION, endpoint
  connection.get path, options
end

Private Instance Methods

connection() click to toggle source
# File lib/shop_sensor/client.rb, line 37
def connection
  @connection ||= Faraday.new(API_HOST, params: default_params) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end
default_params() click to toggle source
# File lib/shop_sensor/client.rb, line 45
def default_params
  { pid: configuration.api_key, site: configuration.site }
end
method_missing(method, *args) click to toggle source
Calls superclass method
# File lib/shop_sensor/client.rb, line 49
def method_missing method, *args
  return super unless simple_apis.include? method.to_s
  get *([method.to_s] + args)
end
simple_apis() click to toggle source
# File lib/shop_sensor/client.rb, line 54
def simple_apis
  [
    'brands',
    'products',
    'categories',
    'colors',
    'retailers'
  ]
end