Class: Idcf::Your::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/idcf/your/response.rb

Overview

HTTP response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • faraday_response (Faraday::Response)


21
22
23
24
25
# File 'lib/idcf/your/response.rb', line 21

def initialize(response)
  @body    = response.body
  @headers = response.headers
  @status  = response.status
end

Instance Attribute Details

#bodyHash, ... (readonly)

Returns response body as a hash, an array of hashes or nil.

Returns:

  • (Hash, Array, nil)

    response body as a hash, an array of hashes or nil.



18
19
20
# File 'lib/idcf/your/response.rb', line 18

def body
  @body
end

#headersHash (readonly)

Returns HTTP response headers.

Returns:

  • (Hash)

    HTTP response headers



18
# File 'lib/idcf/your/response.rb', line 18

attr_reader :body, :headers, :status

#statusObject (readonly)

Returns the value of attribute status.



18
# File 'lib/idcf/your/response.rb', line 18

attr_reader :body, :headers, :status

Instance Method Details

#messageString

Returns error message. When request succeed, this returns nil.

Returns:

  • (String)

    API error message



33
34
35
36
37
38
39
# File 'lib/idcf/your/response.rb', line 33

def message
  if success?
    nil
  else
    body ? self["message"] : "Resource not found."
  end
end

#referenceString

Returns error reference. When request succeed, this returns nil.

Returns:

  • (String)

    API error reference



45
46
47
48
49
50
51
# File 'lib/idcf/your/response.rb', line 45

def reference
  if success?
    nil
  else
    body ? self["reference"] : "No reference"
  end
end

#resourcesArray<Hash>

Returns an array of resource hashes.

Returns:

  • (Array<Hash>)

    an array of resource hashes



56
57
58
# File 'lib/idcf/your/response.rb', line 56

def resources
  body && [*body]
end

#success?Boolean

Returns request success?.

Returns:

  • (Boolean)

    request success?



61
62
63
# File 'lib/idcf/your/response.rb', line 61

def success?
  status < 400
end

#uuidString

Returns UUID of a resource.

Returns:

  • (String)

    UUID of a resource



66
67
68
# File 'lib/idcf/your/response.rb', line 66

def uuid
  body.is_a?(Hash) && body.key?("uuid") ? self["uuid"] : nil
end