require 'spec_helper' module GutenbergRdf describe Rdf do let(:xml) do ' 2006-09-28 en Project Gutenberg Public domain in the USA. ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects an id" do expect(rdf.id).to eql "98765" end it "expects a published date" do expect(rdf.published).to eql "2006-09-28" end it "expects a publisher" do expect(rdf.publisher).to eql "Project Gutenberg" end it "expects a language" do expect(rdf.language).to eql "en" end it "expects the rights" do expect(rdf.rights).to eql "Public domain in the USA." end describe "#type" do let(:xml) do ' Text ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expect the type of entity" do expect(rdf.type).to eql 'Text' end end describe "Titles" do let(:xml) do ' A Great Title ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects a title" do expect(rdf.title).to eql 'A Great Title' end it "expects subtitle to be empty" do expect(rdf.subtitle).to eql '' end context "with a title and subtitle, on separate lines" do let(:xml) do ' A Great Multi-Title Or, a Subtitle ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects the title to be the first line" do expect(rdf.title).to eql 'A Great Multi-Title' end it "expects the subtitle to be the second line" do expect(rdf.subtitle).to eql 'Or, a Subtitle' end end context "when title:subtitle are separated by a colon" do let(:xml) do ' A Great Multi-Title: And a Subtitle ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects a title" do expect(rdf.title).to eql 'A Great Multi-Title' end it "expects a subtitle" do expect(rdf.subtitle).to eql 'And a Subtitle' end end context "when title; and subtitle are separated by a semi-colon" do let(:xml) do ' A Great Multi-Title; Or, a Subtitle ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects a title" do expect(rdf.title).to eql 'A Great Multi-Title' end it "expects a subtitle" do expect(rdf.subtitle).to eql 'Or, a Subtitle' end context "...except when subtitles already exists" do let(:xml) do ' A Great Multi-Title; and some other text Then a Subtitle on a newline ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects a title" do expect(rdf.title).to eql 'A Great Multi-Title; and some other text' end it "expects a subtitle" do expect(rdf.subtitle).to eql 'Then a Subtitle on a newline' end end end end describe "#authors" do let(:xml) do ' 1830 1905 Dodge, Mary Mapes Dodge, Mary Verschillende Various ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects a Array" do expect(rdf.authors.class).to be Array end it "expects correct number to be returned" do expect(rdf.authors.count).to be 2 end it "expects an author object" do expect(rdf.authors.first.class).to be Rdf::Agent end end describe "#subjects" do let(:xml) do %q{ Children's literature -- Periodicals Children's periodicals, American PZ } end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects correct number to be returned" do expect(rdf.subjects.count).to be 2 end it "expects the correct data" do expect(rdf.subjects.first).to eql "Children's literature -- Periodicals" expect(rdf.subjects.last).to eql "Children's periodicals, American" end end describe "#covers" do describe "official PG covers" do let(:xml) do ' http://www.gutenberg.org/files/12345/12345-h/images/cover.jpg 92652 application/epub+zip 2013-09-21T19:22:32.115259 10856 image/jpeg 2013-09-21T19:22:34.484114 1904 image/jpeg 2013-09-21T19:22:34.379124 ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects the correct number of entries returned" do expect(rdf.covers.count).to be 3 end it "expects those to be used" do expect(rdf.covers[0]).to eql 'http://www.gutenberg.org/ebooks/12345.cover.medium' expect(rdf.covers[1]).to eql 'http://www.gutenberg.org/ebooks/12345.cover.small' end it "expects any other images to be listed after the official ones" do expect(rdf.covers[2]).to eql 'http://www.gutenberg.org/files/12345/12345-h/images/cover.jpg' end end describe "HTML ebook cover image" do let(:xml) do ' file:///public/vhost/g/gutenberg/html/files/12345/12345-rst/images/cover.jpg file:///public/vhost/g/gutenberg/html/files/12345/12345-h/images/cover.jpg http://www.gutenberg.org/files/12345/12345-h/images/cover.jpg ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects only unique entries" do expect(rdf.covers.count).to be 2 end it "should convert File URIs to the Gutenberg URL" do expect(rdf.covers.first).to match 'http://www.gutenberg.org' end it "expects the covers to be listed in the correct order" do expect(rdf.covers[0]).to eql 'http://www.gutenberg.org/files/12345/12345-h/images/cover.jpg' expect(rdf.covers[1]).to eql 'http://www.gutenberg.org/files/12345/12345-rst/images/cover.jpg' end end end describe "#ebook" do let(:xml) do ' 293684 text/plain; charset=utf-8 2010-02-16T08:29:52.373092 116685 application/zip text/plain; charset=us-ascii 2006-09-28T12:37:26 ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "expects the correct number of entries" do expect(rdf.ebooks.count).to be 2 end it "expects an entry Hash to have the correct keys" do expect(rdf.ebooks.first).to have_key :uri expect(rdf.ebooks.first).to have_key :mime_type expect(rdf.ebooks.first).to have_key :encoding expect(rdf.ebooks.first).to have_key :modified end it "expcts the modified value to be a DateTime" do expect(rdf.ebooks.first[:modified].class).to be DateTime end it "should return the URL" do expect(rdf.ebooks.first[:uri]).to eql 'http://www.gutenberg.org/ebooks/98765.txt.utf-8' end it "should return the mime_type" do expect(rdf.ebooks.first[:mime_type]).to eql 'text/plain' end it "should return the encoding" do expect(rdf.ebooks.first[:encoding]).to eql 'utf-8' end it "should return the modified datetime" do expect(rdf.ebooks.first[:modified].to_s).to eql '2010-02-16T08:29:52-07:00' end context "when there are two mime-types" do let(:xml) do ' 116685 application/zip text/plain; charset=us-ascii 2006-09-28T12:37:26 ' end let(:rdf) { Rdf.new(Nokogiri::XML(xml)) } it "should use just the first one" do expect(rdf.ebooks.first[:mime_type]).to eql 'application/zip' end it "expects the encoding to be an empty string" do expect(rdf.ebooks.first[:encoding]).to eql '' end end end end end