Class: SunlightApi::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/sunlight_api.rb', line 9

def initialize( public_key, private_key )
  @public_key = public_key
  @private_key = private_key
end

Instance Method Details

#array_of_product_idsObject



26
27
28
29
30
31
32
33
# File 'lib/sunlight_api.rb', line 26

def array_of_product_ids
  @inventroy = inventory
  unless @inventroy.nil?
    @inventroy.inject([]){|r,v| r.push(v["Id"])}
  else
    []
  end
end

#each_product(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# File 'lib/sunlight_api.rb', line 43

def each_product( &block )
  array_of_product_ids.each do |product_id|
    product = product_info( product_id )
    next unless product
    price_breaks = product_price_breaks(product_id)
    price_breaks ||= []
    product["PriceBreaks"] = price_breaks
    yield product
  end
end

#inventoryObject



22
23
24
# File 'lib/sunlight_api.rb', line 22

def inventory
  request("inventory")
end

#product_info(id) ⇒ Object



35
36
37
# File 'lib/sunlight_api.rb', line 35

def product_info( id )
  request("part/#{id}")
end

#product_price_breaks(id) ⇒ Object



39
40
41
# File 'lib/sunlight_api.rb', line 39

def product_price_breaks( id )
  request("pricebreak/#{id}")
end

#request(action) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/sunlight_api.rb', line 14

def request( action )
  uri = SunlightApi::UriGenerator::new(@public_key, @private_key, action, {format: "json"}).url
  rclient = Rquest::new({verb: :get, uri: uri})
  body = rclient.send
  return nil if body.class == Hash and not body['error'].nil?
  JSON::parse( body )
end