Class: Withings::Api::ApiResponse

Inherits:
Object
  • Object
show all
Includes:
ResultsHelpers
Defined in:
lib/withings-api/api_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ResultsHelpers

#coerce_hash

Constructor Details

#initialize(string_or_json, body_class) ⇒ ApiResponse

Returns a new instance of ApiResponse.



13
14
15
16
17
18
19
20
21
# File 'lib/withings-api/api_response.rb', line 13

def initialize(string_or_json, body_class)
  hash = coerce_hash string_or_json

  @status = hash["status"] || raise(InvalidFormat, :status_field_missing)

  if hash.key?("body")
    @body = body_class.new(hash["body"])
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/withings-api/api_response.rb', line 5

def body
  @body
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/withings-api/api_response.rb', line 5

def status
  @status
end

Class Method Details

.create!(http_response, body_class) ⇒ Object



7
8
9
10
11
# File 'lib/withings-api/api_response.rb', line 7

def self.create!(http_response, body_class)
  raise HttpNotSuccessError.new(http_response.code, http_response.body) if http_response.code != '200'

  self.new(http_response.body, body_class)
end