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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, klass) ⇒ WrappedResponse

Returns a new instance of WrappedResponse.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 7

def initialize(response, klass)
  @response = response
  @klass    = klass

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

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



6
7
8
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 6

def response
  @response
end

#resultObject (readonly)

Returns the value of attribute result.



6
7
8
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 6

def result
  @result
end

Instance Method Details

#bodyObject



21
22
23
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 21

def body
  response.body.to_s
end

#eachObject



24
25
26
27
28
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 24

def each
  [*@result].each do |doc|
    yield doc
  end
end


35
36
37
38
39
40
41
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 35

def print
  if success?
    each(&:print)
  else
    puts "Request failed."
  end
end

#statusObject



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

def status
  @response.status.code
end

#success?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/ecoportal/api/common/wrapped_response.rb', line 32

def success?
  @response.success?
end