Module: Adhearsion::Call::Variables::Coercions

Defined in:
lib/adhearsion/voip/call.rb

Constant Summary collapse

COERCION_ORDER =
%w{
  remove_agi_prefixes_from_keys_and_strip_whitespace
  coerce_keys_into_symbols
  coerce_extension_into_phone_number_object
  coerce_numerical_values_to_numerics
  replace_unknown_values_with_nil
  replace_yes_no_answers_with_booleans
  coerce_request_into_uri_object
  decompose_uri_query_into_hash
  override_variables_with_query_params
  remove_dashes_from_context_name
  coerce_type_of_number_into_symbol
}

Class Method Summary collapse

Class Method Details

.coerce_extension_into_phone_number_object(variables) ⇒ Object



351
352
353
354
355
# File 'lib/adhearsion/voip/call.rb', line 351

def coerce_extension_into_phone_number_object(variables)
  variables.tap do
    variables[:extension] = Adhearsion::VoIP::DSL::PhoneNumber.new(variables[:extension])
  end
end

.coerce_keys_into_symbols(variables) ⇒ Object



343
344
345
346
347
348
349
# File 'lib/adhearsion/voip/call.rb', line 343

def coerce_keys_into_symbols(variables)
  variables.inject({}) do |new_variables,(key,value)|
    new_variables.tap do
      new_variables[key.to_sym] = value
    end
  end
end

.coerce_numerical_values_to_numerics(variables) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/adhearsion/voip/call.rb', line 357

def coerce_numerical_values_to_numerics(variables)
  variables.inject({}) do |vars,(key,value)|
    vars.tap do
      is_numeric = value =~ /^-?\d+(?:(\.)\d+)?$/
      is_float   = $1
      vars[key] = if is_numeric
        if Adhearsion::VoIP::DSL::NumericalString.starts_with_leading_zero?(value)
          Adhearsion::VoIP::DSL::NumericalString.new(value)
        else
          if is_float
            if key == :uniqueid
              value
            else
              value.to_f
            end
          else
            value.to_i
          end
        end
      else
        value
      end
    end
  end
end

.coerce_request_into_uri_object(variables) ⇒ Object



398
399
400
401
402
403
# File 'lib/adhearsion/voip/call.rb', line 398

def coerce_request_into_uri_object(variables)
  if variables[:request]
    variables[:request] = URI.parse(variables[:request]) unless variables[:request].kind_of? URI
  end
  variables
end

.coerce_type_of_number_into_symbol(variables) ⇒ Object



405
406
407
408
409
# File 'lib/adhearsion/voip/call.rb', line 405

def coerce_type_of_number_into_symbol(variables)
  variables.tap do
    variables[:type_of_calling_number] = Adhearsion::VoIP::Constants::Q931_TYPE_OF_NUMBER[variables.delete(:callington).to_i]
  end
end

.decompose_uri_query_into_hash(variables) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/adhearsion/voip/call.rb', line 411

def decompose_uri_query_into_hash(variables)
  variables.tap do
    if variables[:request] && variables[:request].query
      variables[:query] = variables[:request].query.split('&').inject({}) do |query_string_parameters, key_value_pair|
        parameter_name, parameter_value = *key_value_pair.match(/(.+)=(.*)/).captures
        query_string_parameters[parameter_name] = parameter_value
        query_string_parameters
      end
    else
      variables[:query] = {}
    end
  end
end

.override_variables_with_query_params(variables) ⇒ Object



425
426
427
428
429
430
431
432
433
# File 'lib/adhearsion/voip/call.rb', line 425

def override_variables_with_query_params(variables)
  variables.tap do
    if variables[:query]
      variables[:query].each do |key, value|
        variables[key.to_sym] = value
      end
    end
  end
end

.remove_agi_prefixes_from_keys_and_strip_whitespace(variables) ⇒ Object



334
335
336
337
338
339
340
341
# File 'lib/adhearsion/voip/call.rb', line 334

def remove_agi_prefixes_from_keys_and_strip_whitespace(variables)
  variables.inject({}) do |new_variables,(key,value)|
    new_variables.tap do
      stripped_name = key.kind_of?(String) ? key[/^([ao]gi_)?(.+)$/,2] : key
      new_variables[stripped_name] = value.kind_of?(String) ? value.strip : value
    end
  end
end

.remove_dashes_from_context_name(variables) ⇒ Object



435
436
437
438
439
# File 'lib/adhearsion/voip/call.rb', line 435

def remove_dashes_from_context_name(variables)
  variables.tap do
    variables[:context].gsub!('-', '_')
  end
end

.replace_unknown_values_with_nil(variables) ⇒ Object



383
384
385
386
387
# File 'lib/adhearsion/voip/call.rb', line 383

def replace_unknown_values_with_nil(variables)
  variables.each do |key,value|
    variables[key] = nil if value == 'unknown'
  end
end

.replace_yes_no_answers_with_booleans(variables) ⇒ Object



389
390
391
392
393
394
395
396
# File 'lib/adhearsion/voip/call.rb', line 389

def replace_yes_no_answers_with_booleans(variables)
  variables.each do |key,value|
    case value
      when 'yes' then variables[key] = true
      when 'no'  then variables[key] = false
    end
  end
end