Class: Ramazon::Product

Inherits:
Object
  • Object
show all
Includes:
HappyMapper, AbstractElement
Defined in:
lib/ramazon/product.rb

Overview

Find and get product details with this class Currently supports the following accessors (all other elements can be accessed via nokogiri selectors and the get method)

asin

Amazon Identifier

upc

UPC ID

title

Title of the product

product_group

The category/product_group of the product

manufacturer

The manufacturer of the product

brand

The brand of the product

url

The Amazon URL of the product

small_image

The small image that Amazon provides (NOTE: Returns Ramazon::Image object)

medium_image

The medium image that Amazon provides (NOTE: Returns Ramazon::Image object)

large_image

The large image that Amazon provides (NOTE: Returns Ramazon::Image object)

list_price

The list price of the item (NOTE: Returns Ramazon::Price object)

lowest_new_price

The lowest new price from the offer summary (NOTE: Returns Ramazon::Price object)

sales_rank

The sales rank of the product

new_count

The quantity of new item offers

used_count

The quantity of used item offers

collectible_count

The quantity of collectible item offers

refurbished_count

The quantity of refurbished item offers

release_date

The release date of the product

original_release_date

The original release date of the product

offers

The collection of offers available for the given product

Examples:

find an individual item

@products = Ramazon::Product.find(:item_id => "B000NU2CY4", :response_group => "Medium")
@products[0].title
@products[0].asin
@products[0].upc
@products[0].large_image.url
@products[0].url

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AbstractElement

included

Instance Attribute Details

#has_first_page_of_full_offersObject

Returns the value of attribute has_first_page_of_full_offers.



85
86
87
# File 'lib/ramazon/product.rb', line 85

def has_first_page_of_full_offers
  @has_first_page_of_full_offers
end

#xml_docObject

Returns the value of attribute xml_doc.



136
137
138
# File 'lib/ramazon/product.rb', line 136

def xml_doc
  @xml_doc
end

Class Method Details

.find(*args) ⇒ Array

Creates the worker that performs the delta indexing (ie. passing the :response_group option will be converted to “ResponseGroup”) :item_id - the ASIN or UPC you’re looking for

Parameters:

  • options

    Amazon request options (you can use an underscore convention)

Returns:

  • (Array)

    array of Ramazon::Product objects



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ramazon/product.rb', line 92

def self.find(*args)
  options = args.extract_options!
  if options[:item_id]
    item_lookup(options[:item_id], options)
  else
    options[:operation] ||= "ItemSearch"
    options[:search_index] ||= "Blended"
    options[:item_page] ||= 1
    res = Ramazon::Request.new(options).submit
    products = Ramazon::ProductCollection.create_from_results(options[:item_page] || 1, 10, res)
    if find_options_retrieve_all_offers?(options)
      products.each do |p|
        p.has_first_page_of_full_offers = true
        p.offer_pages = offer_pages_for(p)
      end
    end
    products
  end
end

.item_lookup(item_id, options = {}) ⇒ Object

Performs an item lookup

Parameters:

  • item_id

    the ASIN or UPC you’re looking for



115
116
117
118
119
120
121
# File 'lib/ramazon/product.rb', line 115

def self.item_lookup(item_id, options = {})
  req = Ramazon::Request.new({:item_id => item_id, 
    :operation => "ItemLookup"}.merge(options))
  res = req.submit

  Ramazon::ProductCollection.create_from_results(1,1,res)
end

.offer_pages_for(product) ⇒ Object

gets the number of offer pages for the specified product

Parameters:



203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/ramazon/product.rb', line 203

def self.offer_pages_for(product)
  if !@offer_pages 
    offer_page_tags = product.get("//Offers/TotalOfferPages")
    if offer_page_tags.size > 0
      offer_pages = offer_page_tags[0].content.to_i
    else
      offer_pages = 1
    end
  end

  offer_pages
end

