class Amazon::Product

Amazon::Product objects are returned by many of the basic forms of search in Amazon::Search::Request.

Public Instance Methods

[](key) click to toggle source

Fake the appearance of a product as a hash. key should be any attribute of the product, as returned by Amazon::Product#properties.

E.g.

puts product['our_price'] => "$8.99"
puts product[:our_price]  => "$8.99"
# File lib/amazon.rb, line 141
def [](key)
  self.instance_variable_get('@' + key.to_s)
end
to_h() click to toggle source

Converts an Amazon::Product to a Hash.

# File lib/amazon.rb, line 122
def to_h
  hash = {}
  self.properties.each do |property|
    key = property[1..-1]
    val = self.instance_variable_get(property)
    hash[key] = val
  end
  hash
end
to_s(first_detail='productname') click to toggle source

Displays a product in a human-readable format. Call without a parameter, as the parameter is only for internal use.

# File lib/amazon.rb, line 97
def to_s(first_detail='productname')
  string = ""

  # get all attributes, minus the leading '@'
  vars = self.instance_variables.sort.map {|v| v[1..-1]}

  # find the longest attribute name
  longest = vars.sort { |a,b| b.length <=> a.length }[0].length

  # put the product name at the front of the list, if we have one
  vars.unshift(first_detail) if vars.delete(first_detail)

  # display the product's details
  vars.each do |iv|
    value = self.instance_variable_get("@#{iv}").inspect
    string << "%-#{longest}s = %s\n" % [iv, value]
  end

  string

end