Module: RegApi2::Action

Included in:
RegApi2
Defined in:
lib/reg_api2/action.rb

Overview

Implementation of request/response activity.

Constant Summary

DEFAULT_IO_ENCODING =

Default IO encoding

'utf-8'.freeze
DEFAULT_LANG =

Default lang.

'en'.freeze
DEFAULT_USERNAME =

Default user name.

'test'.freeze
DEFAULT_PASSWORD =

Default password.

'test'.freeze
API_URI =

REG.API base URI

URI.parse("https://api.reg.ru/api/regru2")

Instance Method Summary (collapse)

Instance Method Details

- (Object) clear_http

Note:

For testing purposes.

Clears internal http singleton. Also finishes any started HTTP session.

Returns:

  • nil

See Also:



72
73
74
75
76
# File 'lib/reg_api2/action.rb', line 72

def clear_http
  return nil  unless @http
  @http.finish  if @http.respond_to?(:started) && @http.started
  @http = nil
end

- (Net::HTTP) create_http

Creates HTTPS handler.

Returns:

  • (Net::HTTP)

    HTTPS handler.

See Also:



48
49
50
51
52
53
54
55
56
57
# File 'lib/reg_api2/action.rb', line 48

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

- (Hash) get_form_data(defopts, opts)

Gets form data for POST request

Parameters:

  • defopts (Hash)
  • opts (Hash)

Returns:

  • (Hash)

    Form data to be sent.

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/reg_api2/action.rb', line 83

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

- (Object) handle_response(defopts, res)

Handles response

Parameters:

  • defopts (Hash)
  • res (Net::HTTPResponse)

    HTTP Response

Returns:

  • (Object)

    Contracted response.

Raises:



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/reg_api2/action.rb', line 110

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

- (Net::HTTP) http

Creates or gets HTTPS handler.

Returns:

  • (Net::HTTP)

    HTTPS handler.

See Also:



63
64
65
# File 'lib/reg_api2/action.rb', line 63

def http
  @http ||= create_http
end

- (Hash) make_action(category, name, defopts, opts = {})

Do actual call to REG.API using POST/JSON convention.

Parameters:

  • category (Symbol)
  • name (Symbol)
  • defopts (Hash)
  • opts (Hash) (defaults to: {})

Returns:

  • (Hash)

    Result answer field.

Raises:



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/reg_api2/action.rb', line 131

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