eat 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/lib/eat.rb +19 -10
  2. data/lib/eat/version.rb +1 -1
  3. data/test/test_eat.rb +8 -0
  4. metadata +8 -8
data/lib/eat.rb CHANGED
@@ -3,15 +3,24 @@ require 'httpclient'
3
3
 
4
4
  require 'eat/version'
5
5
 
6
- # http://weblog.jamisbuck.org/2007/2/7/infinity
7
- unless defined?(::Infinity)
8
- ::Infinity = 1.0/0
9
- end
10
-
11
6
  module Eat
12
7
  # httpclient 2.2.3 inserts the platform info for you, albeit with problems
13
8
  # AGENT_NAME = "Mozilla/5.0 (#{::RUBY_PLATFORM}) Ruby/#{::RUBY_VERSION} HTTPClient/#{::HTTPClient::VERSION} eat/#{::Eat::VERSION}"
14
9
  AGENT_NAME = "eat/#{::Eat::VERSION}"
10
+
11
+ # http://weblog.jamisbuck.org/2007/2/7/infinity
12
+ INFINITY = 1.0/0
13
+
14
+ # https://github.com/nahi/httpclient/blob/master/lib/httpclient.rb#L640
15
+ REDIRECT_HANDLER = ::Proc.new do |uri, res|
16
+ newuri = ::URI.parse(res.header['location'][0])
17
+ unless newuri.is_a?(::URI::HTTP)
18
+ newuri = uri + newuri
19
+ end
20
+ newuri
21
+ end
22
+
23
+ TIMEOUT = 2
15
24
 
16
25
  module ObjectExtensions
17
26
  # <tt>url</tt> can be filesystem or http/https
@@ -26,7 +35,7 @@ module Eat
26
35
  # eat('http://brighterplanet.com', :timeout => 10) #=> '...'
27
36
  # eat('http://brighterplanet.com', :limit => 1) #=> '.'
28
37
  def eat(url, options = {})
29
- limit = options.fetch(:limit, ::Infinity)
38
+ limit = options.fetch(:limit, INFINITY)
30
39
 
31
40
  uri = ::URI.parse url.to_s
32
41
 
@@ -46,20 +55,20 @@ module Eat
46
55
  end
47
56
 
48
57
  when 'http', 'https'
49
- timeout = options.fetch(:timeout, 2)
58
+ timeout = options.fetch(:timeout, TIMEOUT)
50
59
  openssl_verify_mode = options.fetch(:openssl_verify_mode, ::OpenSSL::SSL::VERIFY_PEER)
51
60
  if openssl_verify_mode == 'none'
52
61
  openssl_verify_mode = ::OpenSSL::SSL::VERIFY_NONE
53
62
  end
54
63
  http = ::HTTPClient.new
55
64
  http.agent_name = AGENT_NAME
56
- http.redirect_uri_callback = ::Proc.new { |uri, res| ::URI.parse(res.header['location'][0]) }
65
+ http.redirect_uri_callback = REDIRECT_HANDLER
57
66
  http.transparent_gzip_decompression = true
58
67
  http.receive_timeout = timeout
59
68
  if uri.scheme == 'https'
60
69
  http.ssl_config.verify_mode = openssl_verify_mode
61
70
  end
62
- if limit == ::Infinity
71
+ if limit == INFINITY
63
72
  body << http.get_content(uri.to_s)
64
73
  else
65
74
  catch :stop do
@@ -72,7 +81,7 @@ module Eat
72
81
  end
73
82
  end
74
83
 
75
- limit == ::Infinity ? body.join : body.join[0...limit]
84
+ limit == INFINITY ? body.join : body.join[0...limit]
76
85
  end
77
86
  end
78
87
  end
@@ -1,3 +1,3 @@
1
1
  module Eat
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -86,4 +86,12 @@ class TestEat < Test::Unit::TestCase
86
86
  def test_more_chunks
87
87
  assert eat('http://www.ebay.com/itm/ws/eBayISAPI.dll?ViewItem&item=260910713854?_trksid=p5197.m1256&_trkparms=clkid%3D4726644202417880745').include?('<title>')
88
88
  end
89
+
90
+ def test_ignores_anchor
91
+ assert eat('http://www.orbitgum.com/#/home').include?('Orbit')
92
+ end
93
+
94
+ def test_relative_redirect_handling
95
+ assert eat("http://www.crutchfield.com/p_054D3100/Nikon-D3100-Kit.html").include?('Product Information')
96
+ end
89
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-12-07 00:00:00.000000000Z
12
+ date: 2011-12-13 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httpclient
16
- requirement: &2164292480 !ruby/object:Gem::Requirement
16
+ requirement: &2155438940 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2164292480
24
+ version_requirements: *2155438940
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: test-unit
27
- requirement: &2164292060 !ruby/object:Gem::Requirement
27
+ requirement: &2155438520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *2164292060
35
+ version_requirements: *2155438520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &2164291640 !ruby/object:Gem::Requirement
38
+ requirement: &2155437960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2164291640
46
+ version_requirements: *2155437960
47
47
  description: A (better?) replacement for open-uri. Gets the contents of local and
48
48
  remote files as a String, no questions asked.
49
49
  email: