Class: Ruhue::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • response (HTTPI::Response)


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

def initialize(response)
  @response = response
  @data = JSON.load(response.body)
end

Instance Attribute Details

#dataArray, Hash (readonly)

Returns data.

Returns:

  • (Array, Hash)

    data



12
13
14
# File 'lib/ruhue/response.rb', line 12

def data
  @data
end

#responseString (readonly)

Returns body.

Returns:

  • (String)

    body



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

def response
  @response
end

Instance Method Details

#error?Boolean

Returns true if the response is an error.

Returns:

  • (Boolean)

    true if the response is an error.



15
16
17
# File 'lib/ruhue/response.rb', line 15

def error?
  data.is_a?(Array) and data.any? { |hash| hash.has_key?("error") }
end

#error_messagesArray<String>?

Returns array of error messages and their address, nil if no error.

Returns:

  • (Array<String>, nil)

    array of error messages and their address, nil if no error.



20
21
22
# File 'lib/ruhue/response.rb', line 20

def error_messages
  data.map { |hash| "#{hash["error"]["address"]}: #{hash["error"]["description"]}" } if error?
end