Skip to content

Commit

Permalink
- Allow user to add custom headers when requesting URL
Browse files Browse the repository at this point in the history
  • Loading branch information
huyha85 committed Feb 20, 2013
1 parent e0fe139 commit 2d828be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
12 changes: 8 additions & 4 deletions lib/open_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@
class OpenGraph
attr_accessor :src, :url, :type, :title, :description, :images, :metadata, :response, :original_images

def initialize(src, fallback = true)
def initialize(src, fallback = true, options = {})
if fallback.is_a? Hash
options = fallback
fallback = true
end
@src = src
@images = []
@metadata = {}
parse_opengraph
parse_opengraph(options)
load_fallback if fallback
check_images_path
end

private
def parse_opengraph
def parse_opengraph(options = {})
begin
@response = RedirectFollower.new(@src).resolve
@response = RedirectFollower.new(@src, options).resolve
rescue
@title = @url = @src
return
Expand Down
14 changes: 10 additions & 4 deletions lib/redirect_follower.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
require 'net/https'

class RedirectFollower
REDIRECT_DEFAULT_LIMIT = 5
class TooManyRedirects < StandardError; end

attr_accessor :url, :body, :redirect_limit, :response
attr_accessor :url, :body, :redirect_limit, :response, :headers

def initialize(url, limit = 5)
def initialize(url, limit = REDIRECT_DEFAULT_LIMIT, options = {})
if limit.is_a? Hash
options = limit
limit = REDIRECT_DEFAULT_LIMIT
end
@url, @redirect_limit = url, limit
@headers = options[:headers] || {}
end

def resolve
Expand All @@ -17,9 +23,9 @@ def resolve
https = Net::HTTP.new(uri.host, 443)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
self.response = https.request_get(uri.request_uri)
self.response = https.request_get(uri.request_uri, @headers)
else
self.response = Net::HTTP.get_response(uri)
self.response = Net::HTTP.get_response(uri, @headers)
end

if response.kind_of?(Net::HTTPRedirection)
Expand Down
19 changes: 14 additions & 5 deletions spec/lib/redirect_follower_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

res = RedirectFollower.new(url).resolve
res.body.should == "Body is here."
res.redirect_limit.should == 5
res.redirect_limit.should == RedirectFollower::REDIRECT_DEFAULT_LIMIT
end

describe "and uri scheme is HTTPS" do
Expand All @@ -30,19 +30,28 @@

res = RedirectFollower.new(https_url).resolve
res.body.should == "Body is here."
res.redirect_limit.should == 5
res.redirect_limit.should == RedirectFollower::REDIRECT_DEFAULT_LIMIT
end
end

describe "and has headers option" do
it "should add headers when retrieve the uri" do
Net::HTTP.should_receive(:get_response).with(URI.parse(URI.escape(url)), {'User-Agent' => 'My Custom User-Agent'}).and_return(mock_res)
res = RedirectFollower.new(url, {:headers => {'User-Agent' => 'My Custom User-Agent'}}).resolve
res.body.should == "Body is here."
res.redirect_limit.should == RedirectFollower::REDIRECT_DEFAULT_LIMIT
end
end
end

context "with redirection" do
it "should follow the link in redirection" do
Net::HTTP.should_receive(:get_response).with(URI.parse(URI.escape(url))).and_return(mock_redirect)
Net::HTTP.should_receive(:get_response).with(URI.parse(URI.escape("http://new.test.host"))).and_return(mock_res)
Net::HTTP.should_receive(:get_response).with(URI.parse(URI.escape(url)), {}).and_return(mock_redirect)
Net::HTTP.should_receive(:get_response).with(URI.parse(URI.escape("http://new.test.host")), {}).and_return(mock_res)

res = RedirectFollower.new(url).resolve
res.body.should == "Body is here."
res.redirect_limit.should == 4
res.redirect_limit.should == RedirectFollower::REDIRECT_DEFAULT_LIMIT - 1
end
end

Expand Down

0 comments on commit 2d828be

Please sign in to comment.