lib/fassbinder/response.rb in fassbinder-0.0.8 vs lib/fassbinder/response.rb in fassbinder-0.0.9
- old
+ new
@@ -1,77 +1,52 @@
-module Fassbinder
+require 'fassbinder/book_builder'
+require 'fassbinder/errors'
- # And I don't believe that melodramatic feelings are laughable - they should
- # be taken absolutely seriously.
- #
+module Fassbinder
class Response
include Enumerable
- DEFAULT_SHIPPING_CENTS = { :us => 399,
- :uk => 280,
- :de => 299,
- :ca => 649,
- :fr => 300,
- :jp => 25000 }
-
def initialize(response, locale)
- raise InvalidResponseError unless response.valid?
+ unless response.valid?
+ message =
+ if response.has_errors?
+ response.errors.first['Message']
+ else
+ response.code
+ end
+ raise InvalidResponse, message
+ end
+
@response = response
- @locale = locale
+ @locale = locale.to_sym
end
# Yields each snapshot to given block.
#
def each(&block)
- @response.each('Item') { |doc| block.call(parse(doc)) }
+ @response.each('Item') { |doc| block.call(build_book(doc)) }
end
def errors
@response.errors.map do |error|
error['Message'].scan(/[0-9A-Z]{10}/).first rescue nil
end.compact
end
private
- def parse(doc)
- Kosher::Book.new(
- 'amazon.' + Sucker::Request::HOSTS[@locale].match(/[^.]+$/).to_s,
- nil,
- doc['ASIN'],
- doc['SalesRank'].to_i,
- doc['Offers']['TotalOffers'].to_i,
- [doc['Offers']['Offer']].flatten.compact.map do |doc|
- if doc['OfferListing']['Price']['CurrencyCode'] == 'JPY'
- doc['OfferListing']['Price']['Amount'] = doc['OfferListing']['Price']['Amount'].to_i * 100
- end
+ def build_book(doc)
+ builder = BookBuilder.new
- Kosher::Offer.new(
- doc['OfferListing']['OfferListingId'],
- Kosher::Item.new(doc['OfferListing']['Price']['Amount'].to_i,
- doc['OfferListing']['Price']['CurrencyCode'],
- doc['OfferListing']['Quantity'].to_i,
- Kosher::Condition.new(case doc['OfferAttributes']['SubCondition']
- when 'new' then 1
- when 'mint' then 2
- when 'verygood' then 3
- when 'good' then 4
- when 'acceptable' then 5
- else 6
- end),
- Kosher::Description.new(doc['OfferAttributes']['ConditionNote'].to_s)),
- Kosher::Seller.new(doc['Merchant']['MerchantId'],
- doc['Merchant']['Name'],
- doc['Merchant']['AverageFeedbackRating'].to_f,
- Kosher::Location.new((doc['Merchant']['Location']['CountryCode'] rescue nil),
- (doc['Merchant']['Location']['StateCode'] rescue nil))),
- Kosher::Shipping.new(doc['OfferListing']['IsEligibleForSuperSaverShipping'] == '1' ?
- 0 : DEFAULT_SHIPPING_CENTS[@locale],
- doc['OfferListing']['Price']['CurrencyCode'],
- Kosher::Availability.new(doc['OfferListing']['AvailabilityAttributes']['MaximumHours'].to_i))
- )
- end
- )
+ builder.asin = doc['ASIN']
+ builder.offers_total = doc['Offers']['TotalOffers']
+ host = Sucker::Request::HOSTS[@locale]
+ builder.venue = "amazon.#{host.match(/[^.]+$/)}"
+
+ offers = doc['Offers']['Offer']
+ offers.each { |offer| builder.add_offer(offer) }
+
+ builder.book
end
end
end