rufus-jig 0.1.14 → 0.1.15

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.txt CHANGED
@@ -2,6 +2,13 @@
2
2
  = rufus-jig CHANGELOG.txt
3
3
 
4
4
 
5
+ == rufus-jig - 0.1.15 not yet released
6
+
7
+ - unified :timeout => sec option and Rufus::Jig::TimeoutError
8
+ - fixed small issue with em-http-request and '/' path
9
+ - Ruote::Couch.new now passes options when doing Rufus::Jig::Http
10
+
11
+
5
12
  == rufus-jig - 0.1.14 released 2010/02/25
6
13
 
7
14
  - temporary fallback to net/http for Couch#attach and patron
data/Rakefile CHANGED
@@ -45,6 +45,7 @@ Jeweler::Tasks.new do |gem|
45
45
  gem.add_development_dependency 'yard'
46
46
  gem.add_development_dependency 'jeweler'
47
47
  gem.add_development_dependency 'patron'
48
+ gem.add_development_dependency 'em-http-request'
48
49
 
49
50
  # gemspec spec : http://www.rubygems.org/read/chapter/20
50
51
  end
data/TODO.txt CHANGED
@@ -11,18 +11,17 @@
11
11
  [x] couch : attachments
12
12
  [o] put_doc(h) where _id is set ?
13
13
  [o] :update_rev => true...
14
+ [o] long polling/continuous change (patron ?)
15
+ [x] don't abuse exceptions ! (especially with couch)
16
+ [o] couch.attach
17
+ [o] couch.detach ?
14
18
 
15
- [ ] long polling/continuous change (patron ?)
16
19
  [ ] HEAD
17
-
18
20
  [ ] redirections ? (Patron seem to understand them)
19
-
20
- [ ] don't abuse exceptions ! (especially with couch)
21
-
22
21
  [ ] patron : http can steal each other's patron session (inside the same thread)
23
-
24
22
  [ ] why not a :force_json ?
25
23
 
26
- [ ] couch.attach
27
- [ ] couch.detach ?
24
+ [ ] make HttpCore detect do_request
25
+
26
+ [ ] timeout per request ?
28
27
 
