rufus-verbs 0.8 → 0.9

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 CHANGED
@@ -163,6 +163,8 @@ The Rufus::Verbs module provides as well a <tt>fopen</tt> method, which mostly f
163
163
 
164
164
  But it follows redirections (has all the rufus-verbs features). It's provided for when targets are local files or URIs.
165
165
 
166
+ This fopen() method makes sure to return an object that has a read() method (like a File instance has).
167
+
166
168
 
167
169
  The tests may provide good intel as on 'rufus-verbs' usage as well : http://rufus.rubyforge.org/svn/trunk/verbs/test/
168
170
 
@@ -295,6 +297,13 @@ the URI for the request (or the endpoint)
295
297
  * <b>:user_agent</b> (string, RE)
296
298
  for setting a custom 'User-Agent' HTTP header
297
299
 
300
+ * <b>:v</b> (string/boolean/IO, RE)
301
+ the short version of :verbose
302
+
303
+ * <b>:verbose</b> (string/boolean/IO, RE)
304
+ can be set to true or to an IO instance. If set to true, messages will be directed to STDOUT. Can be set to any IO instance.
305
+ Will produces verbose messages similar to those of the fine cURL utility.
306
+
298
307
 
299
308
  == dependencies
300
309
 
@@ -315,7 +324,9 @@ On the Rufus-Ruby list[http://groups.google.com/group/rufus-ruby] :
315
324
 
316
325
  == source
317
326
 
318
- svn checkout http://rufus.rubyforge.org/svn/trunk/verbs
327
+ http://github.com/jmettraux/rufus-verbs
328
+
329
+ git clone git://github.com/jmettraux/rufus-verbs.git
319
330
 
320
331
 
321
332
  == author
@@ -324,6 +335,11 @@ John Mettraux, jmettraux@gmail.com,
324
335
  http://jmettraux.wordpress.com
325
336
 
326
337
 
338
+ == the rest of Rufus
339
+
340
+ http://rufus.rubyforge.org
341
+
342
+
327
343
  == license
328
344
 
329
345
  MIT
@@ -96,6 +96,8 @@ module Rufus::Verbs
96
96
  #
97
97
  def fopen (uri, *args, &block)
98
98
 
99
+ # should I really care about blocks here ? ...
100
+
99
101
  u = URI.parse uri.to_s
100
102
 
101
103
  return File.open(uri.to_s, &block) \
@@ -111,9 +113,15 @@ module Rufus::Verbs
111
113
  if block
112
114
  block.call r
113
115
  return
114
- else
115
- return r
116
116
  end
117
+
118
+ class << r
119
+ def read
120
+ self.body
121
+ end
122
+ end unless r.respond_to?(:read)
123
+
124
+ return r
117
125
  end
118
126
 
119
127
  raise "can't handle scheme '#{u.scheme}' for #{u.to_s}"
@@ -48,6 +48,11 @@ module Verbs
48
48
  #
49
49
  module CookieMixin
50
50
 
51
+ #
52
+ # making the cookie jar available
53
+ #
54
+ attr_reader :cookies
55
+
51
56
  protected
52
57
 
53
58
  #
@@ -103,7 +108,7 @@ module Verbs
103
108
  path = opts[:path]
104
109
  cpath = c.path || "/"
105
110
 
106
- next unless cookie_acceptable?(opts, c)
111
+ next unless cookie_acceptable?(opts, response, c)
107
112
 
108
113
  domain = c.domain || host
109
114
 
@@ -119,7 +124,7 @@ module Verbs
119
124
  # Checks if the cookie is acceptable in the context of
120
125
  # the request that sent it.
121
126
  #
122
- def cookie_acceptable? (opts, cookie)
127
+ def cookie_acceptable? (opts, response, cookie)
123
128
 
124
129
  # reject if :
125
130
  #
@@ -144,7 +149,9 @@ module Verbs
144
149
  return false if d != cdomain
145
150
  end
146
151
 
147
- path = opts[:path]
152
+ #path = opts[:path]
153
+ path = response.request.path
154
+
148
155
  cpath = cookie.path || "/"
149
156
 
150
157
  return false if path[0..cpath.length-1] != cpath
@@ -40,6 +40,7 @@ require 'zlib'
40
40
  require 'rufus/verbs/version'
41
41
  require 'rufus/verbs/cookies'
42
42
  require 'rufus/verbs/digest'
43
+ require 'rufus/verbs/verbose'
43
44
 
44
45
 
45
46
  module Rufus
@@ -69,6 +70,7 @@ module Verbs
69
70
 
70
71
  include CookieMixin
71
72
  include DigestAuthMixin
73
+ include VerboseMixin
72
74
 
73
75
  #
74
76
  # The endpoint initialization opts (Hash instance)
@@ -163,12 +165,16 @@ module Verbs
163
165
  # if the :cookies option is disabled (the default)
164
166
  # will have no effect
165
167
 
168
+ vlog_request opts, req
169
+
166
170
  return req if o(opts, :dry_run) == true
167
171
 
168
172
  # trigger request
169
173
 
170
174
  http = prepare_http opts
171
175
 
176
+ vlog_http opts, http
177
+
172
178
  res = nil
173
179
 
174
180
  http.start do
@@ -177,6 +183,13 @@ module Verbs
177
183
 
178
184
  # handle response
179
185
 
186
+ class << res
187
+ attr_accessor :request
188
+ end
189
+ res.request = req
190
+
191
+ vlog_response opts, res
192
+
180
193
  register_cookies res, opts
181
194
  # if the :cookies option is disabled (the default)
182
195
  # will have no effect
@@ -246,7 +259,7 @@ module Verbs
246
259
  opts[:host],
247
260
  opts[:port] || 80,
248
261
  opts[:path] || '/',
249
- opts[:query] || opts[:params] || {} ]
262
+ opts[:query] || opts[:params] ]
250
263
 
