dhis2 2.3.8 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/PULL_REQUEST_TEMPLATE.md +11 -0
- data/Gemfile +2 -1
- data/README.md +208 -68
- data/Rakefile +4 -2
- data/bin/console +1 -0
- data/changelog.md +7 -0
- data/dhis2.gemspec +8 -8
- data/lib/dhis2/api/base.rb +5 -94
- data/lib/dhis2/api/bulk_creatable.rb +28 -0
- data/lib/dhis2/api/constants.rb +13 -0
- data/lib/dhis2/api/creatable.rb +47 -0
- data/lib/dhis2/api/deletable.rb +12 -0
- data/lib/dhis2/api/event_creation_status.rb +58 -0
- data/lib/dhis2/api/findable.rb +52 -0
- data/lib/dhis2/api/import_summary.rb +61 -0
- data/lib/dhis2/api/listable.rb +78 -0
- data/lib/dhis2/api/query_parameters_formatter.rb +50 -0
- data/lib/dhis2/api/shared/analytic.rb +20 -0
- data/lib/dhis2/api/shared/category_combo.rb +29 -0
- data/lib/dhis2/api/shared/category_option_combo.rb +23 -0
- data/lib/dhis2/api/shared/constants.rb +35 -0
- data/lib/dhis2/api/shared/data_element_group.rb +25 -0
- data/lib/dhis2/api/shared/data_set.rb +22 -0
- data/lib/dhis2/api/shared/data_value.rb +26 -0
- data/lib/dhis2/api/shared/data_value_set.rb +58 -0
- data/lib/dhis2/api/shared/event.rb +54 -0
- data/lib/dhis2/api/shared/organisation_unit.rb +17 -0
- data/lib/dhis2/api/shared/organisation_unit_group.rb +21 -0
- data/lib/dhis2/api/shared/report_tables.rb +32 -0
- data/lib/dhis2/api/shared/resource_table.rb +19 -0
- data/lib/dhis2/api/shared/save_validator.rb +47 -0
- data/lib/dhis2/api/shared/system_info.rb +19 -0
- data/lib/dhis2/api/updatable.rb +33 -0
- data/lib/dhis2/api/version224/analytic.rb +10 -0
- data/lib/dhis2/api/version224/attribute.rb +23 -0
- data/lib/dhis2/api/version224/category_combo.rb +25 -0
- data/lib/dhis2/api/version224/category_option_combo.rb +23 -0
- data/lib/dhis2/api/version224/constants.rb +22 -0
- data/lib/dhis2/api/version224/data_element.rb +45 -0
- data/lib/dhis2/api/version224/data_element_group.rb +22 -0
- data/lib/dhis2/api/version224/data_set.rb +53 -0
- data/lib/dhis2/api/version224/data_value.rb +11 -0
- data/lib/dhis2/api/version224/data_value_set.rb +25 -0
- data/lib/dhis2/api/version224/event.rb +31 -0
- data/lib/dhis2/api/version224/index.rb +27 -0
- data/lib/dhis2/api/version224/indicator.rb +24 -0
- data/lib/dhis2/api/version224/indicator_group.rb +20 -0
- data/lib/dhis2/api/version224/indicator_type.rb +20 -0
- data/lib/dhis2/api/version224/organisation_unit.rb +24 -0
- data/lib/dhis2/api/version224/organisation_unit_group.rb +22 -0
- data/lib/dhis2/api/version224/organisation_unit_group_set.rb +20 -0
- data/lib/dhis2/api/version224/organisation_unit_level.rb +21 -0
- data/lib/dhis2/api/version224/program.rb +26 -0
- data/lib/dhis2/api/version224/report.rb +20 -0
- data/lib/dhis2/api/version224/report_table.rb +21 -0
- data/lib/dhis2/api/version224/resource_table.rb +11 -0
- data/lib/dhis2/api/version224/save_validator.rb +48 -0
- data/lib/dhis2/api/version224/system_info.rb +11 -0
- data/lib/dhis2/api/version224/user.rb +15 -0
- data/lib/dhis2/api/version225/analytic.rb +10 -0
- data/lib/dhis2/api/version225/attribute.rb +23 -0
- data/lib/dhis2/api/version225/category_combo.rb +25 -0
- data/lib/dhis2/api/version225/category_option_combo.rb +23 -0
- data/lib/dhis2/api/version225/constants.rb +22 -0
- data/lib/dhis2/api/version225/data_element.rb +45 -0
- data/lib/dhis2/api/version225/data_element_group.rb +22 -0
- data/lib/dhis2/api/version225/data_set.rb +35 -0
- data/lib/dhis2/api/version225/data_value.rb +11 -0
- data/lib/dhis2/api/version225/data_value_set.rb +25 -0
- data/lib/dhis2/api/version225/event.rb +31 -0
- data/lib/dhis2/api/version225/index.rb +26 -0
- data/lib/dhis2/api/version225/indicator.rb +24 -0
- data/lib/dhis2/api/version225/indicator_group.rb +20 -0
- data/lib/dhis2/api/version225/indicator_type.rb +20 -0
- data/lib/dhis2/api/version225/organisation_unit.rb +24 -0
- data/lib/dhis2/api/version225/organisation_unit_group.rb +22 -0
- data/lib/dhis2/api/version225/organisation_unit_group_set.rb +20 -0
- data/lib/dhis2/api/version225/organisation_unit_level.rb +21 -0
- data/lib/dhis2/api/version225/program.rb +26 -0
- data/lib/dhis2/api/version225/report.rb +20 -0
- data/lib/dhis2/api/version225/report_table.rb +21 -0
- data/lib/dhis2/api/version225/resource_table.rb +11 -0
- data/lib/dhis2/api/version225/system_info.rb +11 -0
- data/lib/dhis2/api/version225/user.rb +15 -0
- data/lib/dhis2/api/version226/analytic.rb +10 -0
- data/lib/dhis2/api/version226/attribute.rb +23 -0
- data/lib/dhis2/api/version226/category_combo.rb +25 -0
- data/lib/dhis2/api/version226/category_option_combo.rb +23 -0
- data/lib/dhis2/api/version226/constants.rb +22 -0
- data/lib/dhis2/api/version226/data_element.rb +42 -0
- data/lib/dhis2/api/version226/data_element_group.rb +22 -0
- data/lib/dhis2/api/version226/data_set.rb +32 -0
- data/lib/dhis2/api/version226/data_value.rb +11 -0
- data/lib/dhis2/api/version226/data_value_set.rb +25 -0
- data/lib/dhis2/api/version226/event.rb +31 -0
- data/lib/dhis2/api/version226/index.rb +26 -0
- data/lib/dhis2/api/version226/indicator.rb +24 -0
- data/lib/dhis2/api/version226/indicator_group.rb +20 -0
- data/lib/dhis2/api/version226/indicator_type.rb +20 -0
- data/lib/dhis2/api/version226/organisation_unit.rb +24 -0
- data/lib/dhis2/api/version226/organisation_unit_group.rb +22 -0
- data/lib/dhis2/api/version226/organisation_unit_group_set.rb +20 -0
- data/lib/dhis2/api/version226/organisation_unit_level.rb +21 -0
- data/lib/dhis2/api/version226/program.rb +24 -0
- data/lib/dhis2/api/version226/report.rb +20 -0
- data/lib/dhis2/api/version226/report_table.rb +21 -0
- data/lib/dhis2/api/version226/resource_table.rb +11 -0
- data/lib/dhis2/api/version226/system_info.rb +11 -0
- data/lib/dhis2/api/version226/user.rb +15 -0
- data/lib/dhis2/api/version227/analytic.rb +10 -0
- data/lib/dhis2/api/version227/attribute.rb +23 -0
- data/lib/dhis2/api/version227/category_combo.rb +25 -0
- data/lib/dhis2/api/version227/category_option_combo.rb +23 -0
- data/lib/dhis2/api/version227/constants.rb +22 -0
- data/lib/dhis2/api/version227/data_element.rb +42 -0
- data/lib/dhis2/api/version227/data_element_group.rb +22 -0
- data/lib/dhis2/api/version227/data_set.rb +32 -0
- data/lib/dhis2/api/version227/data_value.rb +11 -0
- data/lib/dhis2/api/version227/data_value_set.rb +25 -0
- data/lib/dhis2/api/version227/event.rb +31 -0
- data/lib/dhis2/api/version227/index.rb +26 -0
- data/lib/dhis2/api/version227/indicator.rb +24 -0
- data/lib/dhis2/api/version227/indicator_group.rb +20 -0
- data/lib/dhis2/api/version227/indicator_type.rb +20 -0
- data/lib/dhis2/api/version227/organisation_unit.rb +24 -0
- data/lib/dhis2/api/version227/organisation_unit_group.rb +22 -0
- data/lib/dhis2/api/version227/organisation_unit_group_set.rb +20 -0
- data/lib/dhis2/api/version227/organisation_unit_level.rb +21 -0
- data/lib/dhis2/api/version227/program.rb +24 -0
- data/lib/dhis2/api/version227/report.rb +20 -0
- data/lib/dhis2/api/version227/report_table.rb +21 -0
- data/lib/dhis2/api/version227/resource_table.rb +11 -0
- data/lib/dhis2/api/version227/system_info.rb +11 -0
- data/lib/dhis2/api/version227/user.rb +15 -0
- data/lib/dhis2/api/version228/analytic.rb +10 -0
- data/lib/dhis2/api/version228/attribute.rb +23 -0
- data/lib/dhis2/api/version228/category_combo.rb +25 -0
- data/lib/dhis2/api/version228/category_option_combo.rb +23 -0
- data/lib/dhis2/api/version228/constants.rb +22 -0
- data/lib/dhis2/api/version228/data_element.rb +41 -0
- data/lib/dhis2/api/version228/data_element_group.rb +22 -0
- data/lib/dhis2/api/version228/data_set.rb +32 -0
- data/lib/dhis2/api/version228/data_value.rb +11 -0
- data/lib/dhis2/api/version228/data_value_set.rb +25 -0
- data/lib/dhis2/api/version228/event.rb +31 -0
- data/lib/dhis2/api/version228/index.rb +26 -0
- data/lib/dhis2/api/version228/indicator.rb +24 -0
- data/lib/dhis2/api/version228/indicator_group.rb +20 -0
- data/lib/dhis2/api/version228/indicator_type.rb +20 -0
- data/lib/dhis2/api/version228/organisation_unit.rb +24 -0
- data/lib/dhis2/api/version228/organisation_unit_group.rb +22 -0
- data/lib/dhis2/api/version228/organisation_unit_group_set.rb +20 -0
- data/lib/dhis2/api/version228/organisation_unit_level.rb +21 -0
- data/lib/dhis2/api/version228/program.rb +24 -0
- data/lib/dhis2/api/version228/report.rb +20 -0
- data/lib/dhis2/api/version228/report_table.rb +21 -0
- data/lib/dhis2/api/version228/resource_table.rb +11 -0
- data/lib/dhis2/api/version228/system_info.rb +11 -0
- data/lib/dhis2/api/version228/user.rb +15 -0
- data/lib/dhis2/case.rb +46 -0
- data/lib/dhis2/classes.rb +44 -0
- data/lib/dhis2/client.rb +152 -89
- data/lib/dhis2/collection_wrapper.rb +13 -3
- data/lib/dhis2/configuration.rb +55 -1
- data/lib/dhis2/error.rb +19 -0
- data/lib/dhis2/pager.rb +8 -3
- data/lib/dhis2/version.rb +1 -1
- data/lib/dhis2.rb +51 -42
- metadata +168 -50
- data/lib/dhis2/api/analytic.rb +0 -20
- data/lib/dhis2/api/attribute.rb +0 -8
- data/lib/dhis2/api/category_combo.rb +0 -28
- data/lib/dhis2/api/category_option_combo.rb +0 -8
- data/lib/dhis2/api/data_element.rb +0 -34
- data/lib/dhis2/api/data_element_group.rb +0 -32
- data/lib/dhis2/api/data_set.rb +0 -33
- data/lib/dhis2/api/data_value.rb +0 -15
- data/lib/dhis2/api/data_value_set.rb +0 -71
- data/lib/dhis2/api/event.rb +0 -36
- data/lib/dhis2/api/indicator.rb +0 -8
- data/lib/dhis2/api/indicator_group.rb +0 -8
- data/lib/dhis2/api/organisation_unit.rb +0 -66
- data/lib/dhis2/api/organisation_unit_group.rb +0 -38
- data/lib/dhis2/api/organisation_unit_group_set.rb +0 -6
- data/lib/dhis2/api/organisation_unit_level.rb +0 -8
- data/lib/dhis2/api/program.rb +0 -8
- data/lib/dhis2/api/report.rb +0 -8
- data/lib/dhis2/api/report_table.rb +0 -8
- data/lib/dhis2/api/resource_table.rb +0 -14
- data/lib/dhis2/api/system_info.rb +0 -13
- data/lib/dhis2/api/user.rb +0 -8
- data/lib/dhis2/import_error.rb +0 -6
- data/lib/dhis2/status.rb +0 -42
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Creatable
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
def create(client, args, raw_input = false)
|
|
12
|
+
if raw_input
|
|
13
|
+
client.post(path: resource_name, payload: args, raw_input: true).tap do |response|
|
|
14
|
+
validate_instance_creation(response)
|
|
15
|
+
end
|
|
16
|
+
else
|
|
17
|
+
args = creation_args(args)
|
|
18
|
+
with_valid_args(args) do
|
|
19
|
+
response = client.post(path: resource_name, payload: args)
|
|
20
|
+
validate_instance_creation(response)
|
|
21
|
+
new(client, args.merge(id: created_instance_id(response)))
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def creation_defaults(_args)
|
|
29
|
+
{}
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def creation_args(args)
|
|
33
|
+
creation_defaults(args).merge(args).keep_if { |_, v| !v.nil? }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def with_valid_args(args)
|
|
37
|
+
validator = self::Schema.call(args)
|
|
38
|
+
if validator.success?
|
|
39
|
+
yield
|
|
40
|
+
else
|
|
41
|
+
raise Dhis2::CreationError, "Validation Error: #{validator.messages}"
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
class EventCreationStatus
|
|
6
|
+
def initialize(hash)
|
|
7
|
+
@hash = hash
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def creation_success?
|
|
11
|
+
base_success? && only_updates_and_imports?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def update_success?
|
|
15
|
+
base_success? && only_updates_and_imports?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def bulk_success?
|
|
19
|
+
base_success?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def imported_count
|
|
23
|
+
response["imported"]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def updated_count
|
|
27
|
+
response["updated"]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def ignored_count
|
|
31
|
+
response["ignored"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def import_summaries
|
|
35
|
+
@import_summaries ||= response["import_summaries"].map do |hash|
|
|
36
|
+
Dhis2::Api::ImportSummary.new(hash)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
attr_reader :hash
|
|
43
|
+
|
|
44
|
+
def response
|
|
45
|
+
hash["response"]
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def only_updates_and_imports?
|
|
49
|
+
ignored_count == 0 && (updated_count > 0 || imported_count > 0)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def base_success?
|
|
53
|
+
response["response_type"] == "ImportSummaries" &&
|
|
54
|
+
response["status"] == "SUCCESS"
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Findable
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
# https://docs.dhis2.org/2.25/en/developer/html/webapi_metadata_object_filter.html
|
|
12
|
+
def find(client, id, fields: :all, raw: false)
|
|
13
|
+
raise Dhis2::PrimaryKeyMissingError if id.nil?
|
|
14
|
+
|
|
15
|
+
if id.is_a? Array
|
|
16
|
+
list(client, { filter: "id:in:[#{id.join(',')}]", fields: fields, page_size: id.size }, raw)
|
|
17
|
+
else
|
|
18
|
+
if raw
|
|
19
|
+
client.get(path: "#{resource_name}/#{id}", raw: true)
|
|
20
|
+
else
|
|
21
|
+
new(client, client.get(path: "#{resource_name}/#{id}"))
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# batch size is to prevent from sending get requests with uri too long
|
|
27
|
+
# max uri length is around 2000 chars, a dhis id is around 11 chars, 100 is a safe default
|
|
28
|
+
def find_paginated(client, ids, fields: :all, batch_size: 100, raw: false)
|
|
29
|
+
Enumerator.new do |yielder|
|
|
30
|
+
loop do
|
|
31
|
+
ids.each_slice(batch_size) do |array|
|
|
32
|
+
find(client, array, fields: fields, raw: raw).map do |item|
|
|
33
|
+
yielder << item
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
raise StopIteration
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def find_by(client, clauses, fields: :all, raw: false)
|
|
42
|
+
list(
|
|
43
|
+
client,
|
|
44
|
+
fields: fields,
|
|
45
|
+
filter: clauses.map { |field, value| "#{field}:eq:#{value}" },
|
|
46
|
+
raw: raw
|
|
47
|
+
).first
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
class ImportSummary
|
|
6
|
+
def initialize(hash)
|
|
7
|
+
@hash = hash
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def reference
|
|
11
|
+
hash["reference"]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def creation_success?
|
|
15
|
+
base_success? && only_updates_and_imports?
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def update_success?
|
|
19
|
+
base_success? && only_updates_and_imports?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def bulk_success?
|
|
23
|
+
base_success?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def imported_count
|
|
27
|
+
import_count["imported"]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def updated_count
|
|
31
|
+
import_count["updated"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def ignored_count
|
|
35
|
+
import_count["ignored"]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def raw_status
|
|
39
|
+
hash
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
attr_reader :hash
|
|
45
|
+
|
|
46
|
+
def only_updates_and_imports?
|
|
47
|
+
ignored_count == 0 && (updated_count > 0 || imported_count > 0)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def import_count
|
|
51
|
+
hash["import_count"]
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def base_success?
|
|
55
|
+
%w(ImportSummary ImportTypeSummary).include?(hash["response_type"]) &&
|
|
56
|
+
hash["status"] == "SUCCESS" &&
|
|
57
|
+
import_count
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Listable
|
|
6
|
+
def self.included(base)
|
|
7
|
+
base.extend(ClassMethods)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
module ClassMethods
|
|
11
|
+
|
|
12
|
+
PAGER_KEY = "pager"
|
|
13
|
+
|
|
14
|
+
def list(client, options = {}, raw = false)
|
|
15
|
+
json_response = client.get(path: resource_name, query_params: format_query_parameters(options), raw: raw)
|
|
16
|
+
if paginated
|
|
17
|
+
if raw
|
|
18
|
+
PaginatedArray.new(
|
|
19
|
+
json_response[resource_name],
|
|
20
|
+
json_response[PAGER_KEY]
|
|
21
|
+
)
|
|
22
|
+
else
|
|
23
|
+
PaginatedArray.new(
|
|
24
|
+
json_response[resource_key].map { |raw_resource| new(client, raw_resource) },
|
|
25
|
+
json_response[PAGER_KEY]
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
json_response
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def fetch_paginated_data(client, params = {}, options = {})
|
|
34
|
+
raise InvalidMethodError, "this collection is not paginated" unless paginated
|
|
35
|
+
options = { raw: false, with_pager: false }.merge(options)
|
|
36
|
+
Enumerator.new do |yielder|
|
|
37
|
+
params[:page] ||= 1
|
|
38
|
+
loop do
|
|
39
|
+
results = list(client, params, options[:raw])
|
|
40
|
+
if options[:with_pager]
|
|
41
|
+
results.map { |item| yielder << [item, results.pager] }
|
|
42
|
+
else
|
|
43
|
+
results.map { |item| yielder << item }
|
|
44
|
+
end
|
|
45
|
+
raise StopIteration if results.pager.last_page?
|
|
46
|
+
params[:page] += 1
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def paginated
|
|
52
|
+
true
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
private
|
|
56
|
+
|
|
57
|
+
def format_query_parameters(options)
|
|
58
|
+
::Dhis2::QueryParametersFormatter.new(
|
|
59
|
+
options: options,
|
|
60
|
+
query_parameters: query_parameters
|
|
61
|
+
).call
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def query_parameters
|
|
65
|
+
default_query_parameters + additional_query_parameters
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def additional_query_parameters
|
|
69
|
+
[]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def default_query_parameters
|
|
73
|
+
[:page, :page_size, :root_junction]
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
class QueryParametersFormatter
|
|
5
|
+
def initialize(options:, query_parameters:)
|
|
6
|
+
@options = options
|
|
7
|
+
@query_parameters = query_parameters
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def call
|
|
11
|
+
RestClient::ParamsArray.new(formatted_options)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
attr_reader :options, :query_parameters
|
|
17
|
+
|
|
18
|
+
def formatted_options
|
|
19
|
+
[].tap do |params|
|
|
20
|
+
query_parameters.each do |param|
|
|
21
|
+
Array(options[param]).each do |value|
|
|
22
|
+
params.push([::Dhis2::Case.camelize(param.to_s, false), value])
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
params.push([:fields, format_fields(options[:fields])]) if options[:fields]
|
|
26
|
+
params.concat(format_filter(options[:filter])) if options[:filter]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def format_fields(fields)
|
|
31
|
+
if fields.respond_to?(:join)
|
|
32
|
+
fields.join(",")
|
|
33
|
+
elsif fields == :all
|
|
34
|
+
":all"
|
|
35
|
+
else
|
|
36
|
+
fields
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def format_filter(filter)
|
|
41
|
+
if filter.respond_to?(:map)
|
|
42
|
+
filter.map do |subfilter|
|
|
43
|
+
[:filter, subfilter]
|
|
44
|
+
end
|
|
45
|
+
else
|
|
46
|
+
[[:filter, filter]]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
class Analytic
|
|
7
|
+
def self.list(client, periods:, organisation_units:, data_elements: nil, filter: nil)
|
|
8
|
+
params = [
|
|
9
|
+
[:dimension, "ou:#{organisation_units}"],
|
|
10
|
+
[:dimension, "pe:#{periods}"]
|
|
11
|
+
]
|
|
12
|
+
params << [:dimension, "dx:#{data_elements}"] if data_elements
|
|
13
|
+
params << [:filter, filter.to_s] if filter
|
|
14
|
+
|
|
15
|
+
client.get(path: "analytics", query_params: RestClient::ParamsArray.new(params))
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module CategoryCombo
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def default
|
|
12
|
+
self.class.default(client)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module ClassMethods
|
|
16
|
+
def default(client)
|
|
17
|
+
find_by(client, name: "default")
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def creation_defaults(_args)
|
|
23
|
+
{ data_dimension_type: "DISAGGREGATION" }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module CategoryOptionCombo
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def default
|
|
12
|
+
self.class.default(client)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module ClassMethods
|
|
16
|
+
def default(client)
|
|
17
|
+
find_by(client, name: "default")
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module Constants
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
def data_dimension_types
|
|
13
|
+
::Dhis2::Api::Constants::DATA_DIMENSION_TYPES
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def domain_types
|
|
17
|
+
::Dhis2::Api::Constants::DOMAIN_TYPES
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def feature_types
|
|
21
|
+
::Dhis2::Api::Constants::DATA_DIMENSION_TYPES
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def period_types
|
|
25
|
+
::Dhis2::Api::Constants::PERIOD_TYPES
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def program_types
|
|
29
|
+
::Dhis2::Api::Constants::PROGRAM_TYPES
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module DataElementGroup
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def data_element_ids
|
|
12
|
+
data_elements.map { |elt| elt["id"] }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
module ClassMethods
|
|
16
|
+
def creation_defaults(args)
|
|
17
|
+
{
|
|
18
|
+
code: args[:short_name]
|
|
19
|
+
}
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module DataSet
|
|
7
|
+
def add_data_elements(new_data_element_ids)
|
|
8
|
+
(new_data_element_ids - data_element_ids).tap do |additions|
|
|
9
|
+
(new_data_element_ids - data_element_ids).each do |data_element_id|
|
|
10
|
+
data_set_elements.push("data_element" => { "id" => data_element_id })
|
|
11
|
+
end
|
|
12
|
+
update if additions.any?
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def data_element_ids
|
|
17
|
+
data_set_elements.map { |elt| elt["data_element"]["id"] }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module DataValue
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
def find(client, period:, organisation_unit:, data_element:)
|
|
13
|
+
client.get(
|
|
14
|
+
path: resource_name,
|
|
15
|
+
query_params: {
|
|
16
|
+
pe: period,
|
|
17
|
+
ou: organisation_unit,
|
|
18
|
+
de: data_element
|
|
19
|
+
}
|
|
20
|
+
).first
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module DataValueSet
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
module ClassMethods
|
|
12
|
+
def list(client, options = {}, raw = false)
|
|
13
|
+
response = super(client, options, raw)
|
|
14
|
+
if raw
|
|
15
|
+
response["dataValues"]
|
|
16
|
+
else
|
|
17
|
+
response["data_values"].map do |elt|
|
|
18
|
+
OpenStruct.new(elt)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def bulk_create(client, args, raw_input = false)
|
|
24
|
+
response = client.post(path: resource_name, payload: args, raw_input: raw_input)
|
|
25
|
+
::Dhis2::Api::ImportSummary.new(response).tap do |summary|
|
|
26
|
+
unless summary.bulk_success?
|
|
27
|
+
exception = Dhis2::BulkCreationError.new("Didnt create bulk of data properly.\n Response: #{response.to_json}")
|
|
28
|
+
exception.import_summary = summary
|
|
29
|
+
raise exception
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def instance_creation_success?(response)
|
|
37
|
+
Dhis2::Api::ImportSummary.new(response).creation_success?
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def created_instance_id(_response)
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def paginated
|
|
45
|
+
false
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def additional_query_parameters
|
|
49
|
+
[
|
|
50
|
+
:data_set, :data_element_group, :period, :start_date, :end_date,
|
|
51
|
+
:org_unit, :children, :org_unit_group
|
|
52
|
+
]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module Event
|
|
7
|
+
def self.included(base)
|
|
8
|
+
base.extend(ClassMethods)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def id
|
|
12
|
+
# we use super because id is defined on creation
|
|
13
|
+
super || event
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module ClassMethods
|
|
17
|
+
def list(client, options = {}, raw = false)
|
|
18
|
+
raise InvalidRequestError, new(list_error_message) if invalid_list_arguments?(options)
|
|
19
|
+
super
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def additional_query_parameters
|
|
25
|
+
[:program, :org_unit, :tracked_entity_instance]
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def instance_creation_success?(response)
|
|
29
|
+
response["status"] == "OK" &&
|
|
30
|
+
response["response"] &&
|
|
31
|
+
response["response"]["status"] == "SUCCESS" &&
|
|
32
|
+
response["response"]["imported"] == 1 &&
|
|
33
|
+
response["response"]["import_summaries"]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def created_instance_id(response)
|
|
37
|
+
response["response"]["import_summaries"][0]["reference"]
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def invalid_list_arguments?(options)
|
|
41
|
+
%i(org_unit program tracked_entity_instance event).all? do |arg|
|
|
42
|
+
options[arg].nil?
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def list_error_message
|
|
47
|
+
"At least one of the following query parameters are required:" \
|
|
48
|
+
"org_unit, program, tracked_entity_instance or event."
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dhis2
|
|
4
|
+
module Api
|
|
5
|
+
module Shared
|
|
6
|
+
module OrganisationUnit
|
|
7
|
+
def parent_id
|
|
8
|
+
parent ? parent["id"] : nil
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def children_ids
|
|
12
|
+
children ? children.map { |elt| elt["id"] } : []
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|