Permalink
Browse files

configuration for item type

  • Loading branch information...
1 parent 2434605 commit b401a3e6b3db49445e19704c9c33e44b853027bd @phoet phoet committed with Apr 27, 2011
Showing with 35 additions and 11 deletions.
  1. +7 −3 lib/asin.rb
  2. +7 −5 lib/asin/configuration.rb
  3. +3 −3 lib/asin/item.rb
  4. +18 −0 spec/search_spec.rb
View
@@ -131,7 +131,7 @@ def configure(options={})
#
def lookup(asin, params={})
response = call(params.merge(:Operation => :ItemLookup, :ItemId => asin))
- Item.new(response['ItemLookupResponse']['Items']['Item'])
+ handle_item(response['ItemLookupResponse']['Items']['Item'])
end
# Performs an +ItemSearch+ REST call against the Amazon API.
@@ -153,7 +153,7 @@ def lookup(asin, params={})
def search_keywords(*keywords)
params = keywords.last.is_a?(Hash) ? keywords.pop : {:SearchIndex => :Books}
response = call(params.merge(:Operation => :ItemSearch, :Keywords => keywords.join(' ')))
- (response['ItemSearchResponse']['Items']['Item'] || []).map {|item| Item.new(item)}
+ (response['ItemSearchResponse']['Items']['Item'] || []).map {|item| handle_item(item)}
end
# Performs an +ItemSearch+ REST call against the Amazon API.
@@ -172,7 +172,7 @@ def search_keywords(*keywords)
#
def search(params={:SearchIndex => :Books})
response = call(params.merge(:Operation => :ItemSearch))
- (response['ItemSearchResponse']['Items']['Item'] || []).map {|item| Item.new(item)}
+ (response['ItemSearchResponse']['Items']['Item'] || []).map {|item| handle_item(item)}
end
# Performs an +CartCreate+ REST call against the Amazon API.
@@ -250,6 +250,10 @@ def clear_cart(cart)
end
private
+
+ def handle_item(item)
+ Configuration.item_type.is_a?(Class) ? Configuration.item_type.new(item) : item
+ end
def create_item_params(items)
keyword_mappings = {
View
@@ -5,6 +5,7 @@ class Configuration
class << self
attr_accessor :secret, :key, :host, :logger
+ attr_accessor :item_type
# Rails initializer configuration.
#
@@ -65,11 +66,12 @@ def reset
def init_config(force=false)
return if @init && !force
- @init = true
- @secret = ''
- @key = ''
- @host = 'webservices.amazon.com'
- @logger = Logger.new(STDERR)
+ @init = true
+ @secret = ''
+ @key = ''
+ @host = 'webservices.amazon.com'
+ @logger = Logger.new(STDERR)
+ @item_type = SimpleItem
end
end
end
View
@@ -3,13 +3,13 @@
module ASIN
- # =Item
+ # =SimpleItem
#
- # The +Item+ class is a wrapper for the Amazon XML-REST-Response.
+ # The +SimpleItem+ class is a wrapper for the Amazon XML-REST-Response.
#
# A Hashie::Mash is used for the internal data representation and can be accessed over the +raw+ attribute.
#
- class Item
+ class SimpleItem
attr_reader :raw
View
@@ -64,6 +64,24 @@ module ASIN
item = @helper.lookup(ANY_ASIN)
item.title.should =~ /Learn Objective/
end
+
+ it "should return a custom item class" do
+ module TEST
+ class TestItem
+ attr_accessor :testo
+ def initialize(hash)
+ @testo = hash
+ end
+ end
+ end
+ @helper.configure :item_type => TEST::TestItem
+ @helper.lookup(ANY_ASIN).testo.should_not be_nil
+ end
+
+ it "should return a raw value" do
+ @helper.configure :item_type => :raw
+ @helper.lookup(ANY_ASIN)['ItemAttributes']['Title'].should_not be_nil
+ end
it "should search_keywords a book with fulltext" do
items = @helper.search_keywords 'Learn', 'Objective-C'

0 comments on commit b401a3e

Please sign in to comment.