Class: Anvil::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/anvil/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
# File 'lib/anvil/response.rb', line 7

def initialize(http_response)
  @http_response = http_response
  @code = http_response.code.to_i
  @raw_body = http_response.body
  @headers = extract_headers(http_response)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



5
6
7
# File 'lib/anvil/response.rb', line 5

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/anvil/response.rb', line 5

def headers
  @headers
end

#http_responseObject (readonly)

Returns the value of attribute http_response.



5
6
7
# File 'lib/anvil/response.rb', line 5

def http_response
  @http_response
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



5
6
7
# File 'lib/anvil/response.rb', line 5

def raw_body
  @raw_body
end

Instance Method Details

#binary?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
# File 'lib/anvil/response.rb', line 82

def binary?
  content_type.include?('application/pdf') ||
    content_type.include?('application/octet-stream') ||
    content_type.include?('application/zip')
end

#bodyObject



22
23
24
25
26
27
28
29
30
# File 'lib/anvil/response.rb', line 22

def body
  return raw_body if binary?

  @body ||= begin
    JSON.parse(raw_body, symbolize_names: true)
  rescue JSON::ParserError
    raw_body
  end
end

#content_typeObject



78
79
80
# File 'lib/anvil/response.rb', line 78

def content_type
  headers['content-type'] || ''
end

#dataObject



32
33
34
35
36
37
38
39
40
# File 'lib/anvil/response.rb', line 32

def data
  return raw_body if binary?

  if body.is_a?(Hash)
    body[:data] || body
  else
    body
  end
end

#error?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/anvil/response.rb', line 18

def error?
  !success?
end

#error_messageObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/anvil/response.rb', line 48

def error_message
  return nil unless error?

  if errors.any?
    errors.map { |e| e[:message] || e['message'] }.join(', ')
  elsif body.is_a?(Hash) && body[:message]
    body[:message]
  else
    "HTTP #{code} Error"
  end
end

#errorsObject



42
43
44
45
46
# File 'lib/anvil/response.rb', line 42

def errors
  return [] unless error? && body.is_a?(Hash)

  body[:errors] || body[:fields] || []
end

#rate_limitObject

Rate limiting headers



61
62
63
# File 'lib/anvil/response.rb', line 61

def rate_limit
  headers['x-ratelimit-limit']&.to_i
end

#rate_limit_remainingObject



65
66
67
# File 'lib/anvil/response.rb', line 65

def rate_limit_remaining
  headers['x-ratelimit-remaining']&.to_i
end

#rate_limit_resetObject



69
70
71
72
# File 'lib/anvil/response.rb', line 69

def rate_limit_reset
  reset = headers['x-ratelimit-reset']&.to_i
  Time.at(reset) if reset
end

#retry_afterObject



74
75
76
# File 'lib/anvil/response.rb', line 74

def retry_after
  headers['retry-after']&.to_i
end

#success?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/anvil/response.rb', line 14

def success?
  code >= 200 && code < 300
end