Rufus::Jig::Http

Public Class Methods

extract_http(payload_expected, *args) click to toggle source

Makes sense of arguments and extract an array that goes like [ http, path, payload, opts ].

Typical input :

  a = Rufus::Jig::Http.extract_http(false, 'http://127.0.0.1:5984')
  a = Rufus::Jig::Http.extract_http(false, '127.0.0.1', 5984, '/')
  a = Rufus::Jig::Http.extract_http(true, 'http://127.0.0.1:5984', :payload)
     # File lib/rufus/jig/http.rb, line 369
369:   def self.extract_http (payload_expected, *args)
370: 
371:     host, port = case args.first
372: 
373:       when Rufus::Jig::Http
374:         args.shift
375: 
376:       when /^http:\/\//
377:         u = Rufus::Jig.parse_uri(args.shift)
378:         args.unshift(u.path)
379:         [ u.host, u.port ]
380: 
381:       else
382:         [ args.shift, args.shift ]
383:     end
384: 
385:     path = args.shift
386:     path = '/' if path == ''
387: 
388:     payload = payload_expected ? args.shift : nil
389: 
390:     opts = args.shift || {}
391: 
392:     raise(
393:       ArgumentError.new("option Hash expected, not #{opts.inspect}")
394:     ) unless opts.is_a?(Hash)
395: 
396:     http = host.is_a?(Rufus::Jig::Http) ?
397:       host :
398:       Rufus::Jig::Http.new(host, port, opts)
399: 
400:     [ http, path, payload, opts ]
401:   end
new(host, port, opts={}) click to toggle source
    # File lib/rufus/jig/adapters/patron.rb, line 39
39:   def initialize (host, port, opts={})
40: 
41:     super(host, port, opts)
42:   end
new(host, port, opts={}) click to toggle source
    # File lib/rufus/jig/adapters/net.rb, line 45
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
new( host, port, opts={} ) click to toggle source
    # File lib/rufus/jig/adapters/em.rb, line 51
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

Public Instance Methods

close() click to toggle source
    # File lib/rufus/jig/adapters/patron.rb, line 44
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
variant() click to toggle source
    # File lib/rufus/jig/adapters/net.rb, line 54
54:   def variant
55:     :net
56:   end
variant() click to toggle source
    # File lib/rufus/jig/adapters/patron.rb, line 52
52:   def variant
53:     :patron
54:   end
variant() click to toggle source
    # File lib/rufus/jig/adapters/em.rb, line 58
58:   def variant
59:     :em
60:   end

Protected Instance Methods

do_request(method, path, data, opts) click to toggle source
    # File lib/rufus/jig/adapters/patron.rb, line 78
78:   def do_request (method, path, data, opts)
79: 
80:     opts['Expect'] = '' if (method == :put) && ( ! @options[:expect])
81: 
82:     args = case method
83:       when :post, :put then [ path, data, opts ]
84:       else [ path, opts ]
85:     end
86: 
87:     begin
88:       get_patron(opts).send(method, *args)
89:     rescue Patron::TimeoutError => te
90:       raise Rufus::Jig::TimeoutError
91:     end
92:   end
do_request( method, path, data, opts ) click to toggle source
    # File lib/rufus/jig/adapters/em.rb, line 64
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
do_request(method, path, data, opts) click to toggle source
    # File lib/rufus/jig/adapters/net.rb, line 77
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
em_request( uri = '/' ) click to toggle source
    # File lib/rufus/jig/adapters/em.rb, line 81
81:   def em_request( uri = '/' )
82:     uri = Rufus::Jig.parse_uri( 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
em_response( em_client ) click to toggle source
     # File lib/rufus/jig/adapters/em.rb, line 93
 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
get_http(opts) click to toggle source
    # File lib/rufus/jig/adapters/net.rb, line 60
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
get_patron(opts) click to toggle source
    # File lib/rufus/jig/adapters/patron.rb, line 58
58:   def get_patron (opts)
59: 
60:     to = (opts[:timeout] || @options[:timeout])
61:     to = to.to_i if to
62:     to = nil if to && to < 1
63: 
64:     patron = Patron::Session.new
65:     patron.base_url = "#{@host}:#{@port}"
66: 
67:     #patron.connect_timeout = 1
68:       # connection timeout defaults to 1 second
69:     patron.timeout = to
70: 
71:     patron.headers['User-Agent'] =
72:       @options[:user_agent] ||
73:       [ self.class, Rufus::Jig::VERSION, '(patron)' ].join(' ')
74: 
75:     patron
76:   end
request_headers( options ) click to toggle source
     # File lib/rufus/jig/adapters/em.rb, line 118
118:   def request_headers( options )
119:     headers = { 'user-agent' => @options[:user_agent] }
120: 
121:     ] 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

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.