Module: Torasup::Test::Helpers

Defined in:
lib/torasup/test/helpers.rb

Instance Method Summary collapse

Instance Method Details

#country_data(country_id, custom_file = nil) ⇒ Object



87
88
89
# File 'lib/torasup/test/helpers.rb', line 87

def country_data(country_id, custom_file = nil)
  pstn_data(custom_file)[country_id.to_s] || {}
end

#interpolated_assertion(assertion, interpolations = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/torasup/test/helpers.rb', line 91

def interpolated_assertion(assertion, interpolations = {})
  if assertion
    interpolated_result = assertion.dup
    interpolations.each do |interpolation, value|
      interpolated_result.gsub!("%{#{interpolation}}", value)
    end
    interpolated_result
  end
end

#load_yaml_file(file_to_load) ⇒ Object



8
9
10
# File 'lib/torasup/test/helpers.rb', line 8

def load_yaml_file(file_to_load)
  file_to_load ? YAML.load_file(file_to_load) : {}
end

#pstn_data(custom_spec = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/torasup/test/helpers.rb', line 16

def pstn_data(custom_spec = nil)
  if custom_spec == true
    custom_spec = pstn_spec("custom_pstn_spec.yaml")
  elsif custom_spec
    custom_spec = yaml_file(custom_spec)
  end
  data = load_yaml_file(pstn_spec("pstn_spec.yaml"))
  custom_spec ? data.deeper_merge(load_yaml_file(custom_spec)) : data
end

#pstn_spec(filename) ⇒ Object



12
13
14
# File 'lib/torasup/test/helpers.rb', line 12

def pstn_spec(filename)
  File.join(File.dirname(__FILE__), "../../../spec/support/#{filename}")
end

#with_operator_area_codes(country_data, operator_data, &block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/torasup/test/helpers.rb', line 73

def with_operator_area_codes(country_data, operator_data, &block)
  (operator_data["area_code_prefixes"] || {}).each do |area_code_prefix|
    country_data["area_codes"].each do |area_code, area|
      yield area_code_prefix, area_code, area
    end
  end
end

#with_operator_data(country_id, options = {}, &block) ⇒ Object



66
67
68
69
70
71
# File 'lib/torasup/test/helpers.rb', line 66

def with_operator_data(country_id, options = {}, &block)
  country_data(country_id, options[:with_custom_pstn_data])["operators"].each do |operator, operator_data|
    next if options[:only_registered] && !options[:only_registered][country_id].include?(operator)
    yield operator, operator_data
  end
end

#with_operator_prefixes(operator_data, &block) ⇒ Object



81
82
83
84
85
# File 'lib/torasup/test/helpers.rb', line 81

def with_operator_prefixes(operator_data, &block)
  operator_data["prefixes"].each do |prefix|
    yield prefix
  end
end

#with_operators(options = {}, &block) ⇒ Object



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
51
52
53
54
55
56
57
# File 'lib/torasup/test/helpers.rb', line 26

def with_operators(options = {}, &block)
  operator_assertions = {}
  with_pstn_data(options) do |country_id, country_data, country_prefix|
    operator_assertions[country_prefix] = {}
    default_assertions = {"country_code" => country_prefix}
    with_operator_data(country_id, options) do |operator, operator_data|
      default_operator_assertions = operator_data["assertions"].merge("country_id" => country_id, "id" => operator).merge(default_assertions)
      with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, area|
        operator_assertions[country_prefix][area_code] = {}
        local_number = ("0" * (6 - area_code_prefix.length))
        unresolved_number = area_code_prefix + local_number
        operator_assertions[country_prefix][area_code][unresolved_number] = default_operator_assertions.merge(
          "area_code" => area_code, "prefix" => area_code_prefix, "local_number" => local_number
        )
      end
      with_operator_prefixes(operator_data) do |prefix|
        operator_assertions[country_prefix][prefix] = {}
        local_number = ("0" * 6)
        operator_assertions[country_prefix][prefix][local_number] = default_operator_assertions.merge(
          "prefix" => prefix, "area_code" => nil
        )
      end
    end
  end
  operator_assertions.each do |country_prefix, country_assertions|
    country_assertions.each do |area_code_or_prefix, area_code_or_prefix_assertions|
      area_code_or_prefix_assertions.each do |unresolved_local_number, assertions|
        yield [country_prefix, area_code_or_prefix, unresolved_local_number], assertions
      end
    end
  end
end

#with_pstn_data(options = {}, &block) ⇒ Object



59
60
61
62
63
64
# File 'lib/torasup/test/helpers.rb', line 59

def with_pstn_data(options = {}, &block)
  pstn_data(options[:with_custom_pstn_data]).each do |country_id, country_data|
    next if options[:only_registered] && !options[:only_registered].include?(country_id)
    yield country_id, country_data, country_data["prefix"]
  end
end

#yaml_file(filename) ⇒ Object



4
5
6
# File 'lib/torasup/test/helpers.rb', line 4

def yaml_file(filename)
  raise "Override this method to return the full path of the yaml spec"
end