Class: Mode::Sdk::Client::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/mode/sdk/client/response.rb

Overview

The Response class wraps a Mode API response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, expected_codes = []) ⇒ Mode::Sdk::Client::Response

Construct a new Response instance


21
22
23
24
# File 'lib/mode/sdk/client/response.rb', line 21

def initialize(http_response, expected_codes = [])
  @http_response  = http_response
  @expected_codes = expected_codes
end

Instance Attribute Details

#expected_codesObject (readonly)

Returns the value of attribute expected_codes


11
12
13
# File 'lib/mode/sdk/client/response.rb', line 11

def expected_codes
  @expected_codes
end

#http_responseObject (readonly)

Returns the value of attribute http_response


11
12
13
# File 'lib/mode/sdk/client/response.rb', line 11

def http_response
  @http_response
end

Instance Method Details

#bodyHash

The parsed HTTP response body


38
39
40
# File 'lib/mode/sdk/client/response.rb', line 38

def body
  JSON.parse(http_response.body)
end

#codeInteger

The HTTP response code


30
31
32
# File 'lib/mode/sdk/client/response.rb', line 30

def code
  http_response.code.to_i
end

#raise_unexpected_codeObject

Raise exception due to an unexpected response code

Raises:


59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mode/sdk/client/response.rb', line 59

def raise_unexpected_code
  case code
  when 401
    fail Mode::Sdk::Client::AuthenticationError
  when 403
    fail Mode::Sdk::Client::AuthorizationError
  else
    fail Mode::Sdk::Client::ResponseError,
         "Unexpected response: #{code}\n#{body}"
  end
end

#validate!true

Validate the HTTP response code


46
47
48
49
50
# File 'lib/mode/sdk/client/response.rb', line 46

def validate!
  raise_unexpected_code if unexpected_code?

  true
end