251
264
  elsif u
252
265
 
@@ -265,7 +278,12 @@ module Verbs
265
278
  opts[:host] = r[1] || @opts[:host]
266
279
  opts[:port] = r[2] || @opts[:port]
267
280
  opts[:path] = r[3] || @opts[:path]
268
- opts[:query] = r[4] || @opts[:query] || @opts[:params]
281
+
282
+ opts[:query] =
283
+ r[4] ||
284
+ opts[:params] || opts[:query] ||
285
+ @opts[:query] || @opts[:params] ||
286
+ {}
269
287
 
270
288
  opts.delete :path if opts[:path] == ""
271
289
 
@@ -432,7 +450,9 @@ module Verbs
432
450
  path += "/#{i}" if i
433
451
  end
434
452
 
435
- query = opts[:query]
453
+ path = path[1..-1] if path[0..1] == '//'
454
+
455
+ query = opts[:query] || opts[:params]
436
456
 
437
457
  return path if not query or query.size < 1
438
458
 
@@ -0,0 +1,114 @@
1
+ #
2
+ #--
3
+ # Copyright (c) 2008, John Mettraux, jmettraux@gmail.com
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+ #
23
+ # (MIT license)
24
+ #++
25
+ #
26
+
27
+ #
28
+ # John Mettraux
29
+ #
30
+ # Made in Japan
31
+ #
32
+ # 2008/03/31
33
+ #
34
+
35
+ module Rufus
36
+ module Verbs
37
+
38
+ #
39
+ # Methods for the verbose mode
40
+ #
41
+ module VerboseMixin
42
+
43
+ protected
44
+
45
+ #
46
+ # logs a unique message to the verbose channel (if any).
47
+ #
48
+ def vlog (opts, msg)
49
+
50
+ channel = get_channel(opts) or return
51
+
52
+ channel << msg
53
+ end
54
+
55
+ #
56
+ # logs the outgoing request
57
+ #
58
+ def vlog_request (opts, req)
59
+
60
+ channel = get_channel(opts) or return
61
+
62
+ channel << "> #{req.method} #{req.path}\n"
63
+
64
+ req.each do |k, v|
65
+ channel << "> #{k}: #{v}\n"
66
+ end
67
+
68
+ channel << ">\n"
69
+ end
70
+
71
+ def vlog_http (opts, http)
72
+
73
+ channel = get_channel(opts) or return
74
+
75
+ channel << "* #{http.address}:#{http.port}\n"
76
+ channel << "*\n"
77
+ end
78
+
79
+ #
80
+ # logs the incoming response
81
+ #
82
+ def vlog_response (opts, res)
83
+
84
+ channel = get_channel(opts) or return
85
+
86
+ channel << "< #{res.code} #{res.message}\n"
87
+ channel << "<\n"
88
+
89
+ res.each do |k, v|
90
+ channel << "< #{k}: #{v}\n"
91
+ end
92
+
93
+ channel << "<\n"
94
+ end
95
+
96
+ private
97
+
98
+ def get_channel (opts)
99
+
100
+ v = o(opts, [ :verbose, :v ])
101
+
102
+ return nil if (not v) or (v.to_s == 'false')
103
+
104
+ v = $stdout if v.to_s == 'true'
105
+
106
+ return nil unless v.is_a?(IO)
107
+
108
+ v
109
+ end
110
+ end
111
+
112
+ end
113
+ end
114
+
@@ -39,7 +39,7 @@ module Verbs
39
39
  #
40
40
  # The version of this rufus-verbs [gem]
41
41
  #
42
- VERSION = '0.8'
42
+ VERSION = '0.9'
43
43
  end
44
44
  end
45
45
 
@@ -65,32 +65,37 @@ class Cookie0Test < Test::Unit::TestCase
65
65
 
66
66
  opts = { :host => 'rufus.rubyforge.org', :path => '/' }
67
67
  c = TestCookie.new '.rubyforge.org', '/'
68
- assert cookie_acceptable?(opts, c)
68
+ r = TestResponse.new opts
69
+ assert cookie_acceptable?(opts, r, c)
69
70
 
70
71
  # * The value for the Domain attribute contains no embedded dots
71
72
  # or does not start with a dot.
72
73
 
