rufus-verbs 0.7 → 0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.txt +16 -1
- data/lib/rufus/verbs.rb +32 -0
- data/lib/rufus/verbs/endpoint.rb +8 -7
- data/lib/rufus/verbs/version.rb +1 -1
- data/test/bm.rb +72 -0
- data/test/fopen_test.rb +53 -0
- data/test/test.rb +3 -0
- data/test/uri_test.rb +33 -0
- metadata +5 -2
data/README.txt
CHANGED
@@ -26,13 +26,13 @@ currently :
|
|
26
26
|
* cookie-aware (if :cookies option is explicitely set to true)
|
27
27
|
* http digest authentication (rfc 2617) (auth ok, auth-int not tested)
|
28
28
|
* query parameters are automatically escaped (disable with :no_escape => true)
|
29
|
+
* fopen(uri) method (feels like open-uri's open method, but provides all the previously mentioned features)
|
29
30
|
|
30
31
|
maybe later :
|
31
32
|
|
32
33
|
* retry on failure
|
33
34
|
* cache awareness
|
34
35
|
* greediness (automatic parsing for content like JSON or YAML)
|
35
|
-
* head, options
|
36
36
|
* persistent cookie jar
|
37
37
|
|
38
38
|
|
@@ -149,6 +149,21 @@ Digest authentication and basic authentication are available at request or endpo
|
|
149
149
|
"http://server/doc1",
|
150
150
|
:digest_authentication => [ "toto", "secretpass" ])
|
151
151
|
|
152
|
+
The Rufus::Verbs module provides as well a <tt>fopen</tt> method, which mostly feels like the <tt>open</tt> method of open-uri.
|
153
|
+
|
154
|
+
res = fopen "CHANGELOG.txt"
|
155
|
+
|
156
|
+
Rufus::Verbs.fopen "mydir/mypath.txt" do |f|
|
157
|
+
puts f.readlines
|
158
|
+
end
|
159
|
+
|
160
|
+
res = Rufus::Verbs.fopen "http://whatever.nada.ks"
|
161
|
+
|
162
|
+
res = Rufus::Verbs.fopen "http://whatever.nada.ks", :noredir => true
|
163
|
+
|
164
|
+
But it follows redirections (has all the rufus-verbs features). It's provided for when targets are local files or URIs.
|
165
|
+
|
166
|
+
|
152
167
|
The tests may provide good intel as on 'rufus-verbs' usage as well : http://rufus.rubyforge.org/svn/trunk/verbs/test/
|
153
168
|
|
154
169
|
|
data/lib/rufus/verbs.rb
CHANGED
@@ -87,6 +87,38 @@ module Rufus::Verbs
|
|
87
87
|
EndPoint.request :options, args
|
88
88
|
end
|
89
89
|
|
90
|
+
#
|
91
|
+
# Opens a file or a URI (GETs it and return the reply). Will tolerate
|
92
|
+
# a HTTP[S] URI or a file path.
|
93
|
+
#
|
94
|
+
# It is not named open() in order not to collide with File.open and
|
95
|
+
# open-uri's open.
|
96
|
+
#
|
97
|
+
def fopen (uri, *args, &block)
|
98
|
+
|
99
|
+
u = URI.parse uri.to_s
|
100
|
+
|
101
|
+
return File.open(uri.to_s, &block) \
|
102
|
+
if u.scheme == nil
|
103
|
+
|
104
|
+
return File.open(uri.to_s[5..-1], &block) \
|
105
|
+
if u.scheme == 'file'
|
106
|
+
|
107
|
+
if u.scheme == 'http' or u.scheme == 'https'
|
108
|
+
|
109
|
+
r = EndPoint.request(:get, [ uri ] + args) \
|
110
|
+
|
111
|
+
if block
|
112
|
+
block.call r
|
113
|
+
return
|
114
|
+
else
|
115
|
+
return r
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
raise "can't handle scheme '#{u.scheme}' for #{u.to_s}"
|
120
|
+
end
|
121
|
+
|
90
122
|
extend self
|
91
123
|
end
|
92
124
|
|
data/lib/rufus/verbs/endpoint.rb
CHANGED
@@ -212,8 +212,11 @@ module Verbs
|
|
212
212
|
|
213
213
|
args = [ args ] unless args.is_a?(Array)
|
214
214
|
|
215
|
-
opts = args.last
|
216
|
-
|
215
|
+
opts = args.last \
|
216
|
+
if args.last.is_a?(Hash)
|
217
|
+
|
218
|
+
opts[:uri] = args.first \
|
219
|
+
if args.first.is_a?(String) or args.first.is_a?(URI)
|
217
220
|
|
218
221
|
opts
|
219
222
|
end
|
@@ -297,9 +300,9 @@ module Verbs
|
|
297
300
|
method = :post
|
298
301
|
end
|
299
302
|
|
300
|
-
|
303
|
+
path = compute_path opts
|
301
304
|
|
302
|
-
r = eval("Net::HTTP::#{method.to_s.capitalize}").new
|
305
|
+
r = eval("Net::HTTP::#{method.to_s.capitalize}").new path
|
303
306
|
|
304
307
|
r['User-Agent'] = o(opts, :user_agent)
|
305
308
|
# potentially overriden by opts[:headers]
|
@@ -420,9 +423,7 @@ module Verbs
|
|
420
423
|
r = o(opts, [ :res, :resource ])
|
421
424
|
i = o(opts, :id)
|
422
425
|
|
423
|
-
|
424
|
-
|
425
|
-
path = p
|
426
|
+
path = o(opts, :path)
|
426
427
|
|
427
428
|
if b or r or i
|
428
429
|
path = ""
|
data/lib/rufus/verbs/version.rb
CHANGED
data/test/bm.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# The original version of this benchmark can be found at :
|
4
|
+
#
|
5
|
+
# http://m.onkey.org/2008/2/2/tidbits-from-my-crap
|
6
|
+
#
|
7
|
+
# By running it, you'll simply learn that rufus-verbs perform like
|
8
|
+
# net/http which it wraps.
|
9
|
+
# Seems a bit better than open-uri though.
|
10
|
+
#
|
11
|
+
# Mon Feb 18 09:17:02 JST 2008
|
12
|
+
#
|
13
|
+
|
14
|
+
#['rubygems', 'benchmark', 'eventmachine', 'net/http', 'open-uri', 'rfuzz/session'].each {|lib| require lib }
|
15
|
+
['rubygems', 'benchmark', 'net/http', 'open-uri', 'rufus/verbs' ].each {|lib| require lib }
|
16
|
+
|
17
|
+
server = 'tiramisu'
|
18
|
+
port = 80
|
19
|
+
request_uri = "http://#{server}:#{port}/"
|
20
|
+
|
21
|
+
def run(name, x)
|
22
|
+
x.report(name) do
|
23
|
+
100.times do
|
24
|
+
yield
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
uri = URI.parse(request_uri)
|
30
|
+
puts Net::HTTP.get(uri)
|
31
|
+
|
32
|
+
#rfuzz = RFuzz::HttpClient.new(server, port)
|
33
|
+
#puts rfuzz.get('/').http_body
|
34
|
+
|
35
|
+
puts open(request_uri).read
|
36
|
+
|
37
|
+
#puts Rufus::Verbs.get(uri)
|
38
|
+
puts Rufus::Verbs.get(request_uri)
|
39
|
+
|
40
|
+
#EM.epoll
|
41
|
+
#http = nil
|
42
|
+
#EM.run do
|
43
|
+
# http = EM::Protocols::HttpClient2.connect(server, port).get("/")
|
44
|
+
# http.callback { EM.stop }
|
45
|
+
#end
|
46
|
+
#puts http.content
|
47
|
+
#EM.run { EM::Protocols::HttpClient2.connect(server, port).get("/").callback { EM.stop } }
|
48
|
+
|
49
|
+
Benchmark.bm do |x|
|
50
|
+
|
51
|
+
run("Ruby Net::HTTP ", x) do
|
52
|
+
Net::HTTP.get(uri)
|
53
|
+
end
|
54
|
+
|
55
|
+
run("Open URI ", x) do
|
56
|
+
open(request_uri).read
|
57
|
+
end
|
58
|
+
|
59
|
+
run("Rufus-verbs ", x) do
|
60
|
+
#Rufus::Verbs.get(uri)
|
61
|
+
Rufus::Verbs.get(request_uri)
|
62
|
+
end
|
63
|
+
|
64
|
+
#run("RFuzz ", x) do
|
65
|
+
# rfuzz.get('/').http_body
|
66
|
+
#end
|
67
|
+
|
68
|
+
#run("Event Machine ", x) do
|
69
|
+
# EM.run { EM::Protocols::HttpClient2.connect(server, port).get("/").callback { EM.stop } }
|
70
|
+
#end
|
71
|
+
|
72
|
+
end
|
data/test/fopen_test.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Testing rufus-verbs
|
4
|
+
#
|
5
|
+
# jmettraux@gmail.com
|
6
|
+
#
|
7
|
+
# Mon Feb 18 13:00:36 JST 2008
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
require 'testbase'
|
12
|
+
|
13
|
+
require 'rufus/verbs'
|
14
|
+
|
15
|
+
|
16
|
+
class UriTest < Test::Unit::TestCase
|
17
|
+
include TestBaseMixin
|
18
|
+
|
19
|
+
include Rufus::Verbs
|
20
|
+
|
21
|
+
def test_0
|
22
|
+
|
23
|
+
uri = "http://localhost:7777/items"
|
24
|
+
|
25
|
+
res = fopen uri
|
26
|
+
assert_equal 200, res.code.to_i
|
27
|
+
assert_equal "{}", res.body.strip
|
28
|
+
|
29
|
+
res = fopen "file:CHANGELOG.txt"
|
30
|
+
assert_kind_of String, res.read
|
31
|
+
|
32
|
+
res = fopen "CHANGELOG.txt"
|
33
|
+
assert_kind_of String, res.read
|
34
|
+
|
35
|
+
res = fopen "http://localhost:7777/things"
|
36
|
+
assert_equal 200, res.code.to_i
|
37
|
+
assert_equal "{}", res.body.strip
|
38
|
+
#
|
39
|
+
# it follows redirections :)
|
40
|
+
|
41
|
+
res = fopen "http://localhost:7777/things", :noredir => true
|
42
|
+
assert_equal 303, res.code.to_i
|
43
|
+
|
44
|
+
fopen "CHANGELOG.txt" do |f|
|
45
|
+
assert_kind_of String, f.read
|
46
|
+
end
|
47
|
+
|
48
|
+
fopen "http://localhost:7777/things" do |res|
|
49
|
+
assert_equal 200, res.code.to_i
|
50
|
+
assert_equal "{}", res.body.strip
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/test/test.rb
CHANGED
data/test/uri_test.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Testing rufus-verbs
|
4
|
+
#
|
5
|
+
# jmettraux@gmail.com
|
6
|
+
#
|
7
|
+
# Mon Feb 18 11:05:07 JST 2008
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'test/unit'
|
11
|
+
require 'testbase'
|
12
|
+
|
13
|
+
require 'rufus/verbs'
|
14
|
+
|
15
|
+
|
16
|
+
class UriTest < Test::Unit::TestCase
|
17
|
+
include TestBaseMixin
|
18
|
+
|
19
|
+
include Rufus::Verbs
|
20
|
+
|
21
|
+
def test_0
|
22
|
+
|
23
|
+
uri = URI.parse "http://localhost:7777/items"
|
24
|
+
|
25
|
+
res = get :uri => uri
|
26
|
+
assert_equal 200, res.code.to_i
|
27
|
+
assert_equal "{}", res.body.strip
|
28
|
+
|
29
|
+
res = get uri
|
30
|
+
assert_equal 200, res.code.to_i
|
31
|
+
assert_equal "{}", res.body.strip
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufus-verbs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.8"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Mettraux
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-02-
|
12
|
+
date: 2008-02-18 00:00:00 +09:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -41,11 +41,13 @@ files:
|
|
41
41
|
- test/auth0_test.rb
|
42
42
|
- test/auth1_test.rb
|
43
43
|
- test/block_test.rb
|
44
|
+
- test/bm.rb
|
44
45
|
- test/conditional_test.rb
|
45
46
|
- test/cookie0_test.rb
|
46
47
|
- test/cookie1_test.rb
|
47
48
|
- test/dryrun_test.rb
|
48
49
|
- test/escape_test.rb
|
50
|
+
- test/fopen_test.rb
|
49
51
|
- test/https_test.rb
|
50
52
|
- test/iconditional_test.rb
|
51
53
|
- test/items.rb
|
@@ -55,6 +57,7 @@ files:
|
|
55
57
|
- test/test.htdigest
|
56
58
|
- test/test.rb
|
57
59
|
- test/testbase.rb
|
60
|
+
- test/uri_test.rb
|
58
61
|
- README.txt
|
59
62
|
has_rdoc: true
|
60
63
|
homepage: http://rufus.rubyforge.org/rufus-verbs
|