Class: RegApi2::ResultContract

Inherits:
Object
  • Object
show all
Defined in:
lib/reg_api2/result_contract.rb

Overview

Contract for API results. Waits for answer field and returns it only by default.

See Also:

Constant Summary collapse

INT_FIELDS =

Fields that will be converted to Fixnum.

See Also:

%w[
  active_domains_cnt
  active_domains_get_ctrl_cnt
  domain_folders_cnt
  renew_domains_cnt
  renew_domains_get_ctrl_cnt
  undelegated_domains_cnt
  bill_id
  service_id
  server_id
  folder_id
  cpu_count
  cpu_core
  hdd_count
  ram_count
].freeze
FLOAT_FIELDS =

Fields that will be converted to Float.

See Also:

%w[
  amount
  total_amount
  payment
  total_payment
  month_traf
  price_retail
].freeze
BOOL_FIELDS =

Fields that will be converted to Boolean.

See Also:

%w[
  success
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ ResultContract

Returns a new instance of ResultContract.



50
51
52
# File 'lib/reg_api2/result_contract.rb', line 50

def initialize(opts = {})
  @opts = opts
end

Instance Attribute Details

#optsHash (readonly)

Options of contract.

Returns:

  • (Hash)

    Options hash.



48
49
50
# File 'lib/reg_api2/result_contract.rb', line 48

def opts
  @opts
end

Instance Method Details

#convert(answer) ⇒ Object

Reworks answer to translate INT_FIELDS, FLOAT_FIELDS and BOOL_FIELDS.

Parameters:

  • answer

    API answer field.

Returns:

  • Translated answer.



100
101
102
103
104
105
106
107
108
109
# File 'lib/reg_api2/result_contract.rb', line 100

def convert(answer)
  case answer
  when Hash
    convert_hash(answer)
  when Array
    convert_array(answer)
  else
    answer
  end
end

#handle_answer(answer) ⇒ Object

Handles API answer. Take in care :field option.

Parameters:

  • answer

    API answer field.

Returns:

  • Converted answer by default.

See Also:



116
117
118
119
120
121
122
123
124
# File 'lib/reg_api2/result_contract.rb', line 116

def handle_answer(answer)
  return nil  if answer.nil?
  answer = convert(answer)
  field = opts[:field]
  if field
    answer = answer[field]
  end
  answer
end

#handle_result(result) ⇒ Object

Extracts answer field and returns it wrapped by #handle_answer. Result is unified using SymHash.from.

Parameters:

  • result (Hash)

    API result.

Returns:

  • Reworked API answer field.

See Also:



59
60
61
62
# File 'lib/reg_api2/result_contract.rb', line 59

def handle_result(result)
  result = SymHash.from(result)
  handle_answer(result[:answer])
end