Class: KiwiApi::Response

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

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



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

def initialize(response)
  @response = response
end

Instance Method Details

#bodyObject



17
18
19
20
21
22
# File 'lib/kiwi_api/response.rb', line 17

def body
  JSON.parse(
    JSON.parse(@response.body).deep_transform_keys { |key| key.to_s.underscore }.to_json,
    object_class: OpenStruct
  ) rescue nil
end

#errorsObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/kiwi_api/response.rb', line 24

def errors
  unless @response.success?
    err = body

    OpenStruct.new(
      message: @response.reason_phrase,
      full_messages: err.nil? ? nil : err.message
    )
  end
end

#statusObject



13
14
15
# File 'lib/kiwi_api/response.rb', line 13

def status
  @response.status
end

#success?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/kiwi_api/response.rb', line 9

def success?
  true unless @response.status != 200
end