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

Inherits:
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

rubocop:disable Lint/MissingSuper



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) # rubocop:disable Lint/MissingSuper
  @response = response
  @klass    = klass
  @key      = key

  return unless @response.success?

  @result =
    if data.is_a?(Array)
      data.map do |doc|
        @klass.new(doc)
      end
    else
      @klass.new(data)
    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



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

def body
  data.to_s
end

#dataObject



27
28
29
30
31
32
33
# 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