@@ -0,0 +1,128 @@
1
+ #--
2
+ # Copyright (c) 2009-2010, Kenneth Kalmer and John Mettraux.
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in South Africa and Japan.
23
+ #++
24
+
25
+
26
+ class Rufus::Jig::HttpResponse
27
+
28
+ def initialize( em_client )
29
+
30
+ @original = [ em_client, em_client.response ]
31
+
32
+ @status = em_client.response_header.status
33
+ @headers = response_headers( em_client.response_header )
34
+ @body = em_client.response
35
+ end
36
+
37
+ protected
38
+
39
+ def response_headers( hash )
40
+
41
+ hash.inject({}) do |headers, ( key, value )|
42
+ key = key.downcase.split('_').map { |c| c.capitalize }.join( '-' )
43
+ headers[ key ] = value
44
+ headers
45
+ end
46
+ end
47
+ end
48
+
49
+ class Rufus::Jig::Http < Rufus::Jig::HttpCore
50
+
51
+ def initialize ( host, port, opts={} )
52
+
53
+ super( host, port, opts )
54
+
55
+ @options[:user_agent] ||= "#{self.class} #{Rufus::Jig::VERSION} (em)"
56
+ end
57
+
58
+ def variant
59
+ :em
60
+ end
61
+
62
+ protected
63
+
64
+ def do_request ( method, path, data, opts )
65
+
66
+ args = {}
67
+
68
+ args[:head] = request_headers( opts )
69
+ args[:body] = data if data
70
+
71
+ if to = (opts[:timeout] || @options[:timeout])
72
+ to = to.to_f
73
+ args[:timeout] = (to < 1.0) ? (3 * 24 * 3600).to_f : to
74
+ else
75
+ args[:timeout] = 5.0 # like Patron
76
+ end
77
+
78
+ em_response( em_request( path ).send( method, args ) )
79
+ end
80
+
81
+ def em_request( uri = '/' )
82
+ uri = URI.parse( uri )
83
+ uri = URI::HTTP.build(
84
+ :host => ( uri.host || @host ),
85
+ :port => ( uri.port || @port ),
86
+ :path => uri.path,
87
+ :query => uri.query
88
+ )
89
+
90
+ EventMachine::HttpRequest.new( uri.to_s )
91
+ end
92
+
93
+ def em_response( em_client )
94
+ th = Thread.current
95
+
96
+ timedout = false
97
+
98
+ em_client.errback {
99
+
100
+ #th.raise( Rufus::Jig::TimeoutError.new )
101
+ # works with ruby < 1.9.x
102
+ th.wakeup
103
+ }
104
+
105
+ em_client.callback {
106
+ th.wakeup
107
+ }
108
+
109
+ Thread.stop
110
+
111
+ # after the wake up...
112
+
113
+ raise Rufus::Jig::TimeoutError if em_client.response_header.status == 0
114
+
115
+ Rufus::Jig::HttpResponse.new( em_client )
116
+ end
117
+
118
+ def request_headers( options )
119
+ headers = { 'user-agent' => @options[:user_agent] }
120
+
121
+ %w[ Accept If-None-Match Content-Type ].each do |k|
122
+ headers[k] = options[k] if options.has_key?( k )
123
+ end
124
+
125
+ headers
126
+ end
127
+ end
128
+
@@ -0,0 +1,98 @@
1
+ #--
2
+ # Copyright (c) 2009-2010, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+ require 'thread'
26
+ require 'net/http'
27
+
28
+
29
+ class Rufus::Jig::HttpResponse
30
+
31
+ def initialize (nh_res)
32
+
33
+ @original = nh_res
34
+ @status = nh_res.code.to_i
35
+ @body = nh_res.body
36
+ @headers = {}
37
+ nh_res.each { |k, v|
38
+ @headers[k.split('-').collect { |s| s.capitalize }.join('-')] = v
39
+ }
40
+ end
41
+ end
42
+
43
+ class Rufus::Jig::Http < Rufus::Jig::HttpCore
44
+
45
+ def initialize (host, port, opts={})
46
+
47
+ super(host, port, opts)
48
+
49
+ @options[:user_agent] ||= "#{self.class} #{Rufus::Jig::VERSION} (net/http)"
50
+
51
+ #@mutex = Mutex.new
52
+ end
53
+
54
+ def variant
55
+ :net
56
+ end
57
+
58
+ protected
59
+
60
+ def get_http (opts)
61
+
62
+ http = Net::HTTP.new(@host, @port)
63
+
64
+ http.open_timeout = 5
65
+ # connection timeout
66
+
67
+ if to = (opts[:timeout] || @options[:timeout])
68
+ to = to.to_i
69
+ http.read_timeout = (to < 1) ? nil : to
70
+ else
71
+ http.read_timeout = 5 # like Patron
72
+ end
73
+
74
+ http
75
+ end
76
+
77
+ def do_request (method, path, data, opts)
78
+
79
+ #@mutex.synchronize do
80
+
81
+ path = '/' if path == ''
82
+
83
+ req = eval("Net::HTTP::#{method.to_s.capitalize}").new(path)
84
+
85
+ req['User-Agent'] = @options[:user_agent]
86
+ opts.each { |k, v| req[k] = v if k.is_a?(String) }
87
+
88
+ req.body = data ? data : ''
89
+
90
+ begin
91
+ Rufus::Jig::HttpResponse.new(get_http(opts).start { |h| h.request(req) })
92
+ rescue Timeout::Error => te
93
+ raise Rufus::Jig::TimeoutError
94
+ end
95
+ #end
96
+ end
97
+ end
98
+
@@ -0,0 +1,110 @@
1
+ #--
2
+ # Copyright (c) 2009-2010, John Mettraux, jmettraux@gmail.com
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ # of this software and associated documentation files (the "Software"), to deal
6
+ # in the Software without restriction, including without limitation the rights
7
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ # copies of the Software, and to permit persons to whom the Software is
9
+ # furnished to do so, subject to the following conditions:
10
+ #
11
+ # The above copyright notice and this permission notice shall be included in
12
+ # all copies or substantial portions of the Software.
13
+ #
14
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
+ # THE SOFTWARE.
21
+ #
22
+ # Made in Japan.
23
+ #++
24
+
25
+
26
+ class Rufus::Jig::HttpResponse
27
+
28
+ def initialize (patron_res)
29
+
30
+ @original = patron_res
31
+ @status = patron_res.status
32
+ @headers = patron_res.headers
33
+ @body = patron_res.body
34
+ end
35
+ end
36
+
37
+ class Rufus::Jig::Http < Rufus::Jig::HttpCore
38
+
39
+ def initialize (host, port, opts={})
40
+
41
+ super(host, port, opts)
42
+ end
43
+
44
+ def close
45
+
46
+ # it's not really closing, it's rather making sure the patron
47
+ # session can get collected as garbage
48
+
49
+ Thread.current[key] = nil
50
+ end
51
+
52
+ def variant
53
+ :patron
54
+ end
55
+
56
+ protected
57
+
58
+ def key
59
+ self.object_id.to_s
60
+ end
61
+
62
+ # One patron session per thread
63
+ #
64
+ def get_patron (opts)
65
+
66
+ to = (opts[:timeout] || @options[:timeout])
67
+ to = to.to_i if to
68
+ to = nil if to && to < 1
69
+
70
+ k = key
71
+
72
+ patron = Thread.current[k]
73
+
74
+ return patron if patron && patron.timeout == to
75
+
76
+ if patron.nil?
77
+ patron = Patron::Session.new
78
+ patron.base_url = "#{@host}:#{@port}"
79
+ end
80
+
81
+ #patron.connect_timeout = 1
82
+ # connection timeout defaults to 1 second
83
+ patron.timeout = to
84
+
85
+ patron.headers['User-Agent'] =
86
+ @options[:user_agent] ||
87
+ [
88
+ self.class, Rufus::Jig::VERSION, Thread.current.object_id, '(patron)'
89
+ ].join(' ')
90
+
91
+ Thread.current[k] = patron
92
+ end
93
+
94
+ def do_request (method, path, data, opts)
95
+
96
+ opts['Expect'] = '' if (method == :put) && ( ! @options[:expect])
97
+
98
+ args = case method
99
+ when :post, :put then [ path, data, opts ]
100
+ else [ path, opts ]
101
+ end
102
+
103
+ begin
104
+ get_patron(opts).send(method, *args)
105
+ rescue Patron::TimeoutError => te
106
+ raise Rufus::Jig::TimeoutError
107
+ end
108
+ end
109
+ end
110
+
@@ -72,13 +72,11 @@ module Rufus::Jig
72
72
  nil
