Class: GlobalCollect::ApiClient

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/global_collect/api_client.rb

Constant Summary collapse

DEFAULT_TIMEOUT =

WDL §3.6 recommends this timeout

70

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, environment, authentication) ⇒ ApiClient

Returns a new instance of ApiClient.



16
17
18
19
20
21
# File 'lib/global_collect/api_client.rb', line 16

def initialize(service, environment, authentication)
  @serivce = service
  @environment = environment
  @authentication = authentication
  @service_url = ApiClient.service_url(service, environment, authentication)
end

Instance Attribute Details

#authenticationObject (readonly)

Returns the value of attribute authentication.



15
16
17
# File 'lib/global_collect/api_client.rb', line 15

def authentication
  @authentication
end

#environmentObject (readonly)

Returns the value of attribute environment.



15
16
17
# File 'lib/global_collect/api_client.rb', line 15

def environment
  @environment
end

#serviceObject (readonly)

Returns the value of attribute service.



15
16
17
# File 'lib/global_collect/api_client.rb', line 15

def service
  @service
end

Class Method Details

.service_url(service, environment, authentication) ⇒ Object

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/global_collect/api_client.rb', line 48

def self.service_url(service, environment, authentication)
  # WDL §§3.4-5 specify the allowed arguments
  raise ArgumentError.new("Only [Hosted] Merchant Link is currently supported!") unless [:merchant_link].include?(service)
  raise ArgumentError.new("Only :test and :production are valid environemnts!") unless [:test, :production].include?(environment)
  raise ArgumentError.new("Only :ip_check and :client_auth are valid authentication schemes!") unless [:ip_check, :client_auth].include?(authentication)
  {
    :merchant_link => {
      # WDL §3.4 specifies the test environment urls
      :test       => {
        :ip_check     => "https://ps.gcsip.nl/wdl/wdl",
        :client_auth  => "https://ca.gcsip.nl/wdl/wdl"
      },
      # WDL §3.5 specifies the prodution environment urls
      :production => {
        :ip_check     => "https://ps.gcsip.com/wdl/wdl",
        :client_auth  => "https://ca.gcsip.com/wdl/wdl"
      }
    }
  }[service][environment][authentication]
end

Instance Method Details

#make_request(request, add_mixins = true) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/global_collect/api_client.rb', line 23

def make_request(request, add_mixins=true)
  xml = request.to_xml
  GlobalCollect.wire_logger.info("POST [#{request.action} v#{request.version}] => [#{@service_url}] - body:\n#{xml}")
  
  response = nil
  request_time = Benchmark.realtime do
    response = self.class.post(@service_url,
      :body     => xml,
      :timeout  => DEFAULT_TIMEOUT
    )
  end
  
  unless response
    error_message = "Request [#{request.action} v#{request.version}] => [#{@service_url}] failed! No response!"
    GlobalCollect.wire_logger.error(error_message)
    raise error_message
  end
  GlobalCollect.wire_logger.info("RESP [#{request.action} v#{request.version}] => #{response.code} - #{request_time} s - body:\n#{response.body}")
  
  base = GlobalCollect::Responses::Base.new(response.delegate, response.body)
  raise "Malformed response to #{request.action} request! Body: '#{response.body}'" if base.malformed?
  request.suggested_response_mixins.each{|m| base.extend(m) } if add_mixins
  base
end