Class: Openlive::Response

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

Instance Attribute Summary collapse

Attributes inherited from Base

#api_data

Instance Method Summary collapse

Methods inherited from Base

#connection, connection, handle_response, #oauth, oauth, #refresh

Constructor Details

#initialize(faraday_response) ⇒ Response

Initialize the response object

Parameters:

  • faraday_response (Faraday::Response)


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

def initialize(faraday_response)
  @response = faraday_response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *opts, &block) ⇒ String

Simple method missing accessor for reading returned attributes

Returns:

  • (String)

    the raw returned string from the API



50
51
52
# File 'lib/openlive/response.rb', line 50

def method_missing(method_name, *opts, &block)
  body[method_name.to_s] if body.is_a?(Hash)
end

Instance Attribute Details

#responseFaraday::Response (readonly)

Returns:

  • (Faraday::Response)


4
5
6
# File 'lib/openlive/response.rb', line 4

def response
  @response
end

Instance Method Details

#bodyHash, Nil

Parse the response from the server

Returns:

  • (Hash, Nil)


30
31
32
33
34
# File 'lib/openlive/response.rb', line 30

def body
  @body ||= (
    JSON.parse(response.body) if response.body.length > 0
  )
end

#error_messageString

Convenience method for fetching the error message

Returns:

  • (String)


39
40
41
42
43
44
45
# File 'lib/openlive/response.rb', line 39

def error_message
  if !success?
    body
  end
rescue JSON::ParserError => ex
  ex.message
end

#statusInteger

Return the response status

Returns:

  • (Integer)


23
24
25
# File 'lib/openlive/response.rb', line 23

def status
  response.status
end

#success?Truthy

Was the request successful?

Returns:

  • (Truthy)


16
17
18
# File 'lib/openlive/response.rb', line 16

def success?
  response.success?
end