73
73
  end
74
74
 
75
- def get (doc_or_path)
75
+ def get (doc_or_path, opts={})
76
76
 
77
77
  path = doc_or_path.is_a?(Hash) ? doc_or_path['_id'] : doc_or_path
78
78
  path = adjust(path)
79
79
 
80
- opts = {}
81
-
82
80
  if et = etag(path)
83
81
  opts[:etag] = et
84
82
  end
@@ -52,6 +52,16 @@ module Rufus::Jig
52
52
  end
53
53
  end
54
54
 
55
+ # A common error for all adapters in case of timeout.
56
+ #
57
+ class TimeoutError < RuntimeError
58
+
59
+ def initialize (message=nil)
60
+
61
+ super(message || 'timed out')
62
+ end
63
+ end
64
+
55
65
  class HttpResponse
56
66
 
57
67
  attr_reader :status, :headers, :body
@@ -86,7 +96,8 @@ module Rufus::Jig
86
96
 
87
97
  @host = host
88
98
  @port = port
89
- @options = opts
99
+
100
+ @options = opts.dup
90
101
 
91
102
  @cache = LruHash.new((opts[:cache_size] || 35).to_i)
92
103
 
@@ -153,13 +164,15 @@ module Rufus::Jig
153
164
  path = add_prefix(path, opts)
154
165
  path = add_params(path, opts)
155
166
 
167
+ path = '/' if path == ''
168
+
156
169
  cached = from_cache(path, opts)
157
170
  opts.delete(:etag) if not cached
158
171
 
159
172
  opts = rehash_options(opts)
160
173
  data = repack_data(data, opts)
161
174
 
162
- r = send("do_#{method}", path, data, opts)
175
+ r = do_request(method, path, data, opts)
163
176
 
164
177
  @last_response = r
165
178
 
@@ -291,273 +304,15 @@ end
291
304
 
292
305
  if defined?(Patron) # gem install patron
293
306
 
