Merge pull request #10 from G5/conditional-get-52510097

Conditional get 52510097
This commit is contained in:
Jessica Lynn Suttles 2013-07-01 18:35:55 -07:00
commit f2cc8b037a
8 changed files with 77 additions and 10 deletions

View file

@ -33,7 +33,7 @@ Not Implemented:
## Current Version
2.0.0.pre4
2.0.0.pre5
## Requirements

View file

@ -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
end

View file

@ -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

View file

@ -1,3 +1,3 @@
module Microformats2
VERSION = "2.0.0.pre4"
VERSION = "2.0.0.pre5"
end

View file

@ -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

View file

@ -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 "<!DOCTYPE html>"
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

View file

@ -23,6 +23,9 @@ describe Microformats2 do
Microformats2.read_html(html).should include "<div class=\"h-card\">"
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

View file

@ -4,6 +4,7 @@ SimpleCov.start
require 'rubygems'
require 'rspec'
require 'rspec/autorun'
require 'webmock/rspec'
RSpec.configure do |config|
config.order = "random"