Skip to content

Commit

Permalink
send user-agent to satisfy sites like bestbuy.com
Browse files Browse the repository at this point in the history
  • Loading branch information
seamusabshere committed Dec 7, 2011
1 parent 6a3f753 commit 0bef333
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/eat.rb
@@ -1,4 +1,5 @@
require 'uri'
require 'httpclient'

# http://weblog.jamisbuck.org/2007/2/7/infinity
unless defined?(::Infinity)
Expand All @@ -7,6 +8,8 @@

module Eat
module ObjectExtensions
AGENT_NAME = "Mozilla/5.0 (#{::RUBY_PLATFORM}) Ruby/#{::RUBY_VERSION} HTTPClient/#{::HTTPClient::VERSION} eat/#{::Eat::VERSION}"

# <tt>url</tt> can be filesystem or http/https
#
# Options:
Expand Down Expand Up @@ -39,13 +42,13 @@ def eat(url, options = {})
end

when 'http', 'https'
require 'httpclient'
timeout = options.fetch(:timeout, 2)
openssl_verify_mode = options.fetch(:openssl_verify_mode, ::OpenSSL::SSL::VERIFY_PEER)
if openssl_verify_mode == 'none'
openssl_verify_mode = ::OpenSSL::SSL::VERIFY_NONE
end
http = ::HTTPClient.new
http.agent_name = AGENT_NAME
http.redirect_uri_callback = ::Proc.new { |uri, res| ::URI.parse(res.header['location'][0]) }
http.transparent_gzip_decompression = true
http.receive_timeout = timeout
Expand Down
4 changes: 4 additions & 0 deletions test/test_eat.rb
Expand Up @@ -78,4 +78,8 @@ def test_reads_compressed
def test_chunks_work_out
assert eat('http://www.thinkgeek.com/interests/giftsforhim/60b6/').include?('DOCTYPE HTML PUBLIC')
end

def test_satisfies_sites_that_require_user_agent
assert eat('http://www.bestbuy.com').include?('images.bestbuy.com')
end
end

0 comments on commit 0bef333

Please sign in to comment.