eat 0.0.7 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +5 -0
- data/README.rdoc +19 -11
- data/Rakefile +11 -0
- data/lib/eat.rb +47 -21
- data/lib/eat/version.rb +1 -1
- data/test/test_eat.rb +15 -20
- metadata +32 -53
data/.gitignore
CHANGED
data/CHANGELOG
ADDED
data/README.rdoc
CHANGED
@@ -1,25 +1,33 @@
|
|
1
1
|
=eat
|
2
2
|
|
3
|
-
|
3
|
+
Problems with the standard <tt>open-uri</tt> library
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
* sometimes it returns <tt>String</tt> and sometimes <tt>StringIO</tt> (check out <tt>OpenURI::Buffer::StringMax</tt>, usually 10,240 bytes)
|
6
|
+
* sometimes you have to call <tt>gets</tt> (for example <tt>open('http://yahoo.com').gets</tt>)
|
7
|
+
* it overrides <tt>#open</tt> everywhere, which may be confusing.
|
8
8
|
|
9
|
-
Try
|
9
|
+
Try <tt>#eat</tt>, which ALWAYS returns a <tt>String</tt>:
|
10
10
|
|
11
11
|
require 'eat'
|
12
|
-
eat('http://
|
13
|
-
eat('/home/seamus/foo.txt')
|
12
|
+
eat('http://yahoo.com') #=> String
|
13
|
+
eat('/home/seamus/foo.txt') #=> String
|
14
|
+
eat('file:///home/seamus/foo.txt') #=> String
|
14
15
|
|
15
|
-
==
|
16
|
+
==Options
|
16
17
|
|
17
|
-
|
18
|
+
eat('http://yahoo.com', :timeout => 10) # timeout after 10 seconds
|
19
|
+
eat('http://yahoo.com', :limit => 1024) # only read the first 1024 chars
|
20
|
+
|
21
|
+
==Warning: doesn't verify SSL certs
|
22
|
+
|
23
|
+
If you need to check SSL certificates, please don't use this gem. It always sets
|
24
|
+
|
25
|
+
http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
|
18
26
|
|
19
27
|
==Supported schemas
|
20
28
|
|
21
|
-
* local filesystem
|
22
|
-
* http
|
29
|
+
* local filesystem
|
30
|
+
* http
|
23
31
|
* https (it won't check the SSL certificate... if you want security, don't use this!)
|
24
32
|
|
25
33
|
Copyright 2011 Seamus Abshere
|
data/Rakefile
CHANGED
@@ -8,3 +8,14 @@ Rake::TestTask.new(:test) do |test|
|
|
8
8
|
test.pattern = 'test/**/test_*.rb'
|
9
9
|
test.verbose = true
|
10
10
|
end
|
11
|
+
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
Rake::RDocTask.new do |rdoc|
|
14
|
+
rdoc.rdoc_dir = 'rdoc'
|
15
|
+
rdoc.title = 'eat'
|
16
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
17
|
+
rdoc.rdoc_files.include('README*')
|
18
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :test
|
data/lib/eat.rb
CHANGED
@@ -1,42 +1,68 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'singleton'
|
3
3
|
|
4
|
+
# http://weblog.jamisbuck.org/2007/2/7/infinity
|
5
|
+
unless defined?(::Infinity)
|
6
|
+
::Infinity = 1.0/0
|
7
|
+
end
|
8
|
+
|
4
9
|
module Eat
|
5
|
-
def self.config
|
6
|
-
::Eat::Config.instance
|
7
|
-
end
|
8
|
-
|
9
|
-
class Config
|
10
|
-
include ::Singleton
|
11
|
-
attr_writer :remote_timeout
|
12
|
-
def remote_timeout
|
13
|
-
@remote_timeout || 2 #seconds
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
10
|
module ObjectExtensions
|
18
|
-
|
19
|
-
|
11
|
+
# <tt>url</tt> can be filesystem or http/https
|
12
|
+
#
|
13
|
+
# Options:
|
14
|
+
# * <tt>:timeout</tt> in seconds
|
15
|
+
# * <tt>:limit</tt> is characters (bytes in Ruby 1.8)
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# eat('http://brighterplanet.com') #=> '...'
|
19
|
+
# eat('http://brighterplanet.com', :timeout => 10) #=> '...'
|
20
|
+
# eat('http://brighterplanet.com', :limit => 1) #=> '.'
|
21
|
+
def eat(url, options = {})
|
22
|
+
timeout = options[:timeout] || options['timeout'] || 2
|
23
|
+
limit = options[:limit] || options['limit'] || ::Infinity
|
24
|
+
uri = ::URI.parse url.to_s
|
25
|
+
|
26
|
+
body = []
|
27
|
+
read_so_far = 0
|
28
|
+
|
20
29
|
case uri.scheme
|
30
|
+
|
21
31
|
when 'file', nil
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
32
|
+
chunk_size = limit < 1_048_576 ? limit : 1_048_576
|
33
|
+
::File.open(uri.path, 'r') do |f|
|
34
|
+
while chunk = f.read(chunk_size)
|
35
|
+
break if read_so_far > limit
|
36
|
+
read_so_far += chunk_size
|
37
|
+
body << chunk
|
38
|
+
end
|
26
39
|
end
|
40
|
+
|
27
41
|
when 'http', 'https'
|
28
42
|
require 'net/http'
|
29
43
|
require 'net/https' if uri.scheme == 'https'
|
30
|
-
(defined?(::SystemTimer) ? ::SystemTimer : ::Timeout).timeout(
|
44
|
+
(defined?(::SystemTimer) ? ::SystemTimer : ::Timeout).timeout(timeout) do
|
31
45
|
http = ::Net::HTTP.new uri.host, uri.port
|
32
46
|
if uri.scheme == 'https'
|
33
47
|
http.use_ssl = true
|
34
48
|
# if you were trying to be real safe, you wouldn't use this library
|
35
49
|
http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
|
36
50
|
end
|
37
|
-
http.start
|
38
|
-
|
51
|
+
http.start do |session|
|
52
|
+
catch :stop do
|
53
|
+
session.get(uri.request_uri, 'Accept-Encoding' => '') do |chunk|
|
54
|
+
throw :stop if read_so_far > limit
|
55
|
+
read_so_far += chunk.length
|
56
|
+
body << chunk
|
57
|
+
end
|
58
|
+
session.finish
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
39
63
|
end
|
64
|
+
|
65
|
+
limit == ::Infinity ? body.join : body.join[0...limit]
|
40
66
|
end
|
41
67
|
end
|
42
68
|
end
|
data/lib/eat/version.rb
CHANGED
data/test/test_eat.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
1
2
|
require 'helper'
|
2
3
|
|
3
4
|
require 'tempfile'
|
@@ -9,10 +10,6 @@ class Tempfile
|
|
9
10
|
end
|
10
11
|
|
11
12
|
class TestEat < Test::Unit::TestCase
|
12
|
-
def setup
|
13
|
-
::Eat.config.remote_timeout = 10
|
14
|
-
end
|
15
|
-
|
16
13
|
def test_filesystem
|
17
14
|
assert eat(__FILE__).include?('class TestEat < Test::Unit::TestCase')
|
18
15
|
end
|
@@ -22,27 +19,15 @@ class TestEat < Test::Unit::TestCase
|
|
22
19
|
end
|
23
20
|
|
24
21
|
def test_uri
|
25
|
-
assert eat(::URI.parse('http://brighterplanet.com/robots.txt')).include?('User-agent')
|
22
|
+
assert eat(::URI.parse('http://brighterplanet.com/robots.txt'), :timeout => 10).include?('User-agent')
|
26
23
|
end
|
27
24
|
|
28
25
|
def test_http
|
29
|
-
assert eat('http://brighterplanet.com/robots.txt').include?('User-agent')
|
26
|
+
assert eat('http://brighterplanet.com/robots.txt', :timeout => 10).include?('User-agent')
|
30
27
|
end
|
31
28
|
|
32
29
|
def test_https
|
33
|
-
assert eat('https://brighterplanet.com/robots.txt').include?('User-agent')
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_sudo_filesystem
|
37
|
-
f = File.open('test_sudo_filesystem.txt', 'w')
|
38
|
-
f.write "hello world"
|
39
|
-
f.close
|
40
|
-
`sudo chown root #{f.path}`
|
41
|
-
`sudo chmod go-rwx #{f.path}`
|
42
|
-
assert !File.readable?(f.path)
|
43
|
-
assert eat(f.path).include?('hello world')
|
44
|
-
ensure
|
45
|
-
`sudo rm -f #{f.path}`
|
30
|
+
assert eat('https://brighterplanet.com/robots.txt', :timeout => 10).include?('User-agent')
|
46
31
|
end
|
47
32
|
|
48
33
|
def test_openuri_uses_tempfile
|
@@ -54,7 +39,17 @@ class TestEat < Test::Unit::TestCase
|
|
54
39
|
|
55
40
|
def test_eat_doesnt_use_tempfile
|
56
41
|
assert_nothing_raised do
|
57
|
-
eat 'http://do1ircpq72156.cloudfront.net/0.2.47/javascripts/prototype.rails-3.0.3.js'
|
42
|
+
eat 'http://do1ircpq72156.cloudfront.net/0.2.47/javascripts/prototype.rails-3.0.3.js', :timeout => 10
|
58
43
|
end
|
59
44
|
end
|
45
|
+
|
46
|
+
def test_limit_on_local_files
|
47
|
+
assert_equal '# -', eat(__FILE__, :limit => 3)
|
48
|
+
assert_equal '# -*-', eat(__FILE__, :limit => 5)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_limit_on_remote_files
|
52
|
+
assert_equal 'Use', eat(::URI.parse('http://brighterplanet.com/robots.txt'), :timeout => 10, :limit => 3)
|
53
|
+
assert_equal 'User-', eat(::URI.parse('http://brighterplanet.com/robots.txt'), :timeout => 10, :limit => 5)
|
54
|
+
end
|
60
55
|
end
|
metadata
CHANGED
@@ -1,48 +1,37 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: eat
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 7
|
10
|
-
version: 0.0.7
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Seamus Abshere
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-06-27 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: test-unit
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2153835220 !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
33
22
|
type: :development
|
34
|
-
|
35
|
-
|
36
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2153835220
|
25
|
+
description: Lets you open local and remote files by immediately returning their contents
|
26
|
+
as a string.
|
27
|
+
email:
|
37
28
|
- seamus@abshere.net
|
38
29
|
executables: []
|
39
|
-
|
40
30
|
extensions: []
|
41
|
-
|
42
31
|
extra_rdoc_files: []
|
43
|
-
|
44
|
-
files:
|
32
|
+
files:
|
45
33
|
- .gitignore
|
34
|
+
- CHANGELOG
|
46
35
|
- Gemfile
|
47
36
|
- README.rdoc
|
48
37
|
- Rakefile
|
@@ -51,40 +40,30 @@ files:
|
|
51
40
|
- lib/eat/version.rb
|
52
41
|
- test/helper.rb
|
53
42
|
- test/test_eat.rb
|
54
|
-
has_rdoc: true
|
55
43
|
homepage: https://github.com/seamusabshere/eat
|
56
44
|
licenses: []
|
57
|
-
|
58
45
|
post_install_message:
|
59
46
|
rdoc_options: []
|
60
|
-
|
61
|
-
require_paths:
|
47
|
+
require_paths:
|
62
48
|
- lib
|
63
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
50
|
none: false
|
65
|
-
requirements:
|
66
|
-
- -
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
69
|
-
|
70
|
-
- 0
|
71
|
-
version: "0"
|
72
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
56
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
|
78
|
-
segments:
|
79
|
-
- 0
|
80
|
-
version: "0"
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
81
61
|
requirements: []
|
82
|
-
|
83
62
|
rubyforge_project: eat
|
84
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.8.5
|
85
64
|
signing_key:
|
86
65
|
specification_version: 3
|
87
66
|
summary: A more trustworthy open-uri for use with RSS feeds, config scripts, etc.
|
88
|
-
test_files:
|
67
|
+
test_files:
|
89
68
|
- test/helper.rb
|
90
69
|
- test/test_eat.rb
|