Class: ForemanApiClient::PagedResponse

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

Overview

like the WillPaginate collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ PagedResponse

per_page, search, sort



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/foreman_api_client/paged_response.rb', line 12

def initialize(json)
  if json.kind_of?(Hash) && json["results"]
    @results = json["results"]
    @total   = json["total"].to_i
    @page    = json["page"]
  else
    @results = json.kind_of?(Array) ? json : Array[json]
    @total = json.size
    @page  = 1
  end
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



7
8
9
# File 'lib/foreman_api_client/paged_response.rb', line 7

def page
  @page
end

#resultsObject

Returns the value of attribute results.



9
10
11
# File 'lib/foreman_api_client/paged_response.rb', line 9

def results
  @results
end

#totalObject

Returns the value of attribute total.



8
9
10
# File 'lib/foreman_api_client/paged_response.rb', line 8

def total
  @total
end

Instance Method Details

#==(other) ⇒ Object



32
33
34
# File 'lib/foreman_api_client/paged_response.rb', line 32

def ==(other)
  results == other.results
end

#denormalizeObject



36
37
38
39
40
41
42
43
44
# File 'lib/foreman_api_client/paged_response.rb', line 36

def denormalize
  self.class.new(
    results.collect do |record|
      ancestors(results, record["ancestry"]).each_with_object({}) do |ancestor, h|
        h.merge!(ancestor.select { |_n, v| !v.nil? && v != "" })
      end.merge!(record.select { |_n, v| !v.nil? && v != "" })
    end
  )
end

#map!(&block) ⇒ Object

modify the structure inline



27
28
29
30
# File 'lib/foreman_api_client/paged_response.rb', line 27

def map!(&block)
  self.results = results.map(&block)
  self
end