Class: ShopStyle

Inherits:
Object
  • Object
show all
Includes:
ShopStyleHelper
Defined in:
lib/shopstyle.rb

Constant Summary collapse

HOST =
'api.shopstyle.com'
DEFAULT_FORMAT =
'json'

Instance Method Summary collapse

Methods included from ShopStyleHelper

#escape, #format_param_pair, #query_from_params, #url_for

Constructor Details

#initialize(api_key) ⇒ ShopStyle

Returns a new instance of ShopStyle.



44
45
46
47
# File 'lib/shopstyle.rb', line 44

def initialize api_key
  @api_key = api_key
  @session = Patron::Session.new
end

Instance Method Details

#request(method, params = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/shopstyle.rb', line 49

def request method, params = {}
  params[:pid] = @api_key
  params[:format] ||= DEFAULT_FORMAT
  tries = 0
  begin
    raw_response = @session.get(url_for(HOST, method, params))
  rescue Patron::HostResolutionError, Patron::ConnectionFailed
    retry if tries < 5
    tries += 1
  end
  
  case params[:format]
  when 'json'
    JSON.parse(raw_response.body)
  else
    raise ShopStyleError.new('Unknown format %s' % params[:format])
  end
end