Class: MailchimpAPI::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/mailchimp-api/response.rb

Overview

Response class for handling Mailchimp API responses

Constant Summary collapse

RETRYABLE_RESPONSES =

List of Net::HTTP responses that can be retried

[
  Net::HTTPServerError,    # 5xx
  Net::HTTPTooManyRequests # 429
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, request:) ⇒ Response

Initializes Response object

Examples:

response = MailchimpAPI::Response.new(http_response, request: request)

Parameters:

  • http_response (Net::HTTP::Response)

    original response

  • request (Request)

    Request that generates this response



27
28
29
30
31
32
33
34
# File 'lib/mailchimp-api/response.rb', line 27

def initialize(http_response, request:)
  @request = request
  @http_response = http_response
  @http_status = nil
  @http_headers = nil
  @http_body = nil
  @body = nil
end

Instance Attribute Details

#http_responseNet::HTTP::Response (readonly)

Returns Original Net::HTTP::Response object.

Returns:

  • (Net::HTTP::Response)

    Original Net::HTTP::Response object



16
17
18
# File 'lib/mailchimp-api/response.rb', line 16

def http_response
  @http_response
end

#requestRequest (readonly)

Returns Request object.

Returns:



19
20
21
# File 'lib/mailchimp-api/response.rb', line 19

def request
  @request
end

Instance Method Details

#[](key) ⇒ Object?

Takes specific key from body, returns nil if key not present in parsed body

Examples:

response[:id] # => "123"

Parameters:

  • key (String, Symbol)

    Key to fetch from response body

Returns:

  • (Object, nil)

    Value for the given key or nil if not found



71
72
73
# File 'lib/mailchimp-api/response.rb', line 71

def [](key)
  body[key.to_sym] if body.is_a?(Hash)
end

#bodyHash, String

Parses JSON body if response body contains JSON or returns original http body string

Examples:

response.body # => { id: "123", name: "Example List" }

Returns:

  • (Hash, String)

    Parsed response body (with symbolized keys)



41
42
43
# File 'lib/mailchimp-api/response.rb', line 41

def body
  @body ||= json_response? ? parse_json(http_body) : http_body
end

#failed?Boolean

Checks http status code is not 2xx

Examples:

response.failed? # => false

Returns:

  • (Boolean)

    Returns true if response has not success status code



98
99
100
# File 'lib/mailchimp-api/response.rb', line 98

def failed?
  !success?
end

#fetch(key) ⇒ Object

Fetches specific key from body, raises error if key not exists

Examples:

response.fetch(:id) # => "123"

Parameters:

  • key (String, Symbol)

    Key to fetch from response body

Returns:

  • (Object)

    Value for the given key

Raises:

  • (KeyError)

    if key is not found in response body



81
82
83
84
# File 'lib/mailchimp-api/response.rb', line 81

def fetch(key)
  data = body.is_a?(Hash) ? body : {}
  data.fetch(key.to_sym)
end

#http_bodyString

Returns Original http body.

Examples:

response.http_body # => '{"id":"123","name":"Example List"}'

Returns:

  • (String)

    Original http body



62
63
64
# File 'lib/mailchimp-api/response.rb', line 62

def http_body
  @http_body ||= http_response.body
end

#http_headersHash

Returns HTTP headers as Hash.

Examples:

response.http_headers # => { "content-type" => "application/json" }

Returns:

  • (Hash)

    HTTP headers as Hash



55
56
57
# File 'lib/mailchimp-api/response.rb', line 55

def http_headers
  @http_headers ||= http_response.each_header.to_h
end

#http_statusInteger

Returns HTTP status as Integer.

Examples:

response.http_status # => 200

Returns:

  • (Integer)

    HTTP status as Integer



48
49
50
# File 'lib/mailchimp-api/response.rb', line 48

def http_status
  @http_status ||= http_response.code.to_i
end

#inspectString

Instance representation string. Default was overwritten to hide secrets

Examples:

response.inspect # => "#<MailchimpAPI::Response (200)>"

Returns:

  • (String)

    String representation of the response



113
114
115
# File 'lib/mailchimp-api/response.rb', line 113

def inspect
  "#<#{self.class.name} (#{http_response.code})>"
end

#retryable?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Checks if response status code is retriable (5xx, 409, 429)

Returns:

  • (Boolean)

    Returns true if status code is retriable (5xx, 409, 429)



105
106
107
# File 'lib/mailchimp-api/response.rb', line 105

def retryable?
  failed? && RETRYABLE_RESPONSES.any? { |retryable_class| http_response.is_a?(retryable_class) }
end

#success?Boolean

Checks http status code is 2xx

Examples:

response.success? # => true

Returns:

  • (Boolean)

    Returns true if response has success status code (2xx)



90
91
92
# File 'lib/mailchimp-api/response.rb', line 90

def success?
  http_response.is_a?(Net::HTTPSuccess)
end