Class: Omniva::API::Client

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

Class Method Summary collapse

Class Method Details

.call(data = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/omniva/api/client.rb', line 6

def self.call(data = nil)
  validate_config
  data ||= initial_request
  client = Savon.client do
    wsdl Omniva::API.config[:wsdl]
    open_timeout Omniva::API.config[:timeout].to_i
    read_timeout Omniva::API.config[:timeout].to_i
  end
  auth = { authPhrase: Omniva::API.config[:key] }
  response = client.call :single_address1, attributes: auth, message: data
  parse_response(response)
end

.initial_requestObject



40
41
42
# File 'lib/omniva/api/client.rb', line 40

def self.initial_request
  { nimetus: 'Eesti' }
end

.parse_response(response) ⇒ Object



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

def self.parse_response(response)
  unwrapped =
    response
    .to_hash
    .fetch(:single_address_response)
    .fetch(:aadress_komponent_nimekiri)
  unwrapped ? unwrapped.fetch(:aadress_komponent) : {}
end

.validate_configObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/omniva/api/client.rb', line 19

def self.validate_config
  messages = {
    wsdl: 'WSDL is missing',
    key: 'Key is missing',
    timeout: 'Timeout is missing'
  }

  messages.each do |key, value|
    fail InvalidConfiguration, value if Omniva::API.config[key].blank?
  end
end