Class: Square::ApiResponse

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

Overview

Http response received.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

The constructor

Parameters:

  • The (HttpResponse)

    original, raw response from the api.

  • The (Object)

    data field specified for the response.

  • Any (Array<String>)

    errors returned by the server.



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
# File 'lib/square/http/api_response.rb', line 11

def initialize(http_response,
               data: nil,
               errors: nil)
  @status_code = http_response.status_code
  @reason_phrase = http_response.reason_phrase
  @headers = http_response.headers
  @raw_body = http_response.raw_body
  @request = http_response.request
  @errors = errors

  if data.is_a? Hash
    if 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 = Struct.new(*data.keys).new(*data.values) if data.keys.any?
    end
  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

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



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

def raw_body
  @raw_body
end

#reason_phraseObject (readonly)

Returns the value of attribute reason_phrase.



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

def reason_phrase
  @reason_phrase
end

#requestObject (readonly)

Returns the value of attribute request.



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

def request
  @request
end

#status_codeObject (readonly)

Returns the value of attribute status_code.



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

def status_code
  @status_code
end

Instance Method Details

#error?Boolean

returns true if status_code is between 400-600

Returns:

  • (Boolean)


43
44
45
# File 'lib/square/http/api_response.rb', line 43

def error?
  status_code >= 400 && status_code < 600
end

#success?Boolean

returns true if status_code is between 200-300

Returns:

  • (Boolean)


38
39
40
# File 'lib/square/http/api_response.rb', line 38

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