lib/barnie/response.rb in barnie-0.2.0 vs lib/barnie/response.rb in barnie-0.3.0
- old
+ new
@@ -3,41 +3,56 @@
module Barnie
class Response
include Helpers
- attr_accessor :page
+ attr_reader :page
def initialize(page)
- self.page = page
+ raise Error.new('Blank page') if page.body.empty? && page.code == 200
+
+ @page = page
end
- def snapshots
- raise Error.new('Blank page') if page.body == '' && page.code == 200
+ # Yields each snapshot to given block.
+ #
+ def each
+ container.each { |html| yield parse(html) }
+ end
- container = page.search('#prod-container')
- container.map do |html|
+ # Returns an array of snapshots.
+ #
+ def to_a
+ container.map { |html| parse(html) }
+ end
- price = extract_price(html.search('.price strong').text)
- hours = extract_ships_in(html.search('.availability').text) || 999
- title = title(html)
- link = extract_link(title)
- isbn = extract_isbn(link)
+ private
- Kosher::Snapshot.new(
- 'bn.com',
- isbn,
- nil,
- nil,
- price > 0 ? 1 : 0,
- price > 0 ?
- [Kosher::Offer.new(
- nil,
- Kosher::Item.new(price, 'USD', 1, Kosher::Condition.new(1), Kosher::Description.new('')),
- Kosher::Seller.new(nil, 'Barnes & Noble.com', nil, Kosher::Location.new('US')),
- Kosher::Shipping.new(0, 'USD', Kosher::Availability.new(hours))
- )] :
- [])
- end
+ def container
+ page.search('#prod-container')
+ end
+
+ def parse(html)
+ price = extract_price(html.search('.price strong').text)
+ hours = extract_ships_in(html.search('.availability').text) || 999
+ title = title(html)
+ link = extract_link(title)
+ isbn = extract_isbn(link)
+
+ Kosher::Snapshot.new(
+ 'bn.com',
+ isbn,
+ nil,
+ nil,
+ price > 0 ? 1 : 0,
+ price > 0 ?
+ [Kosher::Offer.new(
+ nil,
+ Kosher::Item.new(price, 'USD', 1, Kosher::Condition.new(1), Kosher::Description.new('')),
+ Kosher::Seller.new(nil, 'Barnes & Noble.com', nil, Kosher::Location.new('US')),
+ Kosher::Shipping.new(0, 'USD', Kosher::Availability.new(hours))
+ )] :
+ [])
+
end
end
end