diff --git a/lib/microformats2.rb b/lib/microformats2.rb index f212271..4b14073 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -5,8 +5,11 @@ require 'date' module Microformats2 VERSION = "1.0.0" + class LoadError < StandardError; end + def self.parse(html) - raise LoadError unless [String, File].include?(html.class) + raise LoadError, "argument must be a String or File" unless [String, File].include?(html.class) + doc = Nokogiri::HTML(html) microformats = Hash.new{|hash, key| hash[key] = Array.new} doc.css("*[class^=h-]").each do |microformat| @@ -132,8 +135,6 @@ module Microformats2 end end - class LoadError < StandardError; end - # Thank you Rails Developers for your unitentional contribution to this project # File activesupport/lib/active_support/inflector/inflections.rb, line 206 def self.classify(str) diff --git a/test/test_microformats2.rb b/test/test_microformats2.rb index 5ed54a2..f79e74e 100644 --- a/test/test_microformats2.rb +++ b/test/test_microformats2.rb @@ -2,18 +2,6 @@ require "test/unit" require "microformats2" class TestMicroformats2 < Test::Unit::TestCase - def test_acceptence_of_string - assert_nothing_raised Microformats2::LoadError do - Microformats2.parse("A String") - end - end - - def test_acceptence_of_file - assert_nothing_raised Microformats2::LoadError do - Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "hcard.html"))) - end - end - def test_throw_exception_on_non_string_params assert_raise Microformats2::LoadError do Microformats2.parse(nil) @@ -30,6 +18,22 @@ class TestMicroformats2 < Test::Unit::TestCase assert_equal 0, result.size end + def test_extracts_hcard_from_an_html_file + hcard = <<-END + + + Simple hCard + + + +

Chris

+ + + END + result = Microformats2.parse(File.open(File.join(File.dirname(__FILE__), "hcard.html"))) + assert_equal HCard, result[:hcard].first.class + end + def test_extracts_hcard_from_html hcard = <<-END