294
- class Rufus::Jig::HttpResponse
295
-
296
- def initialize (patron_res)
297
-
298
- @original = patron_res
299
- @status = patron_res.status
300
- @headers = patron_res.headers
301
- @body = patron_res.body
302
- end
303
- end
304
-
305
- class Rufus::Jig::Http < Rufus::Jig::HttpCore
306
-
307
- def initialize (host, port, opts={})
308
-
309
- super(host, port, opts)
310
- end
311
-
312
- def close
313
-
314
- # it's not really closing, it's rather making sure the patron
315
- # session can get collected as garbage
316
-
317
- Thread.current[key] = nil
318
- end
319
-
320
- def variant
321
- :patron
322
- end
323
-
324
- protected
325
-
326
- def key
327
- self.object_id.to_s
328
- end
329
-
330
- # One patron session per thread
331
- #
332
- def get_patron
333
-
334
- k = key
335
-
336
- patron = Thread.current[k]
337
-
338
- return patron if patron
339
-
340
- patron = Patron::Session.new
341
- patron.base_url = "#{@host}:#{@port}"
342
-
343
- patron.headers['User-Agent'] =
344
- @options[:user_agent] ||
345
- [
346
- self.class, Rufus::Jig::VERSION, Thread.current.object_id, '(patron)'
347
- ].join(' ')
348
-
349
- Thread.current[k] = patron
350
- end
351
-
352
- def do_get (path, data, opts)
353
-
354
- get_patron.get(path, opts)
355
- end
356
-
357
- def do_post (path, data, opts)
358
-
359
- get_patron.post(path, data, opts)
360
- end
361
-
362
- def do_put (path, data, opts)
363
-
364
- opts['Expect'] = '' unless @options[:expect]
365
-
366
- get_patron.put(path, data, opts)
367
- end
368
-
369
- def do_delete (path, data, opts)
370
-
371
- get_patron.delete(path, opts)
372
- end
373
- end
374
-
375
- elsif defined?( EventMachine::HttpRequest )
376
-
377
- class Rufus::Jig::HttpResponse
378
-
379
- def initialize( em_client )
380
-
381
- @original = [ em_client, em_client.response ]
382
-
383
- @status = em_client.response_header.status
384
- @headers = response_headers( em_client.response_header )
385
- @body = em_client.response
386
- end
387
-
388
- protected
389
-
390
- def response_headers( hash )
391
-
392
- hash.inject({}) do |headers, (key, value)|
393
- key = key.downcase.split('_').map { |c| c.capitalize }.join('-')
394
- headers[ key ] = value
395
- headers
396
- end
397
- end
398
- end
399
-
400
- class Rufus::Jig::Http < Rufus::Jig::HttpCore
401
-
402
- def initialize (host, port, opts={})
403
-
404
- super(host, port, opts)
405
-
406
- @em_host = host
407
- @em_port = port
408
-
409
- @em_ua = opts[:user_agent] || "#{self.class} #{Rufus::Jig::VERSION} (em)"
410
- end
411
-
412
- def variant
413
- :em
414
- end
415
-
416
- protected
417
-
418
- def do_get( path, data, opts )
419
- http = em_request( path ).get( :head => request_headers(opts) )
420
-
421
- em_response( http )
422
- end
423
-
424
- def do_post( path, data, opts )
425
- http = em_request( path ).post( :body => data, :head => request_headers(opts) )
426
-
427
- em_response( http )
428
- end
429
-
430
- def do_delete( path, data, opts )
431
- http = em_request( path ).delete( :head => request_headers( opts ) )
432
-
433
- em_response( http )
434
- end
435
-
436
- def do_put( path, data, opts )
437
- http = em_request( path ).put( :body => data, :head => request_headers( opts ) )
307
+ require 'rufus/jig/adapters/patron'
438
308
 
439
- em_response( http )
440
- end
441
-
442
- def em_request( uri = '/' )
443
- uri = URI.parse( uri )
444
- uri = URI::HTTP.build(
445
- :host => ( uri.host || @em_host ),
446
- :port => ( uri.port || @em_port ),
447
- :path => uri.path,
448
- :query => uri.query
449
- )
450
-
451
- EventMachine::HttpRequest.new( uri.to_s )
452
- end
309
+ elsif defined?(EventMachine::HttpRequest)
453
310
 
454
- def em_response( em_client )
455
- th = Thread.current
456
-
457
- em_client.callback {
458
- th.wakeup
459
- }
460
-
461
- Thread.stop
462
-
463
- Rufus::Jig::HttpResponse.new( em_client )
464
- end
465
-
466
- def request_headers( options )
467
- headers = { 'user-agent' => @em_ua }
468
-
469
- %w[ Accept If-None-Match Content-Type ].each do |k|
470
- headers[k] = options[k] if options.has_key?( k )
471
- end
472
-
473
- headers
474
- end
475
- end
311
+ require 'rufus/jig/adapters/em'
476
312
 
