Class: Idcf::Dns::Response

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

Overview

HTTP response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(faraday_response) ⇒ Response

Returns a new instance of Response.

Parameters:

  • faraday_response (Faraday::Response)


19
20
21
22
23
# File 'lib/idcf/dns/response.rb', line 19

def initialize(faraday_response)
  @body    = faraday_response.body
  @headers = faraday_response.headers
  @status  = faraday_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.



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

def body
  @body
end

#headersHash (readonly)

Returns HTTP response headers.

Returns:

  • (Hash)

    HTTP response headers



16
# File 'lib/idcf/dns/response.rb', line 16

attr_reader :body, :headers, :status

#statusObject (readonly)

Returns the value of attribute status.



16
# File 'lib/idcf/dns/response.rb', line 16

attr_reader :body, :headers, :status

Instance Method Details

#countFixnum Also known as: size

Returns the number of resources.

Returns:

  • (Fixnum)

    count of resources.



30
31
32
33
34
35
36
37
38
39
# File 'lib/idcf/dns/response.rb', line 30

def count
  case body
  when Array
    body.size
  when Hash
    body.key?("uuid") ? 1 : 0
  else
    0
  end
end

#messageString

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

Returns:

  • (String)

    API error message



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

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



58
59
60
61
62
63
64
# File 'lib/idcf/dns/response.rb', line 58

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



69
70
71
# File 'lib/idcf/dns/response.rb', line 69

def resources
  body && [*body]
end

#success?Boolean

Returns request success?.

Returns:

  • (Boolean)

    request success?



74
75
76
# File 'lib/idcf/dns/response.rb', line 74

def success?
  status < 400
end

#uuidString

Returns UUID of a resource.

Returns:

  • (String)

    UUID of a resource



79
80
81
# File 'lib/idcf/dns/response.rb', line 79

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