Class: CdekClient::CalculatorClient

Inherits:
AbstractClient show all
Defined in:
lib/cdek_client/calculator_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(account = nil, password = nil) ⇒ CalculatorClient

Returns a new instance of CalculatorClient.



12
13
14
15
# File 'lib/cdek_client/calculator_client.rb', line 12

def initialize( = nil, password = nil)
  @account = 
  @password = password
end

Instance Method Details

#calculate(params) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cdek_client/calculator_client.rb', line 17

def calculate(params)
  params = normalize_request_data params
  formatted_date_execute = if params[:dateExecute].is_a?(Date) || params[:dateExecute].is_a?(Time)
    CdekClient.format_date params[:dateExecute]
  elsif !Util.blank? params[:dateExecute]
    params[:dateExecute]
  else
    CdekClient.format_date Date.today
  end
  params.merge!(
    version: CdekClient::CALCULATOR_API_VERSION,
    dateExecute: formatted_date_execute
  )
  if !@account.nil? && !@password.nil?
    params.merge!({
      authLogin: @account,
      secure: CdekClient.generate_secure(formatted_date_execute, @password)
    })
  end
  result = request url_for(:calculator_primary, :calculate), url_for(:calculator_secondary, :calculate), :post, params
  if result.errors.any?
    result.set_data({})
  elsif result.data.has_key?(:error)
    Util.array_wrap(result.data[:error]).each do |error_data|
      error = Calculator.get_api_error error_data[:code], error_data[:text]
      result.add_error error
    end
    result.set_data({})
  else
    normalized_data = normalize_response_data result.data[:result], response_normalization_rules_for(:calculate)
    result.set_data normalized_data
  end
  return result
end