Class: Dhis2::Client

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dhis2/client.rb', line 46

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
  end
end

Class Method Details

.camelize(string, uppercase_first_letter = true) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dhis2/client.rb', line 27

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

.deep_change_case(hash, type) ⇒ Object



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

def self.deep_change_case(hash, 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



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

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



36
37
38
39
40
41
42
43
44
# File 'lib/dhis2/client.rb', line 36

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



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

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

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



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

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

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



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

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

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



61
62
63
# File 'lib/dhis2/client.rb', line 61

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

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



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

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