Class: SunlightApi::Client
- Inherits:
-
Object
- Object
- SunlightApi::Client
- Defined in:
- lib/sunlight_api.rb
Instance Method Summary collapse
- #array_of_product_ids ⇒ Object
- #each_product(&block) ⇒ Object
-
#initialize(public_key, private_key) ⇒ Client
constructor
A new instance of Client.
- #inventory ⇒ Object
- #product_info(id) ⇒ Object
- #request(action) ⇒ Object
Constructor Details
#initialize(public_key, private_key) ⇒ Client
Returns a new instance of Client.
7 8 9 10 |
# File 'lib/sunlight_api.rb', line 7 def initialize( public_key, private_key ) @public_key = public_key @private_key = private_key end |
Instance Method Details
#array_of_product_ids ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/sunlight_api.rb', line 24 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
37 38 39 40 41 42 43 |
# File 'lib/sunlight_api.rb', line 37 def each_product( &block ) array_of_product_ids.each do |product_id| product = product_info( product_id ) next unless product yield product end end |
#inventory ⇒ Object
20 21 22 |
# File 'lib/sunlight_api.rb', line 20 def inventory request("inventory") end |
#product_info(id) ⇒ Object
33 34 35 |
# File 'lib/sunlight_api.rb', line 33 def product_info( id ) request("part/#{id}") end |
#request(action) ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/sunlight_api.rb', line 12 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 |