Class: Amazon::Associates::Item

Inherits:
ApiResult show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/amazon-associates/types/item.rb,
lib/amazon-associates/types/browse_node.rb

Direct Known Subclasses

CartItem

Constant Summary collapse

PER_PAGE =
10

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all(opts) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/amazon-associates/types/item.rb', line 118

def self.all(opts)
  opts = opts.dup.to_options!
  unless %w[All Blended Merchants].include? opts[:search_index]
    opts.reverse_merge!(:merchant_id => 'Amazon',
                        :condition => 'All')
  end
  opts[:availability] ||= 'Available' unless opts[:condition].nil? or opts[:condition] == 'New'
  opts[:item_page]    ||= (opts.delete(:page) || 1)
  prep_responses(opts)

  response = Amazon::Associates.item_search(opts)

  # TODO: Max count is different for different indexes, for example, All only returns 5 pages
  WillPaginate::Collection.create(response.current_page, PER_PAGE, response.total_results) do |pager|
    # TODO: Some of the returned items may not include offers, we may need something like this:
    #.reject {|i| i.offers[:totaloffers] == '0' }
    pager.replace response.items
  end
end

.find(scope, opts = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/amazon-associates/types/item.rb', line 73

def self.find(scope, opts = {})
  opts = opts.dup # it seems this hash picks up some internal amazon stuff if we don't copy it

  if scope.is_a? String
    opts.merge!(:item_id => scope, :item_type => 'ASIN')
    scope = :one
  end

  case scope
  when :top_sellers then top_sellers(opts)
  when :similar then similar(opts)
  when :all     then all(opts)
  when :first   then first(opts)
  when :one     then one(opts)
  else
    raise ArgumentError, "scope should be :all, :first, :one, or an item id string"
  end
end

.first(opts) ⇒ Object



114
115
116
# File 'lib/amazon-associates/types/item.rb', line 114

def self.first(opts)
  all(opts).first
end

.one(opts) ⇒ Object



138
139
140
141
# File 'lib/amazon-associates/types/item.rb', line 138

def self.one(opts)
  prep_responses(opts)
  Amazon::Associates.item_lookup(opts.delete(:item_id), opts).items.first
end

.similar(*ids) ⇒ Object



100
101
102
103
104
# File 'lib/amazon-associates/types/item.rb', line 100

def self.similar(*ids)
  opts = ids.extract_options!
  opts.reverse_merge!(:response_group => SMALL_RESPONSE_GROUPS)
  Amazon::Associates.similarity_lookup(ids, opts).items
end

.top_sellers(opts) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/amazon-associates/types/item.rb', line 92

def self.top_sellers(opts)
  opts = opts.dup.to_options!
  opts.merge!(:response_group => 'TopSellers')
  opts[:browse_node_id] = opts.delete(:browse_node).id if opts[:browse_node]
  items = Amazon::Associates.browse_node_lookup(opts).top_sellers.map {|i| i.text_at('asin') }
  Amazon::Associates.item_lookup(:item_id => items * ',', :response_group => SMALL_RESPONSE_GROUPS).items
end

Instance Method Details

#==(other) ⇒ Object



51
52
53
# File 'lib/amazon-associates/types/item.rb', line 51

def ==(other)
  asin == other.asin
end

#amazon_product_descriptionObject



47
48
49
# File 'lib/amazon-associates/types/item.rb', line 47

def amazon_product_description
  editorial_reviews.detect {|r| r.source == 'Amazon.com Product Description' }.try(:content)
end

#authorObject



59
60
61
# File 'lib/amazon-associates/types/item.rb', line 59

def author
  authors.only
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/amazon-associates/types/item.rb', line 55

def eql?(other)
  asin == other.asin
end

#inspectObject



67
68
69
# File 'lib/amazon-associates/types/item.rb', line 67

def inspect
  "#<#{self.class}: #{asin} #{attributes.inspect}>"
end

#persisted?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/amazon-associates/types/item.rb', line 43

def persisted?
  true
end

#similarObject



143
144
145
# File 'lib/amazon-associates/types/item.rb', line 143

def similar
  Item.similar(asin)
end

#titleObject



63
64
65
# File 'lib/amazon-associates/types/item.rb', line 63

def title
  root_title || attr_title
end