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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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[/^(agi_)?(.+)$/,2] : key
      new_variables[stripped_name] = value.kind_of?(String) ? value.strip : value
    end
  end
end

.remove_dashes_from_context_name(variables) ⇒ Object



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

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

.replace_unknown_values_with_nil(variables) ⇒ Object



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

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



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

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