Class: Sterling::API::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/sterling/api/product.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Product

Returns a new instance of Product.



28
29
30
31
32
33
# File 'lib/sterling/api/product.rb', line 28

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

Instance Attribute Details

#currencyObject (readonly)

Returns the value of attribute currency.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def currency
  @currency
end

#distanceObject (readonly)

Returns the value of attribute distance.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def distance
  @distance
end

#inventoryObject (readonly)

Returns the value of attribute inventory.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def inventory
  @inventory
end

#lastUpdatedObject (readonly)

Returns the value of attribute lastUpdated.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def lastUpdated
  @lastUpdated
end

#locationObject (readonly)

Returns the value of attribute location.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def location
  @location
end

#priceObject (readonly)

Returns the value of attribute price.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def price
  @price
end

#productObject (readonly)

Returns the value of attribute product.



4
5
6
# File 'lib/sterling/api/product.rb', line 4

def product
  @product
end

Class Method Details

.search(client, params) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sterling/api/product.rb', line 6

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