Class: Slash::TyphoeusConnection

Inherits:
Connection show all
Defined in:
lib/slash/typhoeus.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#proxy, #timeout

Instance Method Summary collapse

Methods inherited from Connection

create_default, #delete, #get, #head, #post, #put

Constructor Details

#initialize(options = {}) ⇒ TyphoeusConnection

Returns a new instance of TyphoeusConnection.



31
32
33
# File 'lib/slash/typhoeus.rb', line 31

def initialize(options = {})
  @queue = options[:queue] || TyphoeusQueue.new
end

Instance Attribute Details

#queueObject

Returns the value of attribute queue.



35
36
37
# File 'lib/slash/typhoeus.rb', line 35

def queue
  @queue
end

Instance Method Details

#request(method, uri, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/slash/typhoeus.rb', line 37

def request(method, uri, options = {})
  options = options.dup
  prepare_request(uri, options)

  params, headers = options[:params], options[:headers]
  rq = Typhoeus::Request.new(uri.to_s,
    :method => method,
    :headers => headers,
    :params => !params.blank? ? params.inject({}) {|h, x| h[x[0].to_s] = x[1] || ''; h } : nil,
    :body => options[:body],
    :timeout => options[:timeout] || timeout,
    :user_agent => headers['User-Agent']
  )
  ret = nil
  rq.on_complete do |response|
    if logger
      logger.debug "%s %s --> %d (%d %.0fs)" % [rq.method.to_s.upcase, rq.url,
        response.code, response.body ? response.body.length : 0, response.time]
    end
    ret = response = augment_response(response)
    ret = yield response if block_given?
    response
  end
  async = options[:async]
  queue = [true, false, nil].include?(async) ? self.queue : async
  queue.submit(rq)
  if async
    queue
  else
    queue.run
    block_given? ? ret : check_and_raise(rq.handled_response)
  end
end