477
313
  else
478
314
 
479
- require 'thread'
480
- require 'net/http'
481
-
482
- class Rufus::Jig::HttpResponse
483
-
484
- def initialize (nh_res)
485
-
486
- @original = nh_res
487
- @status = nh_res.code.to_i
488
- @body = nh_res.body
489
- @headers = {}
490
- nh_res.each { |k, v|
491
- @headers[k.split('-').collect { |s| s.capitalize }.join('-')] = v
492
- }
493
- end
494
- end
495
-
496
- class Rufus::Jig::Http < Rufus::Jig::HttpCore
497
-
498
- def initialize (host, port, opts={})
499
-
500
- super(host, port, opts)
501
-
502
- @http = Net::HTTP.new(host, port)
503
-
504
- to = opts[:timeout]
505
- if to
506
- to = to.to_i
507
- @http.open_timeout = to
508
- @http.read_timeout = to
509
- end
510
-
511
- @options[:user_agent] =
512
- opts[:user_agent] ||
513
- "#{self.class} #{Rufus::Jig::VERSION} (net/http)"
514
-
515
- @mutex = Mutex.new
516
- end
517
-
518
- def variant
519
- :net
520
- end
521
-
522
- protected
523
-
524
- def do_get (path, data, opts)
525
-
526
- do_request(:get, path, data, opts)
527
- end
528
-
529
- def do_post (path, data, opts)
530
-
531
- do_request(:post, path, data, opts)
532
- end
533
-
534
- def do_put (path, data, opts)
535
-
536
- do_request(:put, path, data, opts)
537
- end
538
-
539
- def do_delete (path, data, opts)
540
-
541
- do_request(:delete, path, data, opts)
542
- end
543
-
544
- def do_request (method, path, data, opts)
545
-
546
- @mutex.synchronize do
547
-
548
- path = '/' if path == ''
549
-
550
- req = eval("Net::HTTP::#{method.to_s.capitalize}").new(path)
551
-
552
- req['User-Agent'] = options[:user_agent]
553
- opts.each { |k, v| req[k] = v if k.is_a?(String) }
554
-
555
- req.body = data ? data : ''
556
-
557
- Rufus::Jig::HttpResponse.new(@http.start { |h| h.request(req) })
558
- end
559
- end
560
- end
315
+ require 'rufus/jig/adapters/net'
561
316
 
562
317
  end
563
318
 
@@ -579,7 +334,7 @@ class Rufus::Jig::Http
579
334
  #
580
335
  def self.extract_http (payload_expected, *args)
581
336
 
582
- http = case args.first
337
+ host, port = case args.first
583
338
 
584
339
  when Rufus::Jig::Http
585
340
  args.shift
@@ -587,10 +342,10 @@ class Rufus::Jig::Http
587
342
  when /^http:\/\//
588
343
  u = URI.parse(args.shift)
589
344
  args.unshift(u.path)
590
- Rufus::Jig::Http.new(u.host, u.port)
345
+ [ u.host, u.port ]
591
346
 
592
347
  else
593
- Rufus::Jig::Http.new(args.shift, args.shift)
348
+ [ args.shift, args.shift ]
594
349
  end
595
350
 
596
351
  path = args.shift
@@ -604,6 +359,10 @@ class Rufus::Jig::Http
604
359
  ArgumentError.new("option Hash expected, not #{opts.inspect}")
605
360
  ) unless opts.is_a?(Hash)
606
361
 
362
+ http = host.is_a?(Rufus::Jig::Http) ?
363
+ host :
364
+ Rufus::Jig::Http.new(host, port, opts)
365
+
607
366
  [ http, path, payload, opts ]
608
367
  end
609
368
  end
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Rufus
3
3
  module Jig
4
- VERSION = '0.1.14'
4
+ VERSION = '0.1.15'
5
5
  end
6
6
  end
7
7
 
