From 07d510ab7de824ac97273b18a8a33fbe15b81ff4 Mon Sep 17 00:00:00 2001 From: Jessica Lynn Suttles Date: Mon, 4 Feb 2013 13:40:33 -0800 Subject: [PATCH] allows for parsing of html string, file path, or url --- lib/microformats2.rb | 13 ++++++++++++- spec/lib/microformats2_spec.rb | 23 +++++++++++++++++++++++ spec/support/simple.html | 6 ++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 spec/lib/microformats2_spec.rb create mode 100644 spec/support/simple.html diff --git a/lib/microformats2.rb b/lib/microformats2.rb index bbf8533..03e1ac8 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -1,5 +1,16 @@ +require "nokogiri" +require "open-uri" require "microformats2/version" module Microformats2 - # Your code goes here... + def self.parse(html) + html = read_html(html) + Nokogiri::HTML(html) + end + + def self.read_html(html) + open(html).read + rescue Errno::ENOENT => e + html + end end diff --git a/spec/lib/microformats2_spec.rb b/spec/lib/microformats2_spec.rb new file mode 100644 index 0000000..8e54335 --- /dev/null +++ b/spec/lib/microformats2_spec.rb @@ -0,0 +1,23 @@ +require "spec_helper" +require "microformats2" + +describe Microformats2 do + describe "::read_html" do + before do + @html = <<-HTML.strip +

Jessica Lynn Suttles

+ HTML + end + it "can be a string of html" 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 + end + it "can be a url to html" do + html = "http://google.com" + Microformats2.read_html(html).should include "google" + end + end +end diff --git a/spec/support/simple.html b/spec/support/simple.html new file mode 100644 index 0000000..307441c --- /dev/null +++ b/spec/support/simple.html @@ -0,0 +1,6 @@ + + + +

Jessica Lynn Suttles

+ +