Class: Square::ApiResponse

Inherits:
CoreLibrary::ApiResponse
  • Object
show all
Defined in:
lib/square/http/api_response.rb

Overview

Http response received.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, data: nil, errors: nil) ⇒ ApiResponse

The constructor

Parameters:

  • http_response (HttpResponse)

    The original, raw response from the api.

  • data (Object) (defaults to: nil)

    The data field specified for the response.

  • errors (Array<String>) (defaults to: nil)

    Any errors returned by the server.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/square/http/api_response.rb', line 10

def initialize(http_response,
               data: nil,
               errors: nil)
  super
  @errors = errors

  if (data.is_a? Hash) && data.keys.any?
    @body = Struct.new(*data.keys) do
      define_method(:to_s) { http_response.raw_body }
    end.new(*data.values)

    @cursor = data.fetch(:cursor, nil)
    data.reject! { |k| %i[cursor errors].include?(k) }
    @data = data.keys.any? ? Struct.new(*data.keys).new(*data.values) : nil
  else
    @data = data
    @body = data
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'lib/square/http/api_response.rb', line 4

def body
  @body
end

#cursorObject (readonly)

Returns the value of attribute cursor.



4
5
6
# File 'lib/square/http/api_response.rb', line 4

def cursor
  @cursor
end

Class Method Details

.create(parent_instance) ⇒ Object

The factory method for creating the API Response instance of the SDK from its parent instance in the core lirbary.

Parameters:

  • parent_instance (CoreLibrary::HttpResponse)

    The Api Response instance from core library.



32
33
34
35
36
37
# File 'lib/square/http/api_response.rb', line 32

def self.create(parent_instance)
  ApiResponse.new(CoreLibrary::HttpResponse
                       .new(parent_instance.status_code, parent_instance.reason_phrase,
                            parent_instance.headers, parent_instance.raw_body, parent_instance.request),
                  data: parent_instance.data, errors: parent_instance.errors)
end