Class: PaypalAPI::Response

Inherits:
Object
  • Object
show all
Includes:
PaginationHelpers
Defined in:
lib/paypal-api/response.rb

Overview

PaypalAPI::Response object

Defined Under Namespace

Modules: PaginationHelpers

Constant Summary collapse

RETRYABLE_RESPONSES =

List of Net::HTTP responses that can be retried

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaginationHelpers

#each_page, #each_page_item

Constructor Details

#initialize(http_response, request:) ⇒ Response

Initializes Response object

Parameters:

  • http_response (Net::HTTP::Response)

    original response

  • request (Request)

    Request that generates this response



31
32
33
34
35
36
37
38
# File 'lib/paypal-api/response.rb', line 31

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



18
19
20
# File 'lib/paypal-api/response.rb', line 18

def http_response
  @http_response
end

#requestRequest (readonly)

Returns Request object.

Returns:



21
22
23
# File 'lib/paypal-api/response.rb', line 21

def request
  @request
end

Instance Method Details

#[](key) ⇒ Object

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



64
65
66
# File 'lib/paypal-api/response.rb', line 64

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

Returns:

  • (Hash, String)

    Parsed response body (with symbolized keys)



44
45
46
# File 'lib/paypal-api/response.rb', line 44

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

#failed?Boolean

Checks http status code is not 2xx

Returns:

  • (Boolean)

    Returns true if response has not success status code



84
85
86
# File 'lib/paypal-api/response.rb', line 84

def failed?
  !success?
end

#fetch(key) ⇒ Object

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



69
70
71
72
# File 'lib/paypal-api/response.rb', line 69

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

Follow up HATEOAS link

Parameters:

  • rel (String)

    Target link “rel” attribute

  • query (Hash) (defaults to: nil)

    Request additional query parameters

  • body (Hash) (defaults to: nil)

    Request body parameters

  • headers (Hash) (defaults to: nil)

    Request headers

Returns:

  • (Response, nil)

    Follow-up link response, if link with provided “rel” exists

See Also:



123
124
125
126
127
128
129
130
131
132
# File 'lib/paypal-api/response.rb', line 123

def follow_up_link(rel, query: nil, body: nil, headers: nil)
  links = self[:links]
  return unless links

  link = links.find { |curr_link| curr_link[:rel] == rel.to_s }
  return unless link

  http_method = link[:method]&.downcase || :get
  request.client.public_send(http_method, link[:href], query: query, body: body, headers: headers)
end

#http_bodyString

Returns Original http body.

Returns:

  • (String)

    Original http body



59
60
61
# File 'lib/paypal-api/response.rb', line 59

def http_body
  @http_body ||= http_response.body
end

#http_headersHash

Returns HTTP headers as Hash.

Returns:

  • (Hash)

    HTTP headers as Hash



54
55
56
# File 'lib/paypal-api/response.rb', line 54

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

#http_statusInteger

Returns HTTP status as Integer.

Returns:

  • (Integer)

    HTTP status as Integer



49
50
51
# File 'lib/paypal-api/response.rb', line 49

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

#inspectObject

Instance representation string. Default was overwritten to hide secrets



107
108
109
# File 'lib/paypal-api/response.rb', line 107

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)



92
93
94
# File 'lib/paypal-api/response.rb', line 92

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

#success?Boolean

Checks http status code is 2xx

Returns:

  • (Boolean)

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



77
78
79
# File 'lib/paypal-api/response.rb', line 77

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

#unauthorized?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 unauthorized (401)

Returns:

  • (Boolean)

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



100
101
102
# File 'lib/paypal-api/response.rb', line 100

def unauthorized?
  http_response.is_a?(Net::HTTPUnauthorized)
end