diff --git a/spec/book_spec.cr b/spec/book_spec.cr new file mode 100644 index 0000000..84451b5 --- /dev/null +++ b/spec/book_spec.cr @@ -0,0 +1,96 @@ +require "./spec_helper" + +describe Muse::Dl::Book do + it "it should parse the infobox" do + html = <<-EOT +
+
+

Additional Information

+
+
+
+
+ ISBN +
+
+ 9780813928517 +
+
+
+
+ Related ISBN +
+
+ 9780813928456 +
+
+
+
+ MARC Record +
+
+ Download +
+
+
+
+ OCLC +
+
+ 755633557 +
+
+
+
+ Pages +
+
+ 432 +
+
+
+
+ Launched on MUSE +
+
+ 2012-01-01 +
+
+
+
+ DOI +
+ +
+
+
+ Language +
+
+ English +
+
+
+
+ Open Access +
+
+ No +
+
+
+
+EOT + book = Muse::Dl::Book.new html + book.info["ISBN"].should eq "9780813928517" + book.info["Related ISBN"].should eq "9780813928456" + book.info["OCLC"].should eq "755633557" + book.info["Pages"].should eq "432" + book.info["Launched on MUSE"].should eq "2012-01-01" + book.info["Language"].should eq "English" + book.info["Open Access"].should eq "No" + book.info["DOI"].should eq "10.1353/book.68534" + end +end diff --git a/src/book.cr b/src/book.cr new file mode 100644 index 0000000..adef853 --- /dev/null +++ b/src/book.cr @@ -0,0 +1,12 @@ +require "./infoparser.cr" + +module Muse::Dl + class Book + @info = Hash(String, String).new + getter :info + + def initialize(html : String) + @info = InfoParser.parse(html) + end + end +end diff --git a/src/infoparser.cr b/src/infoparser.cr new file mode 100644 index 0000000..d355a46 --- /dev/null +++ b/src/infoparser.cr @@ -0,0 +1,20 @@ +require "myhtml" + +module Muse::Dl + class InfoParser + def self.parse(html : String) + info = Hash(String, String).new + myhtml = Myhtml::Parser.new(html) + myhtml.css(".details_row").each do |row| + label = row.css(".cell").map(&.inner_text).to_a[0].strip + value = row.css(".cell").map(&.inner_text).to_a[1].strip + case label + when "MARC Record" + else + info[label] = value + end + end + return info + end + end +end diff --git a/src/journal.cr b/src/journal.cr new file mode 100644 index 0000000..1d1559e --- /dev/null +++ b/src/journal.cr @@ -0,0 +1,12 @@ +require "./infoparser.cr" + +module Muse::Dl + class Journal + @info = Hash(String, String).new + getter :info + + def initialize(html : String) + @info = InfoParser.parse(html) + end + end +end