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.



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_idsObject



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

#inventoryObject



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