rufus-jig 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CREDITS.txt +4 -1
- data/README.rdoc +23 -1
- data/lib/rufus/jig/http.rb +201 -8
- data/lib/rufus/jig.rb +1 -1
- metadata +3 -2
data/CREDITS.txt
CHANGED
data/README.rdoc
CHANGED
@@ -3,10 +3,27 @@
|
|
3
3
|
|
4
4
|
A HTTP client, greedy with JSON content, GETting conditionally.
|
5
5
|
|
6
|
-
Uses Patron (http://github.com/toland/patron)
|
6
|
+
Uses Patron (http://github.com/toland/patron) or em-http-request (http://github.com/igrigorik/em-http-request)
|
7
|
+
and Yajl-ruby (http://github.com/brianmario/yajl-ruby) whenever possible.
|
7
8
|
|
8
9
|
This library also contains some CouchDB helpers.
|
9
10
|
|
11
|
+
To select a HTTP transport layer for rufus-jig, just make sure you have loaded
|
12
|
+
the library before loading rufus-jig.
|
13
|
+
|
14
|
+
For Patron:
|
15
|
+
|
16
|
+
require 'patron'
|
17
|
+
require 'rufus/jig'
|
18
|
+
|
19
|
+
For em-http-request:
|
20
|
+
|
21
|
+
require 'em-http'
|
22
|
+
require 'rufus/jig'
|
23
|
+
|
24
|
+
Please not that em-http-request-0.2.2 and earlier don't support the PUT and DELETE
|
25
|
+
verbs. Until a newer gem is released you'll need to build the em-http-request gem
|
26
|
+
from source.
|
10
27
|
|
11
28
|
== examples
|
12
29
|
|
@@ -52,6 +69,7 @@ posting...
|
|
52
69
|
=== Couch helpers
|
53
70
|
|
54
71
|
For the real thing : http://github.com/couchrest/couchrest
|
72
|
+
There is also the excellent : http://github.com/langalex/couch_potato
|
55
73
|
|
56
74
|
require 'patron'
|
57
75
|
require 'yajl'
|
@@ -142,6 +160,10 @@ To test the CouchDB helpers, make you have a running Couch on http:/127.0.0.1:59
|
|
142
160
|
|
143
161
|
ruby test/test.rb --couch
|
144
162
|
|
163
|
+
To test the em-http-request HTTP transport, make sure the sinatra server is running and then do:
|
164
|
+
|
165
|
+
ruby test/test.rb -- --em
|
166
|
+
|
145
167
|
|
146
168
|
== mailing list
|
147
169
|
|
data/lib/rufus/jig/http.rb
CHANGED
@@ -52,6 +52,12 @@ module Rufus::Jig
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
+
class HttpResponse
|
56
|
+
|
57
|
+
attr_reader :status, :headers, :body
|
58
|
+
attr_reader :original
|
59
|
+
end
|
60
|
+
|
55
61
|
# The base for the Rufus::Jig::Http class.
|
56
62
|
#
|
57
63
|
class HttpCore
|
@@ -197,13 +203,22 @@ module Rufus::Jig
|
|
197
203
|
|
198
204
|
def add_prefix (path, opts)
|
199
205
|
|
200
|
-
|
206
|
+
uri = URI.parse( path )
|
201
207
|
|
202
|
-
if
|
203
|
-
|
204
|
-
|
208
|
+
if !uri.host.nil?
|
209
|
+
return uri.to_s
|
210
|
+
|
211
|
+
else
|
212
|
+
uri.host.nil?
|
213
|
+
|
214
|
+
elts = [ path ]
|
205
215
|
|
206
|
-
|
216
|
+
if path.match(/^[^\/]/) && prefix = @options[:prefix]
|
217
|
+
elts.unshift(prefix)
|
218
|
+
end
|
219
|
+
|
220
|
+
return Path.join(*elts)
|
221
|
+
end
|
207
222
|
end
|
208
223
|
|
209
224
|
def add_params (path, opts)
|
@@ -258,6 +273,17 @@ end
|
|
258
273
|
|
259
274
|
if defined?(Patron) # gem install patron
|
260
275
|
|
276
|
+
class Rufus::Jig::HttpResponse
|
277
|
+
|
278
|
+
def initialize (patron_res)
|
279
|
+
|
280
|
+
@original = patron_res
|
281
|
+
@status = patron_res.status
|
282
|
+
@headers = patron_res.headers
|
283
|
+
@body = patron_res.body
|
284
|
+
end
|
285
|
+
end
|
286
|
+
|
261
287
|
class Rufus::Jig::Http < Rufus::Jig::HttpCore
|
262
288
|
|
263
289
|
def initialize (host, port, opts={})
|
@@ -268,7 +294,8 @@ if defined?(Patron) # gem install patron
|
|
268
294
|
@patron.base_url = "#{host}:#{port}"
|
269
295
|
|
270
296
|
@patron.headers['User-Agent'] =
|
271
|
-
opts[:user_agent] ||
|
297
|
+
opts[:user_agent] ||
|
298
|
+
"#{self.class} #{Rufus::Jig::VERSION} (patron)"
|
272
299
|
end
|
273
300
|
|
274
301
|
protected
|
@@ -285,6 +312,8 @@ if defined?(Patron) # gem install patron
|
|
285
312
|
|
286
313
|
def do_put (path, data, opts)
|
287
314
|
|
315
|
+
opts['Expect'] = '' unless @options[:expect]
|
316
|
+
|
288
317
|
@patron.put(path, data, opts)
|
289
318
|
end
|
290
319
|
|
@@ -294,11 +323,175 @@ if defined?(Patron) # gem install patron
|
|
294
323
|
end
|
295
324
|
end
|
296
325
|
|
326
|
+
elsif defined?( EventMachine::HttpRequest )
|
327
|
+
|
328
|
+
class Rufus::Jig::HttpResponse
|
329
|
+
|
330
|
+
def initialize( em_client )
|
331
|
+
|
332
|
+
@original = [ em_client, em_client.response ]
|
333
|
+
|
334
|
+
@status = em_client.response_header.status
|
335
|
+
@headers = response_headers( em_client.response_header )
|
336
|
+
@body = em_client.response
|
337
|
+
end
|
338
|
+
|
339
|
+
protected
|
340
|
+
|
341
|
+
def response_headers( hash )
|
342
|
+
hash.inject({}) do |headers, (key, value)|
|
343
|
+
key = key.downcase.split('_').map { |c| c.capitalize }.join('-')
|
344
|
+
headers[ key ] = value
|
345
|
+
headers
|
346
|
+
end
|
347
|
+
end
|
348
|
+
end
|
349
|
+
|
350
|
+
class Rufus::Jig::Http < Rufus::Jig::HttpCore
|
351
|
+
|
352
|
+
def initialize (host, port, opts={})
|
353
|
+
|
354
|
+
super(host, port, opts)
|
355
|
+
|
356
|
+
@em_host = host
|
357
|
+
@em_port = port
|
358
|
+
|
359
|
+
@em_ua = opts[:user_agent] || "#{self.class} #{Rufus::Jig::VERSION} (em)"
|
360
|
+
end
|
361
|
+
|
362
|
+
protected
|
363
|
+
|
364
|
+
def do_get( path, data, opts )
|
365
|
+
http = em_request( path ).get( :head => request_headers(opts) )
|
366
|
+
|
367
|
+
em_response( http )
|
368
|
+
end
|
369
|
+
|
370
|
+
def do_post( path, data, opts )
|
371
|
+
http = em_request( path ).post( :body => data, :head => request_headers(opts) )
|
372
|
+
|
373
|
+
em_response( http )
|
374
|
+
end
|
375
|
+
|
376
|
+
def do_delete( path, data, opts )
|
377
|
+
http = em_request( path ).delete( :head => request_headers( opts ) )
|
378
|
+
|
379
|
+
em_response( http )
|
380
|
+
end
|
381
|
+
|
382
|
+
def do_put( path, data, opts )
|
383
|
+
http = em_request( path ).put( :body => data, :head => request_headers( opts ) )
|
384
|
+
|
385
|
+
em_response( http )
|
386
|
+
end
|
387
|
+
|
388
|
+
def em_request( uri = '/' )
|
389
|
+
uri = URI.parse( uri )
|
390
|
+
uri = URI::HTTP.build(
|
391
|
+
:host => ( uri.host || @em_host ),
|
392
|
+
:port => ( uri.port || @em_port ),
|
393
|
+
:path => uri.path,
|
394
|
+
:query => uri.query
|
395
|
+
)
|
396
|
+
|
397
|
+
EventMachine::HttpRequest.new( uri.to_s )
|
398
|
+
end
|
399
|
+
|
400
|
+
def em_response( em_client )
|
401
|
+
th = Thread.current
|
402
|
+
|
403
|
+
em_client.callback {
|
404
|
+
th.wakeup
|
405
|
+
}
|
406
|
+
|
407
|
+
Thread.stop
|
408
|
+
|
409
|
+
Rufus::Jig::HttpResponse.new( em_client )
|
410
|
+
end
|
411
|
+
|
412
|
+
def request_headers( options )
|
413
|
+
headers = { 'user-agent' => @em_ua }
|
414
|
+
|
415
|
+
%w[ Accept If-None-Match Content-Type ].each do |k|
|
416
|
+
headers[k] = options[k] if options.has_key?( k )
|
417
|
+
end
|
418
|
+
|
419
|
+
headers
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
297
423
|
else
|
298
424
|
|
299
|
-
|
425
|
+
require 'net/http'
|
426
|
+
|
427
|
+
class Rufus::Jig::HttpResponse
|
428
|
+
|
429
|
+
def initialize (nh_res)
|
430
|
+
|
431
|
+
@original = nh_res
|
432
|
+
@status = nh_res.code.to_i
|
433
|
+
@body = nh_res.body
|
434
|
+
@headers = {}
|
435
|
+
nh_res.each { |k, v|
|
436
|
+
@headers[k.split('-').collect { |s| s.capitalize }.join('-')] = v
|
437
|
+
}
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
class Rufus::Jig::Http < Rufus::Jig::HttpCore
|
442
|
+
|
443
|
+
def initialize (host, port, opts={})
|
444
|
+
|
445
|
+
super(host, port, opts)
|
446
|
+
|
447
|
+
@http = Net::HTTP.new(host, port)
|
448
|
+
|
449
|
+
to = opts[:timeout]
|
450
|
+
if to
|
451
|
+
to = to.to_i
|
452
|
+
@http.open_timeout = to
|
453
|
+
@http.read_timeout = to
|
454
|
+
end
|
455
|
+
|
456
|
+
@options[:user_agent] =
|
457
|
+
opts[:user_agent] ||
|
458
|
+
"#{self.class} #{Rufus::Jig::VERSION} (net/http)"
|
459
|
+
end
|
460
|
+
|
461
|
+
protected
|
462
|
+
|
463
|
+
def do_get (path, data, opts)
|
464
|
+
|
465
|
+
do_request(:get, path, data, opts)
|
466
|
+
end
|
467
|
+
|
468
|
+
def do_post (path, data, opts)
|
469
|
+
|
470
|
+
do_request(:post, path, data, opts)
|
471
|
+
end
|
472
|
+
|
473
|
+
def do_put (path, data, opts)
|
474
|
+
|
475
|
+
do_request(:put, path, data, opts)
|
476
|
+
end
|
477
|
+
|
478
|
+
def do_delete (path, data, opts)
|
479
|
+
|
480
|
+
do_request(:delete, path, data, opts)
|
481
|
+
end
|
300
482
|
|
301
|
-
|
483
|
+
def do_request (method, path, data, opts)
|
484
|
+
|
485
|
+
req = eval("Net::HTTP::#{method.to_s.capitalize}").new(path)
|
486
|
+
|
487
|
+
req['User-Agent'] = options[:user_agent]
|
488
|
+
opts.each { |k, v| req[k] = v if k.is_a?(String) }
|
489
|
+
|
490
|
+
req.body = data ? data : ''
|
491
|
+
|
492
|
+
Rufus::Jig::HttpResponse.new(@http.start { @http.request(req) })
|
493
|
+
end
|
494
|
+
end
|
302
495
|
|
303
496
|
end
|
304
497
|
|
data/lib/rufus/jig.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rufus-jig
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Mettraux
|
8
|
+
- Kenneth Kalmer
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date: 2009-
|
13
|
+
date: 2009-12-04 00:00:00 +09:00
|
13
14
|
default_executable:
|
14
15
|
dependencies: []
|
15
16
|
|