Class: Ecoportal::API::Common::Content::WrappedResponse

Inherits:
Common::WrappedResponse
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ecoportal/api/common/content/wrapped_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, klass, key: nil) ⇒ WrappedResponse

Returns a new instance of WrappedResponse.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ecoportal/api/common/content/wrapped_response.rb', line 10

def initialize(response, klass, key: nil)
  @response = response
  @klass    = klass
  @key      = key

  if @response.success?
    @result =
      if data.is_a?(Array)
        data.map do |doc|
          @klass.new(doc)
        end
      else
        @klass.new(data)
      end
  end
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



8
9
10
# File 'lib/ecoportal/api/common/content/wrapped_response.rb', line 8

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'lib/ecoportal/api/common/content/wrapped_response.rb', line 8

def result
  @result
end

Instance Method Details

#bodyObject



34
35
36
# File 'lib/ecoportal/api/common/content/wrapped_response.rb', line 34

def body
  data.to_s
end

#dataObject



27
28
29
30
31
32
# File 'lib/ecoportal/api/common/content/wrapped_response.rb', line 27

def data
  return @data if instance_variable_defined?(:@data)
  @data = (response.body || {})["data"]
  @data = @data[key] if @key && @data
  @data
end