Class: GdsApi::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/gds_api/response.rb

Direct Known Subclasses

ContentApi::ListResponse

Instance Method Summary collapse

Constructor Details

#initialize(net_http_response) ⇒ Response

Returns a new instance of Response.



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

def initialize(net_http_response)
  @net_http_response = net_http_response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



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

def method_missing(method)
  if to_hash.has_key?(method.to_s)
    to_ostruct.send(method)
  else
    nil
  end
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


46
# File 'lib/gds_api/response.rb', line 46

def blank?; false; end

#codeObject



24
25
26
27
# File 'lib/gds_api/response.rb', line 24

def code
  # Return an integer code for consistency with HTTPErrorResponse
  @net_http_response.code.to_i
end

#present?Boolean

Returns:

  • (Boolean)


45
# File 'lib/gds_api/response.rb', line 45

def present?; ! blank?; end

#raw_response_bodyObject



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

def raw_response_body
  @net_http_response.body
end

#respond_to_missing?(method, include_private) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/gds_api/response.rb', line 41

def respond_to_missing?(method, include_private)
  to_hash.has_key?(method.to_s)
end

#to_hashObject



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

def to_hash
  @parsed ||= JSON.parse(@net_http_response.body)
end

#to_ostructObject



29
30
31
# File 'lib/gds_api/response.rb', line 29

def to_ostruct
  self.class.build_ostruct_recursively(to_hash)
end