Class: Nitlink::ResponseNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/nitlink/response_normalizer.rb

Instance Method Summary collapse

Instance Method Details

#metadata(response) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nitlink/response_normalizer.rb', line 6

def (response)
  response_class = response.class.name

  uri, status, (link, content_location) = case response_class
  when 'Curl::Easy'
    [response.url.chomp('?'), response.response_code, grab_headers(headers_from_string response.header_str)]
  when 'Excon::Response'
    scheme = response.port == 443 ? 'https' : 'http'
    # We have to reconstruct to URL annoyingly
    uri = URI::HTTP.new(scheme, nil, response.host, nil, nil, response.path, nil, nil, nil)

    [uri, response.status, grab_headers(response.headers)]
  when 'Faraday::Response'
    response = response.to_hash
    [response[:url], response[:status], grab_headers(response[:response_headers])]
  when 'HTTP::Message'
    [response.header.request_uri, response.status, grab_headers(Hash[response.header.all])]
  when 'HTTP::Response'
    [response.uri, response.status, grab_headers(response.headers.to_h)]
  when 'HTTParty::Response'
    [response.request.uri, response.code, grab_headers(response.response.to_hash)]
  when 'Patron::Response'
    [response.url, response.status, grab_headers(response.headers)]
  when 'RestClient::Response'
    [response.request.url, response.code, grab_headers(response.net_http_res.to_hash)]
  when 'Tempfile', 'StringIO'
    # ↑ returned by OpenURI
    [response.base_uri, response.status[0], grab_headers(response.meta)]
  when 'Typhoeus::Response'
    [response.request.base_url, response.code, grab_headers(response.headers)]
  when 'Unirest::HttpResponse'
    warn "Unirest support is deprecated and will be removed in Nitlink 2.0"
    return (response.raw_body)
  when 'Hash'
    response = Nitlink::HashWithIndifferentAccess.new(response)
    response[:headers] = headers_from_string(response[:headers]) if String === response[:headers]

    [response[:request_uri], response[:status], grab_headers(response[:headers])]
  else
    if defined?(Net::HTTPResponse) && Net::HTTPResponse === response
      [response.uri, response.code, grab_headers(response.to_hash)]
    else
      raise unknown_type(response)
    end
  end

  [URI.parse(uri.to_s), (status ? Integer(status) : status), link, content_location]
end