diff --git a/README.md b/README.md index 52493ff..ff82ff7 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Not Implemented: ## Current Version -2.0.0.pre4 +2.0.0.pre5 ## Requirements diff --git a/lib/microformats2.rb b/lib/microformats2.rb index e5a9257..8515004 100644 --- a/lib/microformats2.rb +++ b/lib/microformats2.rb @@ -4,6 +4,7 @@ require "json" require "active_support/inflector" require "microformats2/version" +require "microformats2/parser" require "microformats2/format_parser" require "microformats2/property_parser" require "microformats2/collection" @@ -22,17 +23,13 @@ require "microformats2/implied_property/url" module Microformats2 class << self def parse(html) - html = read_html(html) - document = Nokogiri::HTML(html) - Collection.new(document).parse + Parser.new.parse(html) end def read_html(html) - open(html).read - rescue Errno::ENOENT, Errno::ENAMETOOLONG => e - html + Parser.new.read_html(html) end end # class << self class InvalidPropertyPrefix < StandardError; end -end \ No newline at end of file +end diff --git a/lib/microformats2/parser.rb b/lib/microformats2/parser.rb new file mode 100644 index 0000000..4a3d13d --- /dev/null +++ b/lib/microformats2/parser.rb @@ -0,0 +1,25 @@ +module Microformats2 + class Parser + attr_reader :http_headers, :http_body + + def initialize + @http_headers = {} + end + + def parse(html, headers={}) + html = read_html(html, headers) + document = Nokogiri::HTML(html) + Collection.new(document).parse + end + + def read_html(html, headers={}) + open(html, headers) do |response| + @http_headers = response.meta if response.respond_to?(:meta) + @http_body = response.read + end + @http_body + rescue Errno::ENOENT, Errno::ENAMETOOLONG => e + @http_body = html + end + end +end diff --git a/lib/microformats2/version.rb b/lib/microformats2/version.rb index 28675c5..60003dc 100644 --- a/lib/microformats2/version.rb +++ b/lib/microformats2/version.rb @@ -1,3 +1,3 @@ module Microformats2 - VERSION = "2.0.0.pre4" + VERSION = "2.0.0.pre5" end diff --git a/microformats2.gemspec b/microformats2.gemspec index 5c56e00..9dc26ee 100644 --- a/microformats2.gemspec +++ b/microformats2.gemspec @@ -26,5 +26,5 @@ Gem::Specification.new do |gem| gem.add_development_dependency "guard-rspec", "~> 2.1.0" gem.add_development_dependency "rb-fsevent", "~> 0.9.1" gem.add_development_dependency "simplecov", "~> 0.7.1" - gem.add_development_dependency "debugger", "~> 1.2.1" + gem.add_development_dependency "webmock", "~> 1.12.3" end diff --git a/spec/lib/microformats2/parser_spec.rb b/spec/lib/microformats2/parser_spec.rb new file mode 100644 index 0000000..56cea33 --- /dev/null +++ b/spec/lib/microformats2/parser_spec.rb @@ -0,0 +1,41 @@ +require "spec_helper" +require "microformats2" + +describe Microformats2::Parser do + let(:parser) { Microformats2::Parser.new } + + describe "#http_headers" do + it "starts as a blank hash" do + parser.http_headers.should eq({}) + end + + describe "open file" do + before do + parser.parse("spec/support/lib/microformats2/simple.html") + end + + it "doesn't save #http_headers" do + parser.http_headers.should eq({}) + end + it "saves #http_body" do + parser.http_body.should include "" + end + end + + describe "http response" do + before do + stub_request(:get, "http://www.example.com/"). + with(:headers => {"Accept"=>"*/*", "User-Agent"=>"Ruby"}). + to_return(:status => 200, :body => "abc", :headers => {"Content-Length" => 3}) + parser.parse("http://www.example.com") + end + + it "saves #http_headers" do + parser.http_headers.should eq({"content-length" => "3"}) + end + it "saves #http_body" do + parser.http_body.should eq("abc") + end + end + end +end diff --git a/spec/lib/microformats2_spec.rb b/spec/lib/microformats2_spec.rb index 613ac75..31cf743 100644 --- a/spec/lib/microformats2_spec.rb +++ b/spec/lib/microformats2_spec.rb @@ -23,6 +23,9 @@ describe Microformats2 do Microformats2.read_html(html).should include "
" end it "can be a url to html" do + stub_request(:get, "http://google.com/"). + with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). + to_return(:status => 200, :body => "google", :headers => {}) html = "http://google.com" Microformats2.read_html(html).should include "google" end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7034fa3..b7815d8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ SimpleCov.start require 'rubygems' require 'rspec' require 'rspec/autorun' +require 'webmock/rspec' RSpec.configure do |config| config.order = "random"