Class: RegApi2::ResultContract
- Inherits:
-
Object
- Object
- RegApi2::ResultContract
- Defined in:
- lib/reg_api2/result_contract.rb
Overview
Contract for API results. Waits for answer field and returns it only by default.
Constant Summary collapse
- INT_FIELDS =
Fields that will be converted to Fixnum.
%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.
%w[ amount total_amount payment total_payment month_traf price_retail ].freeze
- BOOL_FIELDS =
Fields that will be converted to Boolean.
%w[ success ].freeze
Instance Attribute Summary collapse
-
#opts ⇒ Hash
readonly
Options of contract.
Instance Method Summary collapse
-
#convert(answer) ⇒ Object
Reworks answer to translate INT_FIELDS, FLOAT_FIELDS and BOOL_FIELDS.
-
#handle_answer(answer) ⇒ Object
Handles API answer.
-
#handle_result(result) ⇒ Object
Extracts answer field and returns it wrapped by #handle_answer.
-
#initialize(opts = {}) ⇒ ResultContract
constructor
A new instance of ResultContract.
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
#opts ⇒ Hash (readonly)
Options of contract.
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.
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.
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.
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 |