Module: Dhis2::Api::Listable::ClassMethods

Defined in:
lib/dhis2/api/listable.rb

Constant Summary collapse

PAGER_KEY =
"pager"

Instance Method Summary collapse

Instance Method Details

#fetch_paginated_data(client, params = {}, options = {}) ⇒ Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dhis2/api/listable.rb', line 33

def fetch_paginated_data(client, params = {}, options = {})
  raise InvalidMethodError, "this collection is not paginated" unless paginated
  options = { raw: false, with_pager: false }.merge(options)
  Enumerator.new do |yielder|
    params[:page] ||= 1
    loop do
      results = list(client, params, options[:raw])
      if options[:with_pager]
        results.map { |item| yielder << [item, results.pager] }
      else
        results.map { |item| yielder << item }
      end
      raise StopIteration if results.pager.last_page?
      params[:page] += 1
    end
  end
end

#list(client, options = {}, raw = false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dhis2/api/listable.rb', line 14

def list(client, options = {}, raw = false)
  json_response = client.get(path: resource_name, query_params: format_query_parameters(options), raw: raw)
  if paginated
    if raw
      PaginatedArray.new(
        json_response[resource_name],
        json_response[PAGER_KEY]
      )
    else
      PaginatedArray.new(
        json_response[resource_key].map { |raw_resource| new(client, raw_resource) },
        json_response[PAGER_KEY]
      )
    end
  else
    json_response
  end
end

#paginatedObject



51
52
53
# File 'lib/dhis2/api/listable.rb', line 51

def paginated
  true
end