class Centralpos::Core::Gateway

Attributes

mode[R]

Public Class Methods

new(username, password, mode = :sandbox) click to toggle source
# File lib/centralpos/core/gateway.rb, line 6
def initialize(username, password, mode = :sandbox)
  @username = username
  @password = password
  @mode = mode
end

Public Instance Methods

call(method, extra_data = {}) click to toggle source
# File lib/centralpos/core/gateway.rb, line 12
def call(method, extra_data = {})
  message = authentication_params.merge(extra_data)

  response = client.call(method, message: message)

  parse_result(response, method)
rescue Savon::Error, Errno::ENETUNREACH => error
  handle_exception(error)
end
live() click to toggle source
# File lib/centralpos/core/gateway.rb, line 27
def live
  @client = nil
  @mode = :live
end
sandbox() click to toggle source
# File lib/centralpos/core/gateway.rb, line 22
def sandbox
  @client = nil
  @mode = :sandbox
end

Private Instance Methods

authentication_params() click to toggle source
# File lib/centralpos/core/gateway.rb, line 51
def authentication_params
  {
    "Autenticacion" => {
      "Usuario" => @username,
      "Clave" => @password
    }
  }
end
client() click to toggle source
# File lib/centralpos/core/gateway.rb, line 34
def client
  @client ||= Savon.client(opts)
end
failed_response(error) click to toggle source
# File lib/centralpos/core/gateway.rb, line 94
def failed_response(error)
  {
    response_code: error.http.code,
    success: false,
    result: nil
  }
end
handle_exception(error) click to toggle source
# File lib/centralpos/core/gateway.rb, line 73
def handle_exception(error)
  case error
  when Savon::SOAPFault
    failed_response(error).merge(error: error.to_hash[:fault])
  when Savon::HTTPError
    failed_response(error).merge(error: error.to_hash)
  when Errno::ENETUNREACH
    unable_to_connect(error).merge(error: error.message)
  else
    raise error
  end
end
opts() click to toggle source
# File lib/centralpos/core/gateway.rb, line 38
def opts
  { wsdl: wsdl_endpoint }.merge(Centralpos::Core::Logger.options)
end
parse_body(response, method) click to toggle source
# File lib/centralpos/core/gateway.rb, line 66
def parse_body(response, method)
  method_response = "#{method}_response".to_sym
  method_result = "#{method}_result".to_sym

  response.body[method_response][method_result]
end
parse_result(response, method) click to toggle source
# File lib/centralpos/core/gateway.rb, line 60
def parse_result(response, method)
  body = parse_body(response, method)

  successful_response(response).merge(result: body)
end
successful_response(response) click to toggle source
# File lib/centralpos/core/gateway.rb, line 86
def successful_response(response)
  {
    response_code: response.http.code,
    success: true,
    error: nil
  }
end
unable_to_connect(error) click to toggle source
# File lib/centralpos/core/gateway.rb, line 102
def unable_to_connect(error)
  {
    response_code: nil,
    success: false,
    result: nil
  }
end
wsdl_endpoint() click to toggle source
# File lib/centralpos/core/gateway.rb, line 42
def wsdl_endpoint
  case @mode
  when :live then Centralpos.production_wsdl_endpoint
  when :sandbox then Centralpos.sandbox_wsdl_endpoint
  else
    Centralpos.sandbox_wsdl_endpoint
  end
end