.parse(xml, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/ramazon/product.rb', line 137

def self.parse(xml, options = {})
  node = XML::Parser.string(xml.to_s).parse.root
  node.find("//Item").collect do |n|
    p = super(n.to_s)
    p.xml_doc = Nokogiri::XML.parse(n.to_s)
    p
  end
end

Instance Method Details

#category_treeObject

returns a hash of category browse nodes from the top down



156
157
158
159
160
161
162
# File 'lib/ramazon/product.rb', line 156

def category_tree
  @category_tree = {}
  get_category_browse_nodes.each do |n|
    build_category_tree(n)
  end
  @category_tree
end

#get(*args) ⇒ Object

perform a nokogiri search on the product’s XML

Examples:

find the actor

@product = Ramazon::Product.find(:item_id => "B000NU2CY4", :response_group => "Medium")[0]
@product.get("ItemAttributes Actor").collect{|a| a.content}

Parameters:

  • args

    Passes directly to a Nokogiri::Xml.parse(xml).search method



151
152
153
# File 'lib/ramazon/product.rb', line 151

def get(*args)
  result = @xml_doc.search(args)
end

#imagesHash

assembles the available images for the object

Returns:

  • (Hash)

    hash of symbolized image_name => Ramazon::Image pairs



125
126
127
128
129
130
131
132
133
134
# File 'lib/ramazon/product.rb', line 125

def images
  if !@images
    @images = {}
    @images[:thumb] = self.thumb_image if self.thumb_image
    @images[:tiny_image] = self.tiny_image if self.tiny_image
    @images[:small] = self.small_image if self.small_image   
    @images[:medium] = self.medium_image if self.medium_image
    @images[:large] = self.large_image if self.large_image
  end
end

#lowest_offersHash

get the lowest offers broken down by subcondition

Returns:

  • (Hash)

    a nested hash of prices ie [“new”] => Ramazon::Offer



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/ramazon/product.rb', line 218

def lowest_offers
  if @lowest_offers.nil?
    @lowest_offers = {}
    offers_by_condition.each do |condition, sub_conditions|
      @lowest_offers[condition] = {}
      sub_conditions.each do |sub_condition, col|
        sorted_offers = col.sort{|a,b| a.price.amount.to_i <=> b.price.amount.to_i}
        @lowest_offers[condition][sub_condition] = sorted_offers.first
      end
    end
  end

  @lowest_offers
end

#offer_page(page = 1) ⇒ Array

get offers from a given page

Parameters:

  • page (Integer) (defaults to: 1)

    the page number you want to get

Returns:

  • (Array)

    Array of Offers returned from the page



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/ramazon/product.rb', line 244

def offer_page(page = 1)
  #get all offers
  if page == 1 && has_first_page_of_full_offers
    self.offers 
  else
    products = self.class.find(:item_id => self.asin, 
      :response_group => "OfferListings",
      :merchant_id => "All",
      :condition => "All",
      :offer_page => page)

    if products
      product = products[0] 
      self.offer_pages = self.class.offer_pages_for(product)
      product.offers
    else
      []
    end
  end
end

#offer_pagesObject



237
238
239
# File 'lib/ramazon/product.rb', line 237

def offer_pages
  @offer_pages
end

#offer_pages=(pages) ⇒ Object



233
234
235
# File 'lib/ramazon/product.rb', line 233

def offer_pages=(pages)
  @offer_pages = pages.to_i
end

#offers_by_conditionHash

breaks down all the offers in a nested hash of [condition] note: this will load ALL offers into memory

Returns:

  • (Hash)

    a nest hash of offers [condition] => Array of Ramazon::Offer objects



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/ramazon/product.rb', line 181

def offers_by_condition
  @offer_hash = {}
  offer_page = 1
  
  offers = offer_page(offer_page)
  while offer_page <= offer_pages
    offers.each do |o|
      @offer_hash[o.condition.downcase] ||= {}
      @offer_hash[o.condition.downcase][o.sub_condition] ||= []
      @offer_hash[o.condition.downcase][o.sub_condition] << o
    end

    offer_page += 1
    offers = offer_page(offer_page)
  end

  @offer_hash
end

#used_offersObject

a sorted list of used offers



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ramazon/product.rb', line 165

def used_offers
  if @used_offers.nil?
    @used_offers = []
    self.offers.each do |o|
      if o.condition.downcase == "used"
        @used_offers << o
      end
    end
    @used_offers.sort!{|a,b| a.price.amount <=> b.price.amount}
  end
  @used_offers
end