Sha256: 9131e450200b3be24993523a40be3bc53c52e48e8429a891bbc11e7e135caf0e

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module Sterling
  module API
    class Product
      attr_reader :product, :distance, :location, :inventory, :price, :currency, :inventory, :lastUpdated

      def self.search(client, params)
        return [] unless  params[:keywords] && params[:user_location] && params[:requestor_id]

        response = client.conn.get do |req|
          req.url 'products'
          req.params['apikey'] = client.config.api_key
          req.params['userlocation'] = params[:user_location]
          req.params['requestorid'] = params[:requestor_id]
          req.params['keywords'] = params[:keywords]
        end

        parsed = JSON.parse(response.body)

        if parsed['RetailigenceSearchResult']['results'] && parsed['RetailigenceSearchResult']['results'].size > 0
          products = parsed['RetailigenceSearchResult']['results'].map do |product|
            Product.new(product['SearchResult'])
          end
        else
          []
        end
      end

      def initialize(object)
        object.each do |k,v|
          self.class.class_eval { attr_reader k.to_sym }
          instance_variable_set "@#{k}", object[k]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sterling-1.1.1 lib/sterling/api/product.rb
sterling-1.1.0 lib/sterling/api/product.rb