class SunlightApi::Client

Public Class Methods

new( public_key, private_key, test=false ) click to toggle source
# File lib/sunlight_api.rb, line 9
def initialize( public_key, private_key, test=false )
        @public_key = public_key
        @private_key = private_key
        @test = test
end

Public Instance Methods

array_of_product_ids() click to toggle source
# File lib/sunlight_api.rb, line 39
def array_of_product_ids
        @inventroy = inventory
        unless @inventroy.nil?
                @inventroy.inject([]){|r,v| r.push(v["Id"])}
        else
                []
        end
end
each_product( ) { |product| ... } click to toggle source
# File lib/sunlight_api.rb, line 56
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
inventory() click to toggle source
# File lib/sunlight_api.rb, line 35
def inventory
        request("inventory")
end
place_order(params ) click to toggle source
# File lib/sunlight_api.rb, line 31
def place_order(params )
        post_request("order", params)
end
post_request( action, params ) click to toggle source
# File lib/sunlight_api.rb, line 23
def post_request( action, params )
        uri = SunlightApi::UriGenerator::new(@public_key, @private_key, @test, action, {format: "json"}).url
        rclient = Rquest::new({verb: :post, uri: uri, payload: params})
        body = rclient.send
        return nil if body.class == Hash and not body['error'].nil?
        JSON::parse( body )
end
product_info( id ) click to toggle source
# File lib/sunlight_api.rb, line 48
def product_info( id )
        request("part/#{id}")
end
product_price_breaks( id ) click to toggle source
# File lib/sunlight_api.rb, line 52
def product_price_breaks( id )
        request("pricebreak/#{id}")
end
products_array() click to toggle source
# File lib/sunlight_api.rb, line 67
def products_array
        request("part")
end
request( action ) click to toggle source
# File lib/sunlight_api.rb, line 15
def request( action )
        uri = SunlightApi::UriGenerator::new(@public_key, @private_key, @test, 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