73
74
  opts = { :host => 'rufus.rubyforge.org', :path => '/' }
74
75
  c = TestCookie.new 'rufus.rubyforge.org', '/'
75
- assert ! cookie_acceptable?(opts, c)
76
+ r = TestResponse.new opts
77
+ assert ! cookie_acceptable?(opts, r, c)
76
78
 
77
79
  opts = { :host => 'rufus.rubyforge.org', :path => '/' }
78
80
  c = TestCookie.new 'org', '/'
79
- assert ! cookie_acceptable?(opts, c)
81
+ r = TestResponse.new opts
82
+ assert ! cookie_acceptable?(opts, r, c)
80
83
 
81
84
  # * The value for the Path attribute is not a prefix of the
82
85
  # request-URI.
83
86
 
84
87
  opts = { :host => 'rufus.rubyforge.org', :path => '/this' }
85
88
  c = TestCookie.new '.rubyforge.org', '/that'
86
- assert ! cookie_acceptable?(opts, c)
89
+ r = TestResponse.new opts
90
+ assert ! cookie_acceptable?(opts, r, c)
87
91
 
88
92
  # * The value for the request-host does not domain-match the
89
93
  # Domain attribute.
90
94
 
91
95
  opts = { :host => 'rufus.rubyforg.org', :path => '/' }
92
96
  c = TestCookie.new '.rubyforge.org', '/'
93
- assert ! cookie_acceptable?(opts, c)
97
+ r = TestResponse.new opts
98
+ assert ! cookie_acceptable?(opts, r, c)
94
99
 
95
100
  # * The request-host is a FQDN (not IP address) and has the form
96
101
  # HD, where D is the value of the Domain attribute, and H is a
@@ -119,4 +124,22 @@ class Cookie0Test < Test::Unit::TestCase
119
124
  @name = name
120
125
  end
121
126
  end
127
+
128
+ class TestResponse
129
+
130
+ def initialize (opts)
131
+
132
+ @path = opts[:path]
133
+ end
134
+
135
+ def request
136
+
137
+ r = Object.new
138
+ class << r
139
+ attr_accessor :path
140
+ end
141
+ r.path = @path
142
+ r
143
+ end
144
+ end
122
145
  end
@@ -26,6 +26,13 @@ class DryRunTest < Test::Unit::TestCase
26
26
 
27
27
  assert_equal "/items/1?a=A&b=B", req.path
28
28
 
29
+ req = post(
30
+ :dry_run => true,
31
+ :uri => "http://localhost:7777/items/1",
32
+ :params => { "a" => "A", :b => :B })
33
+
34
+ assert_equal "/items/1?a=A&b=B", req.path
35
+
29
36
  req = put(
30
37
  :dry_run => true,
31
38
  :uri => "http://localhost:7777/items/1",
@@ -39,4 +46,25 @@ class DryRunTest < Test::Unit::TestCase
39
46
  assert_equal "/items/1?a=A", req.path
40
47
  assert_equal "toto", req.body
41
48
  end
49
+
50
+ def test_1
51
+
52
+ ep = Rufus::Verbs::EndPoint.new(
53
+ :host => 'localhost',
54
+ :resource => 'whatever')
55
+
56
+ req = ep.get(
57
+ :dry_run => true,
58
+ :resource => 'other',
59
+ :params => { 'a' => 'A', 'b' => 'B' })
60
+
61
+ assert_equal "/other?a=A&b=B", req.path
62
+
63
+ req = ep.post(
64
+ :dry_run => true,
65
+ :resource => 'other',
66
+ :query => { 'a' => 'A', 'b' => 'B' })
67
+
68
+ assert_equal "/other?a=A&b=B", req.path
69
+ end
42
70
  end
@@ -50,4 +50,11 @@ class UriTest < Test::Unit::TestCase
50
50
  assert_equal "{}", res.body.strip
51
51
  end
52
52
  end
53
+
54
+ def test_1
55
+
56
+ assert_kind_of String, fopen("CHANGELOG.txt").read
57
+ assert_kind_of String, fopen("file:CHANGELOG.txt").read
58
+ assert_kind_of String, fopen("http://localhost:7777/items").read
59
+ end
53
60
  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.8"
4
+ version: "0.9"
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-18 00:00:00 +09:00
12
+ date: 2008-04-10 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -36,6 +36,7 @@ files:
36
36
  - lib/rufus/verbs/cookies.rb
37
37
  - lib/rufus/verbs/digest.rb
38
38
  - lib/rufus/verbs/endpoint.rb
39
+ - lib/rufus/verbs/verbose.rb
39
40
  - lib/rufus/verbs/version.rb
40
41
  - lib/rufus/verbs.rb
41
42
  - test/auth0_test.rb
@@ -81,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
82
  requirements:
82
83
  - rufus-lru
83
84
  rubyforge_project:
84
- rubygems_version: 0.9.5
85
+ rubygems_version: 1.1.0
85
86
  signing_key:
86
87
  specification_version: 2
87
88
  summary: GET, POST, PUT, DELETE, with something around