Class: Tucano::ProductSearch

Inherits:
Object
  • Object
show all
Defined in:
lib/tucano.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, secret, associate_tag) ⇒ ProductSearch

Returns a new instance of ProductSearch.



10
11
12
13
14
15
16
17
# File 'lib/tucano.rb', line 10

def initialize(key, secret, associate_tag)
  @request = ::Vacuum.new(:product_advertising)
  @request.configure do |config|
    config.key = key
    config.secret = secret
    config.tag = associate_tag
  end
end

Instance Method Details

#convert_to_product(item) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/tucano.rb', line 19

def convert_to_product(item)
  i = Item.new
  i.name = item["ItemAttributes"]["Title"]
  i.category = item["ItemAttributes"]["ProductGroup"]
  i.price = item["ItemAttributes"]["ListPrice"]["Amount"]
  i.currency = item["ItemAttributes"]["ListPrice"]["CurrencyCode"]
  #i.description = item["EditorialReviews"]["EditorialReview"]["Content"]
  i.image = item["LargeImage"]["URL"]
  return i
end

#parse_response(response) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/tucano.rb', line 30

def parse_response(response)
  products = []
  response.find("Item") do |item|
    product = convert_to_product(item)
    products << product
  end
  products
end

#search(query) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/tucano.rb', line 39

def search(query)
  rg = %W{ItemAttributes EditorialReview Images}
  @request.build(:operation => "ItemSearch", 
                :search_index => "All", 
                :keywords => query,
                :response_group => rg)
  products = []
  response = @request.get
  products = parse_response(response)
  products
end