Module: Gnarly::Typhoeus

Included in:
Environment
Defined in:
lib/gnarly/typhoeus.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make_headers(response) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gnarly/typhoeus.rb', line 24

def self.make_headers(response)
  headers = {}
  response.headers.split("\n").each do |ln|
    a = ln.split(":", 2)
    if a.size == 2
      key = a.first.strip
      unless ["status"].include?(key.downcase)
        headers[key] = a.last.strip
      end
    end
  end
  headers
end

.make_rack_response(response) ⇒ Object



38
39
40
# File 'lib/gnarly/typhoeus.rb', line 38

def self.make_rack_response(response)
  [response.code, Typhoeus.make_headers(response), [response.body]]
end

Instance Method Details

#typhoeus(method, url, opts = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/gnarly/typhoeus.rb', line 9

def typhoeus(method, url, opts={})
  response = ::Typhoeus::Request.send method, url, opts
  if block_given?
    r = yield response
    case r
    when ::Typhoeus::Response
      Typhoeus.make_rack_response r
    else
      r
    end
  else
    Typhoeus.make_rack_response response
  end
end