# encoding: utf-8 require "spec_helper" module Barnie describe Helpers do let(:mock_page) do page = mock('response') page.stub!(:body).and_return('foo') page.stub!(:code) page end before do @response = Response.new(mock_page) end it "sanitizes string" do @response.sanitize_string('"I am" a book \r\n "').should eql 'I am" a book' end it "extract price" do @response.extract_price('$14.95 abc').should eql 1495 @response.extract_price(nil).should eql 0 @response.extract_price('').should eql 0 end it "extracts ISBN" do @response.extract_isbn('http://search.barnesandnoble.com/Best-of-Waffles-Pancakes/Jane-Stacey/e/9780002554756/?itm=1').should eql "9780002554756" @response.extract_isbn('http://music.barnesandnoble.com/Live/Brian-Regan/e/706442377723/?itm=1').should eql "0706442377723" @response.extract_isbn('"http://music.barnesandnoble.com/Getting-a-Good-Nights-Sleep/John-Selby/e/52296602529/?itm=1"').should eql "0052296602529" @response.extract_isbn(nil).should be_nil @response.extract_isbn('').should be_nil end it "extracts binding" do @response.extract_binding(' Paperback ').should eql "Paperback" @response.extract_binding(nil).should be_nil @response.extract_binding('').should be_nil end it "extracts maximum shipping hours" do @response.extract_ships_in('Usually ships within 24 hours').should eql 24 @response.extract_ships_in('Usually ships within 2-3 days').should eql 3 * 24 @response.extract_ships_in('Usually ships within 7 days').should eql 7 * 24 @response.extract_ships_in('Usually ships within 7 days').should eql 7 * 24 @response.extract_ships_in('A new copy is not available from Barnes & Noble.com at this time.').should be_nil @response.extract_ships_in(nil).should be_nil @response.extract_ships_in('').should be_nil end context "requiring real HTML objects" do let(:request) do Request.new('Company Info') end before do VCR.insert_cassette('barnie') end after do VCR.eject_cassette end context "ebook" do before do request << ["9781400826094"] @response = request.get @html = @response.page.search("#prod-container").first end it "returns title as XML NodeSet" do @response.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet end it "extracts title" do @response.extract_title(@response.title(@html)).should eql "Birth of the Symbol : Ancient Readers at the Limits of Their Texts" end it "extracts link" do @response.extract_link(@response.title(@html)).should eql "http://search.barnesandnoble.com/Birth-of-the-Symbol/Peter-T-Struck/e/9781400826094/?itm=1" end it "extracts authors" do @response.extract_authors(@html).should =~ ["Peter T. Struck"] end end context "paperback" do before do request << ["9780816614028"] @response = request.get @html = @response.page.search("#prod-container").first end it "returns title as XML NodeSet" do @response.title(@html).should be_an_instance_of Nokogiri::XML::NodeSet end it "extracts title" do @response.extract_title(@response.title(@html)).should eql "A Thousand Plateaus : Capitalism and Schizophrenia" end it "extracts link" do @response.extract_link(@response.title(@html)).should eql "http://search.barnesandnoble.com/A-Thousand-Plateaus/Gilles-Deleuze/e/9780816614028/?itm=1" end it "extracts authors" do @response.extract_authors(@html).should =~ ["Brian Massumi", "Gilles Deleuze"] end end end end end