diff --git a/lib/microformats2/format.rb b/lib/microformats2/format.rb
index 7b56681..74b618c 100644
--- a/lib/microformats2/format.rb
+++ b/lib/microformats2/format.rb
@@ -8,7 +8,7 @@ module Microformats2
def to_hash
hash = { type: [type], properties: {} }
@added_methods.each do |method_name|
- hash[:properties][method_name.to_sym] = send(method_name)
+ hash[:properties][method_name.to_sym] = send(method_name).to_s
end
hash
end
diff --git a/lib/microformats2/property.rb b/lib/microformats2/property.rb
index 19f74db..b6c5438 100644
--- a/lib/microformats2/property.rb
+++ b/lib/microformats2/property.rb
@@ -12,11 +12,13 @@ module Microformats2
class DateTimeProperty
def parse(element)
DateTime.parse(element.attribute("datetime") || property.text)
+ rescue ArgumentError => e
+ element.attribute("datetime") || property.text
end
end
class EmbeddedProperty
def parse(element)
- element.text
+ element.text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
end
end
diff --git a/spec/lib/microformats2/collection_spec.rb b/spec/lib/microformats2/collection_spec.rb
index ebb015f..c6d062c 100644
--- a/spec/lib/microformats2/collection_spec.rb
+++ b/spec/lib/microformats2/collection_spec.rb
@@ -2,28 +2,45 @@ require "spec_helper"
require "microformats2"
describe Microformats2::Collection do
- before do
- @html = <<-HTML.strip
-
- HTML
- @collection = Microformats2::Collection.new.parse(Nokogiri::HTML(@html))
- end
-
- describe "#to_hash" do
- it "returns the correct Hash" do
- hash = {items: [
- {type: ["h-card"], properties: {name: "Jessica Lynn Suttles"}}
- ]}
- @collection.to_hash.should == hash
+ describe "with simple h-card" do
+ before do
+ html = "spec/support/simple_hcard.html"
+ @collection = Microformats2.parse(html)
end
- end
- describe "#to_json" do
- it "returns the correct JSON" do
- json = {items: [
- {type: ["h-card"], properties: {name: "Jessica Lynn Suttles"}}
- ]}.to_json
- @collection.to_json.should == json
+ describe "#parse" do
+ it "creates ruby class HCard" do
+ @collection.h_card.should be_kind_of HCard
+ end
+ it "assigns .h-card .p-name to HCard#name" do
+ @collection.h_card.name.should == "Jessica Lynn Suttles"
+ end
+ it "assigns .h-card .u-url to HCard#url" do
+ @collection.h_card.url.should == "http://twitter.com/jlsuttles"
+ end
+ it "assings .h-card .dt-bday to HCard#bday" do
+ @collection.h_card.bday.should be_kind_of DateTime
+ @collection.h_card.bday.to_s.should == "1990-10-15T20:45:33-08:00"
+ end
+ it "assigns .h-card .e-content to HCard#content" do
+ @collection.h_card.content.should == "Vegan. Cat lover. Coder."
+ end
+ end
+
+ describe "#to_hash" do
+ it "returns the correct Hash" do
+ hash = {
+ :items => [{ :type => ["h-card"],
+ :properties => {
+ :url => "http://twitter.com/jlsuttles",
+ :name => "Jessica Lynn Suttles",
+ :bday => "1990-10-15T20:45:33-08:00",
+ :content => "Vegan. Cat lover. Coder."
+ }
+ }]
+ }
+ @collection.to_hash.should == hash
+ end
end
end
end
diff --git a/spec/lib/microformats2_spec.rb b/spec/lib/microformats2_spec.rb
index 57a3634..11fd8a3 100644
--- a/spec/lib/microformats2_spec.rb
+++ b/spec/lib/microformats2_spec.rb
@@ -9,17 +9,8 @@ describe Microformats2 do
end
describe "::parse" do
- before do
- @microformats2 = Microformats2.parse(@html)
- end
it "returns a collection" do
- @microformats2.should be_kind_of Microformats2::Collection
- end
- it "assigns root formats to collection" do
- @microformats2.h_card.should be_kind_of HCard
- end
- it "assigns properties to found root microformats" do
- @microformats2.h_card.name.should == "Jessica Lynn Suttles"
+ Microformats2.parse(@html).should be_kind_of Microformats2::Collection
end
end
@@ -28,8 +19,8 @@ describe Microformats2 do
Microformats2.read_html(@html).should include @html
end
it "can be a file path to html" do
- html = "spec/support/simple.html"
- Microformats2.read_html(html).should include @html
+ html = "spec/support/simple_hcard.html"
+ Microformats2.read_html(html).should include ""
end
it "can be a url to html" do
html = "http://google.com"
diff --git a/spec/support/simple.html b/spec/support/simple.html
deleted file mode 100644
index 307441c..0000000
--- a/spec/support/simple.html
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-
-
-
-
diff --git a/spec/support/simple_hcard.html b/spec/support/simple_hcard.html
new file mode 100644
index 0000000..fd350a6
--- /dev/null
+++ b/spec/support/simple_hcard.html
@@ -0,0 +1,16 @@
+
+
+
+
+
+