Skip to content

Commit

Permalink
CurlAgent contructor can receive options now
Browse files Browse the repository at this point in the history
  • Loading branch information
romanbsd committed Mar 3, 2009
1 parent 607a3bc commit f7a1b93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
26 changes: 14 additions & 12 deletions lib/curl_agent.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
require 'curb' require 'curb'


class CurlAgent class CurlAgent
def initialize(url) # See CurlAgent::open for explanation about options
def initialize(url, options = {})
@curl = Curl::Easy.new(url) @curl = Curl::Easy.new(url)
# Defaults # Defaults
@curl.headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6' @curl.headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6'
Expand All @@ -13,6 +14,17 @@ def initialize(url)
@curl.connect_timeout = 5 @curl.connect_timeout = 5
@curl.timeout = 30 @curl.timeout = 30
@performed = false @performed = false

options ||= {}
options.each {|k, v|
# Strings will be passed as headers, as in original open-uri
next unless k.is_a? Symbol
@curl.send("#{k}=".intern, v)
options.delete(k)
}

# All that's left should be considered headers
@curl.headers.merge!(options)
end end


# Do the actual fetch, after which it's possible to call body_str method # Do the actual fetch, after which it's possible to call body_str method
Expand Down Expand Up @@ -61,17 +73,7 @@ def self.open(name, *rest, &block)
raise ArgumentError.new("invalid access mode #{mode} (resource is read only.)") raise ArgumentError.new("invalid access mode #{mode} (resource is read only.)")
end end


agent = CurlAgent.new(name) agent = CurlAgent.new(name, options)

options ||= {}
options.each {|k, v|
# Strings will be passed as headers, as in original open-uri
next unless k.is_a? Symbol
agent.send("#{k}=".intern, v)
options.delete(k)
}
# All that's left should be considered headers
agent.headers.merge!(options)


agent.perform! agent.perform!
io = StringIO.new(agent.body_str) io = StringIO.new(agent.body_str)
Expand Down
21 changes: 13 additions & 8 deletions spec/curl_agent_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,21 +62,26 @@
describe 'when used with open' do describe 'when used with open' do
before(:each) do before(:each) do
@headers = {'User-Agent'=>'foo'} @headers = {'User-Agent'=>'foo'}
@curl = mock('curl') @curl_easy = mock('curl_easy')
@curl.stub!(:headers).and_return(@headers) Curl::Easy.should_receive(:new).and_return(@curl_easy)
@curl.stub!(:perform!) @curl_easy.stub!(:headers).and_return(@headers)
@curl.stub!(:body_str).and_return('test') @curl_easy.stub!(:follow_location=)
CurlAgent.should_receive(:new).and_return(@curl) @curl_easy.stub!(:max_redirects=)
@curl_easy.stub!(:enable_cookies=)
@curl_easy.stub!(:connect_timeout=)
@curl_easy.stub!(:timeout=)
@curl_easy.stub!(:perform)
@curl_easy.stub!(:body_str).and_return('test')
end end


it 'shall permit to specify user-agent' do it 'shall permit to specify user-agent' do
@curl.headers['User-Agent'].should_not == 'curl' @curl_easy.headers['User-Agent'].should_not == 'curl'
CurlAgent.open('http://www.example.com/', 'User-Agent'=>'curl') CurlAgent.open('http://www.example.com/', 'User-Agent'=>'curl')
@curl.headers['User-Agent'].should == 'curl' @curl_easy.headers['User-Agent'].should == 'curl'
end end


it 'shall permit to override timeout' do it 'shall permit to override timeout' do
@curl.should_receive(:'timeout=').once.with(10) @curl_easy.should_receive(:'timeout=').once.with(10)
CurlAgent.open('http://www.example.com/', :timeout => 10) CurlAgent.open('http://www.example.com/', :timeout => 10)
end end


Expand Down

0 comments on commit f7a1b93

Please sign in to comment.