lib/fassbinder/response.rb in fassbinder-0.0.5 vs lib/fassbinder/response.rb in fassbinder-0.0.6

- old
+ new

@@ -1,66 +1,82 @@ module Fassbinder + + # And I don't believe that melodramatic feelings are laughable - they should + # be taken absolutely seriously. + # class Response def initialize(response, locale) raise InvalidResponseError unless response.valid? @response = response @locale = locale end - # And I don't believe that melodramatic feelings are laughable - - # they should be taken absolutely seriously. - def snapshots - @response.map('Item') do |doc| - Kosher::Snapshot.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 + # Yields each snapshot to given block. + # + def each + @response.each('Item') { |doc| yield parse(doc) } + end - 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 : (case @locale - when :us then 399 - when :uk then 280 - when :de then 299 - when :ca then 649 - when :fr then 300 - when :jp then 25000 - end), - doc['OfferListing']['Price']['CurrencyCode'], - Kosher::Availability.new(doc['OfferListing']['AvailabilityAttributes']['MaximumHours'].to_i)) - ) - end - ) + # Returns an array of snapshots. + # + def to_a + @response.map('Item') do |doc| + parse(doc) end 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::Snapshot.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 + + 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 : (case @locale + when :us then 399 + when :uk then 280 + when :de then 299 + when :ca then 649 + when :fr then 300 + when :jp then 25000 + end), + doc['OfferListing']['Price']['CurrencyCode'], + Kosher::Availability.new(doc['OfferListing']['AvailabilityAttributes']['MaximumHours'].to_i)) + ) + end + ) end end end