data/rufus-jig.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rufus-jig}
8
- s.version = "0.1.14"
8
+ s.version = "0.1.15"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["John Mettraux", "Kenneth Kalmer"]
12
- s.date = %q{2010-02-25}
12
+ s.date = %q{2010-03-12}
13
13
  s.description = %q{
14
14
  Json Interwebs Get.
15
15
 
@@ -32,6 +32,9 @@ Gem::Specification.new do |s|
32
32
  "TODO.txt",
33
33
  "lib/rufus-jig.rb",
34
34
  "lib/rufus/jig.rb",
35
+ "lib/rufus/jig/adapters/em.rb",
36
+ "lib/rufus/jig/adapters/net.rb",
37
+ "lib/rufus/jig/adapters/patron.rb",
35
38
  "lib/rufus/jig/couch.rb",
36
39
  "lib/rufus/jig/http.rb",
37
40
  "lib/rufus/jig/path.rb",
@@ -47,6 +50,8 @@ Gem::Specification.new do |s|
47
50
  "test/ct_4_attachments.rb",
48
51
  "test/server.rb",
49
52
  "test/test.rb",
53
+ "test/to.sh",
54
+ "test/tt_0_get_timeout.rb",
50
55
  "test/tweet.png",
51
56
  "test/ut_0_http_get.rb",
52
57
  "test/ut_1_http_post.rb",
@@ -60,7 +65,7 @@ Gem::Specification.new do |s|
60
65
  s.rdoc_options = ["--charset=UTF-8"]
61
66
  s.require_paths = ["lib"]
62
67
  s.rubyforge_project = %q{rufus}
63
- s.rubygems_version = %q{1.3.5}
68
+ s.rubygems_version = %q{1.3.6}
64
69
  s.summary = %q{An HTTP client, greedy with JSON content, GETting conditionally.}
65
70
  s.test_files = [
66
71
  "test/test.rb"
@@ -77,6 +82,7 @@ Gem::Specification.new do |s|
77
82
  s.add_development_dependency(%q<yard>, [">= 0"])
78
83
  s.add_development_dependency(%q<jeweler>, [">= 0"])
79
84
  s.add_development_dependency(%q<patron>, [">= 0"])
85
+ s.add_development_dependency(%q<em-http-request>, [">= 0"])
80
86
  else
81
87
  s.add_dependency(%q<rufus-lru>, [">= 0"])
82
88
  s.add_dependency(%q<rufus-json>, [">= 0"])
@@ -84,6 +90,7 @@ Gem::Specification.new do |s|
84
90
  s.add_dependency(%q<yard>, [">= 0"])
85
91
  s.add_dependency(%q<jeweler>, [">= 0"])
86
92
  s.add_dependency(%q<patron>, [">= 0"])
93
+ s.add_dependency(%q<em-http-request>, [">= 0"])
87
94
  end
88
95
  else
89
96
  s.add_dependency(%q<rufus-lru>, [">= 0"])
@@ -92,6 +99,7 @@ Gem::Specification.new do |s|
92
99
  s.add_dependency(%q<yard>, [">= 0"])
93
100
  s.add_dependency(%q<jeweler>, [">= 0"])
94
101
  s.add_dependency(%q<patron>, [">= 0"])
102
+ s.add_dependency(%q<em-http-request>, [">= 0"])
95
103
  end
96
104
  end
97
105
 
data/test/base.rb CHANGED
@@ -26,28 +26,17 @@ if transport_library == 'em-http'
26
26
  Thread.pass until EM.reactor_running?
27
27
  end
28
28
 
29
- #unless $test_server
30
- #
31
- # pss = `ps aux | grep "test\/server.rb"`
32
- # puts pss
33
- #
34
- # $test_server = pss.index(' ruby ') || fork do
35
- # #exec('ruby test/server.rb')
36
- # exec('ruby test/server.rb > /dev/null 2>&1')
37
- # end
38
- # puts
39
- # puts "...test server is at #{$test_server}"
40
- # puts
41
- # sleep 1
42
- #end
43
-
29
+ t = nil
44
30
  begin
45
- Rufus::Jig::Http.new('127.0.0.1', 4567).get('/document')
31
+ t = Time.now
32
+ Rufus::Jig::Http.new('127.0.0.1', 4567).get('/document', :timeout => -1)
46
33
  rescue Exception => e
47
34
  puts
48
35
  p e
49
36
  e.backtrace.each { |l| puts l }
50
37
  puts
38
+ puts "(#{Time.now - t} seconds)"
39
+ puts
51
40
  puts "test server not running, please run :"
52
41
  puts
53
42
  puts " ruby test/server.rb"
data/test/server.rb CHANGED
@@ -207,3 +207,12 @@ delete '/a/b/c' do
207
207
  'delete'
208
208
  end
209
209
 
210
+ #
211
+ # TIMEOUT testing
212
+
213
+ get '/later' do
214
+
215
+ sleep 7
216
+ 'later'
217
+ end
218
+
data/test/to.sh ADDED
@@ -0,0 +1,25 @@
1
+ #!/bin/bash
2
+
3
+ echo
4
+ echo "================================================================================"
5
+ echo "ruby test/ut_7_http_get_timeout.rb -- --patron"
6
+ ruby -v
7
+ echo
8
+ ruby test/ut_7_http_get_timeout.rb -- --patron
9
+
10
+ echo
11
+ echo "================================================================================"
12
+ echo "ruby test/ut_7_http_get_timeout.rb -- --net"
13
+ ruby -v
14
+ echo
15
+ ruby test/ut_7_http_get_timeout.rb -- --net
16
+
17
+ echo
18
+ echo "================================================================================"
19
+ echo "ruby test/ut_7_http_get_timeout.rb -- --em"
20
+ ruby -v
21
+ echo
22
+ ruby test/ut_7_http_get_timeout.rb -- --em
23
+
24
+ echo
25
+
@@ -0,0 +1,92 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Tue Mar 9 11:09:43 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpGetTimeoutTest < Test::Unit::TestCase
12
+
13
+ #def setup
14
+ #end
15
+ def teardown
16
+ @h.close
17
+ end
18
+
19
+ def test_timeout_after_1
20
+
21
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => 1)
22
+
23
+ t = nil
24
+
25
+ assert_raise Rufus::Jig::TimeoutError do
26
+ t = Time.now
27
+ @h.get('/later')
28
+ end
29
+
30
+ d = Time.now - t; dd = 4.0; assert d < dd, "after #{d} seconds (#{dd})"
31
+ # grr, em-http-request forces me to use 4.0 (2.0 for the others)
32
+ end
33
+
34
+ def test_timeout_after_5
35
+
36
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
37
+
38
+ t = nil
39
+
40
+ assert_raise Rufus::Jig::TimeoutError do
41
+ t = Time.now
42
+ @h.get('/later')
43
+ end
44
+
45
+ d = Time.now - t; dd = 6.0; assert d < dd, "after #{d} seconds (#{dd})"
46
+ end
47
+
48
+ def test_timeout_after_15
49
+
50
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => 15)
51
+
52
+ t = Time.now
53
+
54
+ r = begin
55
+ @h.get('/later')
56
+ rescue Rufus::Jig::TimeoutError => e
57
+ puts " :( timed out after #{Time.now - t} seconds"
58
+ flunk
59
+ end
60
+
61
+ assert_equal 'later', r
62
+ d = Time.now - t; dd = 8.0; assert d < dd, "after #{d} seconds (#{dd})"
63
+ end
64
+
65
+ def test_never_timeout
66
+
67
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => -1)
68
+
69
+ t = Time.now
70
+
71
+ r = @h.get('/later')
72
+
73
+ assert_equal 'later', r
74
+ d = Time.now - t; dd = 8.0; assert d < dd, "after #{d} seconds (#{dd})"
75
+ end
76
+
77
+ def test_request_timeout_after_1
78
+
79
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => 15)
80
+
81
+ t = nil
82
+
83
+ assert_raise Rufus::Jig::TimeoutError do
84
+ t = Time.now
85
+ @h.get('/later', :timeout => 1)
86
+ end
87
+
88
+ d = Time.now - t; dd = 4.0; assert d < dd, "after #{d} seconds (#{dd})"
89
+ # grr, em-http-request forces me to use 4.0 (2.0 for the others)
90
+ end
91
+ end
92
+
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rufus-jig
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 15
9
+ version: 0.1.15
5
10
  platform: ruby
