class Dhis2::Client

Constants

API
EMPTY_RESPONSES
TAB

Attributes

version[R]

Public Class Methods

new(options) click to toggle source
# File lib/dhis2/client.rb, line 7
def initialize(options)
  @base_url   = options.fetch(:url)
  @version    = options.fetch(:version)
  @verify_ssl = options.fetch(:verify_ssl, OpenSSL::SSL::VERIFY_PEER)
  @timeout    = options.fetch(:timeout, 120)
  @debug      = options.fetch(:debug, false)
end
uri(base_url, path) click to toggle source
# File lib/dhis2/client.rb, line 160
def self.uri(base_url, path)
  File.join(base_url, API, path)
end

Public Instance Methods

analytics() click to toggle source
# File lib/dhis2/client.rb, line 35
def analytics
  @analytics ||= CollectionWrapper.new("Analytic", self)
end
attributes() click to toggle source
# File lib/dhis2/client.rb, line 39
def attributes
  @attributes ||= CollectionWrapper.new("Attribute", self)
end
can_connect?() click to toggle source
# File lib/dhis2/client.rb, line 151
def can_connect?
  system_infos.get
  true
rescue ::Dhis2::Error
  false
rescue StandardError
  false
end
categories() click to toggle source
# File lib/dhis2/client.rb, line 43
def categories
  @categories ||= CollectionWrapper.new("Category", self)
end
category_combos() click to toggle source
# File lib/dhis2/client.rb, line 51
def category_combos
  @category_combos ||= CollectionWrapper.new("CategoryCombo", self)
end
category_option_combos() click to toggle source
# File lib/dhis2/client.rb, line 55
def category_option_combos
  @category_option_combos ||= CollectionWrapper.new("CategoryOptionCombo", self)
end
category_options() click to toggle source
# File lib/dhis2/client.rb, line 47
def category_options
  @category_options ||= CollectionWrapper.new("CategoryOption", self)
end
complete_data_set_registrations() click to toggle source
# File lib/dhis2/client.rb, line 59
def complete_data_set_registrations
  @complete_data_set_registrations ||= CollectionWrapper.new("CompleteDataSetRegistration", self)
end
data_element_groups() click to toggle source
# File lib/dhis2/client.rb, line 67
def data_element_groups
  @data_element_groups ||= CollectionWrapper.new("DataElementGroup", self)
end
data_elements() click to toggle source
# File lib/dhis2/client.rb, line 63
def data_elements
  @data_elements ||= CollectionWrapper.new("DataElement", self)
end
data_sets() click to toggle source
# File lib/dhis2/client.rb, line 71
def data_sets
  @data_sets ||= CollectionWrapper.new("DataSet", self)
end
data_value_sets() click to toggle source
# File lib/dhis2/client.rb, line 79
def data_value_sets
  @data_value_sets ||= CollectionWrapper.new("DataValueSet", self)
end
data_values() click to toggle source
# File lib/dhis2/client.rb, line 75
def data_values
  @data_values ||= CollectionWrapper.new("DataValue", self)
end
delete(path:, query_params: {}, raw: false) click to toggle source
# File lib/dhis2/client.rb, line 23
def delete(path:, query_params: {}, raw: false)
  execute(method_name: :delete, url: uri(path), query_params: query_params, raw: raw)
end
events() click to toggle source
# File lib/dhis2/client.rb, line 83
def events
  @events ||= CollectionWrapper.new("Event", self)
end
get(path:, query_params: {}, raw: false) click to toggle source
# File lib/dhis2/client.rb, line 19
def get(path:, query_params: {}, raw: false)
  execute(method_name: :get, url: uri(path), query_params: query_params, raw: raw)
end
indicator_groups() click to toggle source
# File lib/dhis2/client.rb, line 91
def indicator_groups
  @indicator_groups ||= CollectionWrapper.new("IndicatorGroup", self)
end
indicator_types() click to toggle source
# File lib/dhis2/client.rb, line 95
def indicator_types
  @indicator_types ||= CollectionWrapper.new("IndicatorType", self)
end
indicators() click to toggle source
# File lib/dhis2/client.rb, line 87
def indicators
  @indicators ||= CollectionWrapper.new("Indicator", self)
end
legend_sets() click to toggle source
# File lib/dhis2/client.rb, line 99
def legend_sets
  @legend_sets ||= CollectionWrapper.new("LegendSet", self)
end
organisation_unit_group_sets() click to toggle source
# File lib/dhis2/client.rb, line 111
def organisation_unit_group_sets
  @organisation_unit_group_sets ||= CollectionWrapper.new("OrganisationUnitGroupSet", self)
end
organisation_unit_groups() click to toggle source
# File lib/dhis2/client.rb, line 107
def organisation_unit_groups
  @organisation_unit_groups ||= CollectionWrapper.new("OrganisationUnitGroup", self)
end
organisation_unit_levels() click to toggle source
# File lib/dhis2/client.rb, line 115
def organisation_unit_levels
  @organisation_unit_levels ||= CollectionWrapper.new("OrganisationUnitLevel", self)
