require "spec_helper" module Barnie describe Client do let (:client) do Barnie.new("company details") end let(:isbns) do [ "9780142404447", "9780080433042", "9780001053335", "9781902741154", "9780001053373", "9781902084008", "9781903098189", "9780001053380", "9780195177312", "9780001053397", "9780201549768", "9780141310763", "9780142404430", "9780080433028", "9780080448527", "9780001053618", "9781903098196", "9780152022624", "9781903098202", "9780001053656", "9780203940174", "9780152008680", "9780080426648", "9780001053731", "9781902741109", "9780195366877", "9782715219359", "9780001053748", "9781905334186", "9780001054097", "9780152022617", "9780080441993", "9780001054608", "9780203940112", "9781905340309", "9780152008673", "9780080426631", "9780001054646", "9781905340279", "9781902934006", "9780001054677", "9780001054691", "9781920888855", "9781920888862", "9780152022600", "9780001054721", "9780203939994", "9780195366860", "9780152008666", "9781902932255" ] end before do VCR.insert_cassette('barnie_client') client.isbns = isbns end after do VCR.eject_cassette end it "includes Helpers" do Client.ancestors.should include Helpers end it "requires user agent" do lambda { Barnie::Client.new }.should raise_error /wrong number of arguments/ end it "sets up a mechanize agent" do client.instance_variable_get(:@agent).should be_an_instance_of Mechanize end it "sets ISBNs to query" do client.isbns.should == isbns end it "returns the URI with which to query Barnes & Noble" do client.send(:uri).should be_an_instance_of URI::HTTP end it "performs a request" do page = client.request page.should be_an_instance_of Mechanize::Page end describe "#books" do let(:books) do client.books end it "are an array of hashes" do books.should be_an_instance_of Array books.each { |book| book.should be_an_instance_of Hash } end it "must have a title and ISBN" do books.each do |book| book[:title].should_not be_nil book[:isbn].should_not be_nil end end it "has authors, binding, ships_in and price" do books.each do |book| book.has_key?(:authors).should be_true book.has_key?(:binding).should be_true book.has_key?(:ships_in).should be_true book.has_key?(:price).should be_true end end it "returns a result for a single ISBN" do client.isbns = [isbns.first] books = client.books books.size.should eql 1 book = books.first book[:title].should_not be_nil book[:isbn].should_not be_nil book.should have_key(:price) book.should have_key(:ships_in) end context "when page does not include #prod-container element" do let(:mock_page) do page = mock("response") page.stub!(:search).and_return(nil) page end context "when page body is empty and HTTP is OK" do before do mock_page.stub!(:body).and_return("") mock_page.stub!(:code).and_return(200) client.stub!(:request).and_return(mock_page) end it "throws a Barnie error" do lambda {client.books}.should raise_error Barnie::Error end end end end end end