6
11
  authors:
7
12
  - John Mettraux
@@ -10,69 +15,93 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2010-02-25 00:00:00 +09:00
18
+ date: 2010-03-12 00:00:00 +09:00
14
19
  default_executable:
15
20
  dependencies:
16
21
  - !ruby/object:Gem::Dependency
17
22
  name: rufus-lru
18
- type: :runtime
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
21
25
  requirements:
22
26
  - - ">="
23
27
  - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
24
30
  version: "0"
25
- version:
31
+ type: :runtime
32
+ version_requirements: *id001
26
33
  - !ruby/object:Gem::Dependency
27
34
  name: rufus-json
28
- type: :runtime
29
- version_requirement:
30
- version_requirements: !ruby/object:Gem::Requirement
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
31
37
  requirements:
32
38
  - - ">="
33
39
  - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
34
42
  version: "0"
35
- version:
43
+ type: :runtime
44
+ version_requirements: *id002
36
45
  - !ruby/object:Gem::Dependency
37
46
  name: rake
38
- type: :development
39
- version_requirement:
40
- version_requirements: !ruby/object:Gem::Requirement
47
+ prerelease: false
48
+ requirement: &id003 !ruby/object:Gem::Requirement
41
49
  requirements:
42
50
  - - ">="
43
51
  - !ruby/object:Gem::Version
