From 22d84e0845eef7bf5beac92c186f0ce7a781153c Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 16:31:39 -0700 Subject: [PATCH 1/9] adds Microformats::Parser to move module methods into class instance --- lib/microformats2.rb | 11 ++++------- lib/microformats2/parser.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 lib/microformats2/parser.rb 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..680a034 --- /dev/null +++ b/lib/microformats2/parser.rb @@ -0,0 +1,15 @@ +module Microformats2 + class Parser + def parse(html) + html = read_html(html) + document = Nokogiri::HTML(html) + Collection.new(document).parse + end + + def read_html(html) + open(html).read + rescue Errno::ENOENT, Errno::ENAMETOOLONG => e + html + end + end +end From ed2f66002e8979f827734d88d0d2f35acfc43e55 Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 16:46:08 -0700 Subject: [PATCH 2/9] adds Microformats2::Parser#http_headers and Microformats2::Parser#http_body and saves them from the response --- lib/microformats2/parser.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/microformats2/parser.rb b/lib/microformats2/parser.rb index 680a034..1238690 100644 --- a/lib/microformats2/parser.rb +++ b/lib/microformats2/parser.rb @@ -1,13 +1,19 @@ module Microformats2 class Parser - def parse(html) - html = read_html(html) + attr_reader :http_headers, :http_body + + def parse(html, headers={}) + html = read_html(html, headers) document = Nokogiri::HTML(html) Collection.new(document).parse end - def read_html(html) - open(html).read + 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 html end From 78df1dc8bcbef23a2dee7c5e5c5683d4e1cebbf3 Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 16:47:28 -0700 Subject: [PATCH 3/9] sets Microformats2::Parser#http_headers on initialize --- lib/microformats2/parser.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/microformats2/parser.rb b/lib/microformats2/parser.rb index 1238690..6ee4e6e 100644 --- a/lib/microformats2/parser.rb +++ b/lib/microformats2/parser.rb @@ -2,13 +2,17 @@ module Microformats2 class Parser attr_reader :http_headers, :http_body - def parse(html, headers={}) + def initialize + @http_headers = {} + end + + def parse(html, headers=@http_headers) html = read_html(html, headers) document = Nokogiri::HTML(html) Collection.new(document).parse end - def read_html(html, headers={}) + def read_html(html, headers=@http_headers) open(html, headers) do |response| @http_headers = response.meta if response.respond_to?(:meta) @http_body = response.read From 8a5e63dd21921bf64b592c3f7221c071c00fabb7 Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 17:34:04 -0700 Subject: [PATCH 4/9] adds webmock gem to development dependencies and requires in spec_helper.rb --- microformats2.gemspec | 1 + spec/spec_helper.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/microformats2.gemspec b/microformats2.gemspec index 5c56e00..074a483 100644 --- a/microformats2.gemspec +++ b/microformats2.gemspec @@ -27,4 +27,5 @@ Gem::Specification.new do |gem| 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/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" From 3f61f48025272b7542d7d4862c01582cbbf9a80e Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 17:34:15 -0700 Subject: [PATCH 5/9] adds parser specs --- lib/microformats2/parser.rb | 2 +- spec/lib/microformats2/parser_spec.rb | 41 +++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 spec/lib/microformats2/parser_spec.rb diff --git a/lib/microformats2/parser.rb b/lib/microformats2/parser.rb index 6ee4e6e..8f9383b 100644 --- a/lib/microformats2/parser.rb +++ b/lib/microformats2/parser.rb @@ -19,7 +19,7 @@ module Microformats2 end @http_body rescue Errno::ENOENT, Errno::ENAMETOOLONG => e - html + @http_body = html end end 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 From 27822a835f4e68640cf64bbfe5496eb48f3f6129 Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 17:38:18 -0700 Subject: [PATCH 6/9] removes debugger gem from development dependencies. sorry travis! --- microformats2.gemspec | 1 - 1 file changed, 1 deletion(-) diff --git a/microformats2.gemspec b/microformats2.gemspec index 074a483..9dc26ee 100644 --- a/microformats2.gemspec +++ b/microformats2.gemspec @@ -26,6 +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 From 73d25abefe1c9a091ef4b7f5f57e52513512f87c Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 17:43:31 -0700 Subject: [PATCH 7/9] adds stub_request for outside http call --- spec/lib/microformats2_spec.rb | 3 +++ 1 file changed, 3 insertions(+) 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 From 1311dec8a37242ae727cd9ed43ca17cb178cb859 Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 18:31:25 -0700 Subject: [PATCH 8/9] removes saved headers from param defaults --- lib/microformats2/parser.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/microformats2/parser.rb b/lib/microformats2/parser.rb index 8f9383b..4a3d13d 100644 --- a/lib/microformats2/parser.rb +++ b/lib/microformats2/parser.rb @@ -6,13 +6,13 @@ module Microformats2 @http_headers = {} end - def parse(html, headers=@http_headers) + def parse(html, headers={}) html = read_html(html, headers) document = Nokogiri::HTML(html) Collection.new(document).parse end - def read_html(html, headers=@http_headers) + def read_html(html, headers={}) open(html, headers) do |response| @http_headers = response.meta if response.respond_to?(:meta) @http_body = response.read From c0a86fa6b06e40634e664ad837b82ef9d883d32d Mon Sep 17 00:00:00 2001 From: Jessica Dillon & Jessica Lynn Suttles Date: Mon, 1 Jul 2013 18:32:38 -0700 Subject: [PATCH 9/9] updates version --- README.md | 2 +- lib/microformats2/version.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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