class TINCheck::Request

Constants

InvalidConfig
ResponseError
SERVICES
SERVICE_XMLNS

Attributes

config[R]
http[R]
serializer[R]

Public Class Methods

new(config = TINCheck.default_config, http: nil, serializer: nil) click to toggle source
# File lib/tincheck/request.rb, line 22
def initialize(config = TINCheck.default_config, http: nil, serializer: nil)
  raise InvalidConfig, 'invalid or missing config' unless config.is_a?(Config)
  @config = config
  @http = http || _http
  @parser = XML.parser_with(config.xml_lib)
  @serializer = serializer || XML.serializer_with(config.xml_lib)
end

Public Instance Methods

call(request_hash) click to toggle source
# File lib/tincheck/request.rb, line 30
def call(request_hash)
  r = post(serializer.(format_request(request_hash)))
  Response.with_http_response(r, parser: @parser)
end
post(xml) click to toggle source
# File lib/tincheck/request.rb, line 35
def post(xml)
  http.dup.start { |h| h.request(post_request(xml)) }
end
status(*) click to toggle source
# File lib/tincheck/request.rb, line 39
def status(*)
  call(SERVICES[:status] => {})
end
tin_name(**kwargs) click to toggle source
# File lib/tincheck/request.rb, line 43
def tin_name(**kwargs)
  require_arguments!(kwargs, :name, :tin)
  call(SERVICES[:tin_name] => tin_name_arg(**kwargs))
end

Private Instance Methods

_http() click to toggle source
# File lib/tincheck/request.rb, line 50
def _http
  Net::HTTP.new(config.uri.host, config.uri.port, *config.proxy_args).tap do |h|
    h.use_ssl = true if config.uri.scheme == 'https'
  end
end
auth_args() click to toggle source
# File lib/tincheck/request.rb, line 56
def auth_args
  raise InvalidConfig, 'no username or password specified in config' unless
    config.username && config.password
  {
    'CurUser' => {
      'UserLogin' => config.username,
      'UserPassword' => config.password
    }
  }
end
format_request(request_hash) click to toggle source
# File lib/tincheck/request.rb, line 67
def format_request(request_hash)
  soap_envelop(inject_auth_args(request_hash))
end
inject_auth_args(request_hash) click to toggle source
# File lib/tincheck/request.rb, line 71
def inject_auth_args(request_hash)
  k, h = request_hash.first
  return request_hash if h.key?('CurUser')
  request_hash.merge(k => h.merge(auth_args))
end
post_request(xml) click to toggle source
# File lib/tincheck/request.rb, line 77
def post_request(xml)
  Net::HTTP::Post.new(config.uri.path).tap do |r|
    r['Content-Type'] ||= 'text/xml; charset=UTF-8'
    r.body = xml
  end
end
require_arguments!(kwargs, *keys) click to toggle source
# File lib/tincheck/request.rb, line 84
def require_arguments!(kwargs, *keys)
  keys.each { |k| raise ArgumentError, "missing keyword: #{k}" unless kwargs.key?(k) }
  kwargs
end
soap_envelop(request_hash) click to toggle source
# File lib/tincheck/request.rb, line 89
def soap_envelop(request_hash)
  {
    'SOAP-ENV:Envelope' => {
      'xmlns:SOAP-ENC' => 'http://schemas.xmlsoap.org/soap/encoding/',
      'xmlns:SOAP-ENV' => 'http://schemas.xmlsoap.org/soap/envelope/',
      'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
      'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
      'SOAP-ENV:Body' => request_hash.merge('xmlns' => SERVICE_XMLNS)
    }
  }
end
tin_name_arg(giin: nil, name:, tin: nil, **) click to toggle source
# File lib/tincheck/request.rb, line 101
def tin_name_arg(giin: nil, name:, tin: nil, **)
  {
    'TinName' => {
      'TIN' => tin,
      'LName' => name,
      'GIIN' => giin
    }
  }
end