adds specs for all property types
This commit is contained in:
parent
a02055fd93
commit
9fae5c7248
6 changed files with 60 additions and 40 deletions
|
@ -8,7 +8,7 @@ module Microformats2
|
||||||
def to_hash
|
def to_hash
|
||||||
hash = { type: [type], properties: {} }
|
hash = { type: [type], properties: {} }
|
||||||
@added_methods.each do |method_name|
|
@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
|
end
|
||||||
hash
|
hash
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,11 +12,13 @@ module Microformats2
|
||||||
class DateTimeProperty
|
class DateTimeProperty
|
||||||
def parse(element)
|
def parse(element)
|
||||||
DateTime.parse(element.attribute("datetime") || property.text)
|
DateTime.parse(element.attribute("datetime") || property.text)
|
||||||
|
rescue ArgumentError => e
|
||||||
|
element.attribute("datetime") || property.text
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
class EmbeddedProperty
|
class EmbeddedProperty
|
||||||
def parse(element)
|
def parse(element)
|
||||||
element.text
|
element.text.gsub(/\n+/, " ").gsub(/\s+/, " ").strip
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -2,28 +2,45 @@ require "spec_helper"
|
||||||
require "microformats2"
|
require "microformats2"
|
||||||
|
|
||||||
describe Microformats2::Collection do
|
describe Microformats2::Collection do
|
||||||
before do
|
describe "with simple h-card" do
|
||||||
@html = <<-HTML.strip
|
before do
|
||||||
<div class="h-card"><p class="p-name">Jessica Lynn Suttles</p></div>
|
html = "spec/support/simple_hcard.html"
|
||||||
HTML
|
@collection = Microformats2.parse(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
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
describe "#to_json" do
|
describe "#parse" do
|
||||||
it "returns the correct JSON" do
|
it "creates ruby class HCard" do
|
||||||
json = {items: [
|
@collection.h_card.should be_kind_of HCard
|
||||||
{type: ["h-card"], properties: {name: "Jessica Lynn Suttles"}}
|
end
|
||||||
]}.to_json
|
it "assigns .h-card .p-name to HCard#name" do
|
||||||
@collection.to_json.should == json
|
@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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,17 +9,8 @@ describe Microformats2 do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "::parse" do
|
describe "::parse" do
|
||||||
before do
|
|
||||||
@microformats2 = Microformats2.parse(@html)
|
|
||||||
end
|
|
||||||
it "returns a collection" do
|
it "returns a collection" do
|
||||||
@microformats2.should be_kind_of Microformats2::Collection
|
Microformats2.parse(@html).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"
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -28,8 +19,8 @@ describe Microformats2 do
|
||||||
Microformats2.read_html(@html).should include @html
|
Microformats2.read_html(@html).should include @html
|
||||||
end
|
end
|
||||||
it "can be a file path to html" do
|
it "can be a file path to html" do
|
||||||
html = "spec/support/simple.html"
|
html = "spec/support/simple_hcard.html"
|
||||||
Microformats2.read_html(html).should include @html
|
Microformats2.read_html(html).should include "<div class=\"h-card\">"
|
||||||
end
|
end
|
||||||
it "can be a url to html" do
|
it "can be a url to html" do
|
||||||
html = "http://google.com"
|
html = "http://google.com"
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
|
||||||
<html>
|
|
||||||
<body>
|
|
||||||
<div class="h-card"><p class="p-name">Jessica Lynn Suttles</p></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
16
spec/support/simple_hcard.html
Normal file
16
spec/support/simple_hcard.html
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<div class="h-card">
|
||||||
|
<a href="http://twitter.com/jlsuttles" class="u-url p-name">
|
||||||
|
Jessica Lynn Suttles
|
||||||
|
</a>
|
||||||
|
<time class="dt-bday" datetime="1990-10-15T20:45:33-08:00">
|
||||||
|
October 15, 1990
|
||||||
|
</time>
|
||||||
|
<div class="e-content">
|
||||||
|
Vegan. Cat lover. Coder.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Add a link
Reference in a new issue