Class: ShopSensor::Client

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

Constant Summary collapse

API_VERSION =
'v2'
API_ROOT_PATH =
'/api'
API_HOST =
"http://api.shopstyle.com"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



11
12
13
# File 'lib/shop_sensor/client.rb', line 11

def initialize
  @configuration = ShopSensor.configuration.clone
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



49
50
51
52
# 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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/shop_sensor/client.rb', line 5

def configuration
  @configuration
end

Instance Method Details

#configure(&block) ⇒ Object



31
32
33
34
# File 'lib/shop_sensor/client.rb', line 31

def configure &block
  @connection = nil
  @configuration.configure &block
end

#get(endpoint, options = {}) ⇒ Object



15
16
17
18
# 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) ⇒ Object



27
28
29
# File 'lib/shop_sensor/client.rb', line 27

def product prod_id
  self.get "/products/#{prod_id}"
end

#request(endpoint, options = {}) ⇒ Object



20
21
22
23
24
25
# 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