Module: Dhis2::Api::Findable::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#find(client, id, fields: :all, raw: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dhis2/api/findable.rb', line 12

def find(client, id, fields: :all, raw: false)
  raise Dhis2::PrimaryKeyMissingError if id.nil?

  if id.is_a? Array
    list(client, { filter: "id:in:[#{id.join(',')}]", fields: fields, page_size: id.size }, raw)
  else
    if raw
      client.get(path: "#{resource_name}/#{id}", raw: true)
    else
      new(client, client.get(path: "#{resource_name}/#{id}"))
    end
  end
end

#find_by(client, clauses, fields: :all, raw: false) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/dhis2/api/findable.rb', line 41

def find_by(client, clauses, fields: :all, raw: false)
  list(
    client,
    fields: fields,
    filter: clauses.map { |field, value| "#{field}:eq:#{value}" },
    raw: raw
  ).first
end

#find_paginated(client, ids, fields: :all, batch_size: 100, raw: false) ⇒ Object

batch size is to prevent from sending get requests with uri too long max uri length is around 2000 chars, a dhis id is around 11 chars, 100 is a safe default



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dhis2/api/findable.rb', line 28

def find_paginated(client, ids, fields: :all, batch_size: 100, raw: false)
  Enumerator.new do |yielder|
    loop do
      ids.each_slice(batch_size) do |array|
        find(client, array, fields: fields, raw: raw).map do |item|
          yielder << item
        end
      end
      raise StopIteration
    end
  end
end