end
organisation_units() click to toggle source
# File lib/dhis2/client.rb, line 103
def organisation_units
  @organisation_units ||= CollectionWrapper.new("OrganisationUnit", self)
end
patch(path:, payload:, query_params: {}, raw: false) click to toggle source
# File lib/dhis2/client.rb, line 31
def patch(path:, payload:, query_params: {}, raw: false)
  execute(method_name: :patch, url: uri(path), query_params: query_params, payload: payload, raw: raw)
end
post(path:, payload: nil, query_params: {}, raw: false, raw_input: false) click to toggle source
# File lib/dhis2/client.rb, line 15
def post(path:, payload: nil, query_params: {}, raw: false, raw_input: false)
  execute(method_name: :post, url: uri(path), query_params: query_params, payload: payload, raw: raw, raw_input: raw_input)
end
program_indicator_groups() click to toggle source
# File lib/dhis2/client.rb, line 127
def program_indicator_groups
  @program_indicator_groups ||= CollectionWrapper.new("ProgramIndicatorGroup", self)
end
program_indicators() click to toggle source
# File lib/dhis2/client.rb, line 123
def program_indicators
  @program_indicators ||= CollectionWrapper.new("ProgramIndicator", self)
end
programs() click to toggle source
# File lib/dhis2/client.rb, line 119
def programs
  @programs ||= CollectionWrapper.new("Program", self)
end
put(path:, payload:, query_params: {}, raw: false) click to toggle source
# File lib/dhis2/client.rb, line 27
def put(path:, payload:, query_params: {}, raw: false)
  execute(method_name: :put, url: uri(path), query_params: query_params, payload: payload, raw: raw)
end
report_tables() click to toggle source
# File lib/dhis2/client.rb, line 135
def report_tables
  @report_tables ||= CollectionWrapper.new("ReportTable", self)
end
reports() click to toggle source
# File lib/dhis2/client.rb, line 131
def reports
  @reports ||= CollectionWrapper.new("Report", self)
end
resource_tables() click to toggle source
# File lib/dhis2/client.rb, line 139
def resource_tables
  @resource_tables ||= CollectionWrapper.new("ResourceTable", self)
end
system_infos() click to toggle source
# File lib/dhis2/client.rb, line 143
def system_infos
  @system_infos ||= CollectionWrapper.new("SystemInfo", self)
end
users() click to toggle source
# File lib/dhis2/client.rb, line 147
def users
  @users ||= CollectionWrapper.new("User", self)
end

Private Instance Methods

compute_payload(payload, raw_input) click to toggle source
# File lib/dhis2/client.rb, line 216
def compute_payload(payload, raw_input)
  return nil unless payload
  return payload.to_json if raw_input

  Dhis2::Case.deep_change(payload, :camelize).to_json
end
execute(method_name:, url:, query_params: {}, payload: nil, raw: false, raw_input: false) click to toggle source
# File lib/dhis2/client.rb, line 189
def execute(method_name:, url:, query_params: {}, payload: nil, raw: false, raw_input: false)
  computed_payload = compute_payload(payload, raw_input)

  start_tracking = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  raw_response = RestClient::Request.execute(
    method:     method_name,
    url:        url,
    headers:    headers(method_name, query_params),
    payload:    computed_payload,
    verify_ssl: @verify_ssl,
    timeout:    @timeout,
    cookies:    @@cookie_jar[@base_url],
  )
  finish_tracking = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  @@cookie_jar[@base_url] = raw_response.cookies

  present_response(raw_response, raw, start_tracking, finish_tracking)
rescue RestClient::RequestFailed => e
  exception = ::Dhis2::RequestError.new(e.message)
  exception.response  = e.response  if e.respond_to?(:response)
  exception.http_code = e.http_code if e.respond_to?(:http_code)
  exception.http_body = e.http_body if e.respond_to?(:http_body)
  log(exception.response.request, exception.response)
  raise exception
end
headers(method_name, query_params) click to toggle source
# File lib/dhis2/client.rb, line 227
def headers(method_name, query_params)
  { params: query_params, accept: :json }.tap do |hash|
    hash[:content_type] = :json unless method_name == :get
  end
end
log(request, response, diff = nil) click to toggle source
# File lib/dhis2/client.rb, line 233
def log(request, response, diff = nil)
  return unless @debug

  puts [
    request.url,
    request.args[:payload].to_json,
    response,
    "in #{diff}",
  ].join(TAB)
end
present_response(raw_response, raw, start_tracking, finish_tracking) click to toggle source
# File lib/dhis2/client.rb, line 172
def present_response(raw_response, raw, start_tracking, finish_tracking)
  safe_response = if EMPTY_RESPONSES.include?(raw_response)
                    {}
                  else
                    JSON.parse(raw_response)
                  end

  diff_tracking = finish_tracking - start_tracking
  log(raw_response.request, safe_response, diff_tracking)

  if raw
    safe_response
  else
    Dhis2::Case.deep_change(safe_response, :underscore)
  end
end
uri(path) click to toggle source
# File lib/dhis2/client.rb, line 223
def uri(path)
  self.class.uri(@base_url, path)
end