Class: Dhis2::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/dhis2/client.rb

Constant Summary collapse

SUPPORTER_CASE_CHANGES =
%i[underscore camelize].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dhis2/client.rb', line 50

def initialize(options)
  if options.is_a?(String)
    @base_url = options
  else
    raise "Missing :url attribute"      unless options[:url]
    raise "Missing :user attribute"     unless options[:user]
    raise "Missing :password attribute" unless options[:password]
    url          = URI.parse(options[:url])
    url.user     = CGI.escape(options[:user])
    url.password = CGI.escape(options[:password])
    @base_url    = url.to_s
    @verify_ssl = options[:no_ssl_verification] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
    @timeout = options[:timeout] ? options[:timeout].to_i : 120
  end
end

Class Method Details

.camelize(string, uppercase_first_letter = true) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/dhis2/client.rb', line 31

def self.camelize(string, uppercase_first_letter = true)
  string = if uppercase_first_letter
             string.sub(/^[a-z\d]*/) { $&.capitalize }
           else
             string.sub(/^(?:(?=\b|[A-Z_])|\w)/) { $&.downcase }
           end
  string.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }.gsub("/", "::")
end

.deep_change_case(hash, type) ⇒ Object



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

def self.deep_change_case(hash, type)
  raise "unsupported case changes #{type} vs #{SUPPORTER_CASE_CHANGES}" unless SUPPORTER_CASE_CHANGES.include?(type)
  case hash
  when Array
    hash.map { |v| deep_change_case(v, type) }
  when Hash
    new_hash = {}
    hash.each do |k, v|
      new_key = type == :underscore ? underscore(k.to_s) : camelize(k.to_s, false)
      new_hash[new_key] = deep_change_case(v, type)
    end
    new_hash
  else
    hash
  end
end

.register_resource(resource_class) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/dhis2/client.rb', line 5

def self.register_resource(resource_class)
  class_name  = resource_class.name.split("::").last
  method_name = underscore(class_name) + "s"
  define_method(method_name) do
    CollectionWrapper.new(resource_class, self)
  end
end

.underscore(camel_cased_word) ⇒ Object



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

def self.underscore(camel_cased_word)
  return camel_cased_word unless camel_cased_word =~ /[A-Z-]|::/
  word = camel_cased_word.to_s.gsub(/::/, "/")
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!("-", "_")
  word.downcase!
  word
end

Instance Method Details

#delete(path, query_params = {}) ⇒ Object



74
75
76
# File 'lib/dhis2/client.rb', line 74

def delete(path, query_params = {})
  execute(:delete, uri(path), headers, query_params)
end

#get(path, query_params = {}) ⇒ Object



70
71
72
# File 'lib/dhis2/client.rb', line 70

def get(path, query_params = {})
  execute(:get, uri(path), headers, query_params)
end

#patch(path, payload, query_params = {}) ⇒ Object



82
83
84
# File 'lib/dhis2/client.rb', line 82

def patch(path, payload, query_params = {})
  execute(:patch, uri(path), headers, query_params, payload)
end

#post(path, payload = nil, query_params = {}) ⇒ Object



66
67
68
# File 'lib/dhis2/client.rb', line 66

def post(path, payload = nil, query_params = {})
  execute(:post, uri(path), headers, query_params, payload)
end

#put(path, payload, query_params = {}) ⇒ Object



78
79
80
# File 'lib/dhis2/client.rb', line 78

def put(path, payload, query_params = {})
  execute(:put, uri(path), headers, query_params, payload)
end