52
+ segments:
53
+ - 0
44
54
  version: "0"
45
- version:
55
+ type: :development
56
+ version_requirements: *id003
46
57
  - !ruby/object:Gem::Dependency
47
58
  name: yard
48
- type: :development
49
- version_requirement:
50
- version_requirements: !ruby/object:Gem::Requirement
59
+ prerelease: false
60
+ requirement: &id004 !ruby/object:Gem::Requirement
51
61
  requirements:
52
62
  - - ">="
53
63
  - !ruby/object:Gem::Version
64
+ segments:
65
+ - 0
54
66
  version: "0"
55
- version:
67
+ type: :development
68
+ version_requirements: *id004
56
69
  - !ruby/object:Gem::Dependency
57
70
  name: jeweler
58
- type: :development
59
- version_requirement:
60
- version_requirements: !ruby/object:Gem::Requirement
71
+ prerelease: false
72
+ requirement: &id005 !ruby/object:Gem::Requirement
61
73
  requirements:
62
74
  - - ">="
63
75
  - !ruby/object:Gem::Version
76
+ segments:
77
+ - 0
64
78
  version: "0"
65
- version:
79
+ type: :development
80
+ version_requirements: *id005
66
81
  - !ruby/object:Gem::Dependency
67
82
  name: patron
83
+ prerelease: false
84
+ requirement: &id006 !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ segments:
89
+ - 0
90
+ version: "0"
68
91
  type: :development
69
- version_requirement:
70
- version_requirements: !ruby/object:Gem::Requirement
92
+ version_requirements: *id006
93
+ - !ruby/object:Gem::Dependency
94
+ name: em-http-request
95
+ prerelease: false
96
+ requirement: &id007 !ruby/object:Gem::Requirement
71
97
  requirements:
72
98
  - - ">="
73
99
  - !ruby/object:Gem::Version
100
+ segments:
101
+ - 0
74
102
  version: "0"
75
- version:
103
+ type: :development
104
+ version_requirements: *id007
76
105
  description: "\n Json Interwebs Get.\n\n An HTTP client, greedy with JSON content, GETting conditionally.\n\n Uses Patron and Yajl-ruby whenever possible.\n "
77
106
  email: jmettraux@gmail.com
78
107
  executables: []
@@ -92,6 +121,9 @@ files:
92
121
  - TODO.txt
93
122
  - lib/rufus-jig.rb
94
123
  - lib/rufus/jig.rb
124
+ - lib/rufus/jig/adapters/em.rb
125
+ - lib/rufus/jig/adapters/net.rb
126
+ - lib/rufus/jig/adapters/patron.rb
95
127
  - lib/rufus/jig/couch.rb
96
128
  - lib/rufus/jig/http.rb
97
129
  - lib/rufus/jig/path.rb
@@ -107,6 +139,8 @@ files:
107
139
  - test/ct_4_attachments.rb
108
140
  - test/server.rb
109
141
  - test/test.rb
142
+ - test/to.sh
143
+ - test/tt_0_get_timeout.rb
110
144
  - test/tweet.png
111
145
  - test/ut_0_http_get.rb
112
146
  - test/ut_1_http_post.rb
@@ -128,18 +162,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
128
162
  requirements:
129
163
  - - ">="
130
164
  - !ruby/object:Gem::Version
165
+ segments:
166
+ - 0
131
167
  version: "0"
132
- version:
133
168
  required_rubygems_version: !ruby/object:Gem::Requirement
134
169
  requirements:
135
170
  - - ">="
136
171
  - !ruby/object:Gem::Version
172
+ segments:
173
+ - 0
137
174
  version: "0"
138
- version:
139
175
  requirements: []
140
176
 
141
177
  rubyforge_project: rufus
142
- rubygems_version: 1.3.5
178
+ rubygems_version: 1.3.6
143
179
  signing_key:
144
180
  specification_version: 3
145
181
  summary: An HTTP client, greedy with JSON content, GETting conditionally.