module RegApi2::Action

Implementation of request/response activity.

Constants

API_URI

REG.API base URI

DEFAULT_IO_ENCODING

Default IO encoding

DEFAULT_LANG

Default lang.

DEFAULT_PASSWORD

Default password.

DEFAULT_USERNAME

Default user name.

Public Instance Methods

clear_http() click to toggle source

Clears internal ‘http` singleton. Also finishes any started HTTP session. @return nil @note For testing purposes. @see http

# File lib/reg_api2/action.rb, line 71
def clear_http
  return nil  unless @http
  @http.finish  if @http.respond_to?(:started) && @http.started
  @http = nil
end
create_http() click to toggle source

Creates HTTPS handler. @return [Net::HTTP] HTTPS handler. @see http

# File lib/reg_api2/action.rb, line 47
def create_http
  http = Net::HTTP.new(
    API_URI.host, 
    API_URI.port
  )
  http.use_ssl = true
  apply_ca_cert_path(http)
  apply_pem(http)
  http
end
get_form_data(defopts, opts) click to toggle source

Gets form data for POST request @param [Hash] defopts @param [Hash] opts @return [Hash] Form data to be sent. @raise [ContractError]

# File lib/reg_api2/action.rb, line 82
def get_form_data(defopts, opts)
  # HACK: REG.API doesn't know about utf-8.
  io_encoding = 'utf8'  if !io_encoding || io_encoding == DEFAULT_IO_ENCODING
  opts = opts.to_hash  if opts.respond_to?(:to_hash)
  req_contract = RegApi2::RequestContract.new(defopts)
  opts = req_contract.validate(opts)

  form = {
    'username'          => username || DEFAULT_USERNAME,
    'password'          => password || DEFAULT_PASSWORD,
    'io_encoding'       => io_encoding,
    'lang'              => lang || DEFAULT_LANG,
    'output_format'     => 'json',
    'input_format'      => 'json',
    'show_input_params' => 0,
    'input_data'        => Yajl::Encoder.encode(opts)
  }

  form
end
handle_response(defopts, res) click to toggle source

Handles response @param [Hash] defopts @param [Net::HTTPResponse] res HTTP Response @return [Object] Contracted response. @raise [NetError] @raise [ApiError]

# File lib/reg_api2/action.rb, line 109
def handle_response(defopts, res)
  raise NetError.new(res.body)  unless res.code == '200'

  json = Yajl::Parser.parse(res.body)
  RegApi2.got_response(json)

  raise ApiError.from_json(json)  if json['result'] == 'error'

  res_contract = RegApi2::ResultContract.new(defopts)
  res_contract.handle_result(json)
end
http() click to toggle source

Creates or gets HTTPS handler. @return [Net::HTTP] HTTPS handler. @see create_http @see clear_http

# File lib/reg_api2/action.rb, line 62
def http
  @http ||= create_http
end
make_action(category, name, defopts, opts = {}) click to toggle source

Do actual call to REG.API using POST/JSON convention. @param [Symbol] category @param [Symbol] name @param [Hash] defopts @param [Hash] opts @return [Hash] Result answer field. @raise [NetError] @raise [ApiError] @raise [ContractError]

# File lib/reg_api2/action.rb, line 130
def make_action category, name, defopts, opts = {}
  req = Net::HTTP::Post.new(
    category.nil? ? "#{API_URI.path}/#{name}" : "#{API_URI.path}/#{category}/#{name}"
  )
  form = get_form_data(defopts, opts)
  RegApi2.form_to_be_sent(req.path, form)

  req.set_form_data(form)
  res = http.request(req)

  handle_response(defopts, res)
end

Private Instance Methods

apply_ca_cert_path(http) click to toggle source
# File lib/reg_api2/action.rb, line 22
def apply_ca_cert_path(http)
  unless ca_cert_path.nil?
    http.verify_mode = OpenSSL::SSL::VERIFY_PEER
    http.ca_file     = ca_cert_path
  else
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
end
apply_pem(http) click to toggle source
# File lib/reg_api2/action.rb, line 31
def apply_pem(http)
  return  if pem.nil?
  http.cert = OpenSSL::X509::Certificate.new(pem)
  if pem_password
    raise ArgumentError, "The private key requires a password"  if pem_password.empty?
    http.key = OpenSSL::PKey::RSA.new(pem, pem_password)
  else
    http.key = OpenSSL::PKey::RSA.new(pem)
  end
end