torasup 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -118,6 +118,48 @@ By default the following counties will take precedence: `["US", "GB", "AU", "IT"
118
118
 
119
119
  Now `Torasup::PhoneNumber.new(+1 415-234 567).country_id` will return `"ca"`
120
120
 
121
+ ## Testing
122
+
123
+ Torasup exposes a few test helpers methods which you can use in your tests. See [the helper module](https://github.com/dwilkie/torasup/blob/master/lib/torasup/test/helpers.rb) for more info.
124
+
125
+ Here's an example using rspec:
126
+
127
+ require 'spec_helper'
128
+
129
+ describe User do
130
+ include Torasup::Test::Helpers
131
+
132
+ ASSERTED_REGISTERED_OPERATORS = {"kh" => %w{smart beeline hello}}
133
+
134
+ # override this method to return the full path of the yaml spec
135
+ def yaml_file(filename)
136
+ File.join(File.dirname(__FILE__), "/#{filename}")
137
+ end
138
+
139
+ # provide a custom spec file for example see:
140
+ # see https://github.com/dwilkie/torasup/blob/master/spec/support/custom_pstn_spec.yaml
141
+ def pstn_data(custom_spec = nil)
142
+ super("custom_operators_spec.yaml")
143
+ end
144
+
145
+ def with_operators(&block)
146
+ super(:only_registered => ASSERTED_REGISTERED_OPERATORS, &block)
147
+ end
148
+
149
+ describe "#operator" do
150
+ it "should return the correct operator" do
151
+ with_operators do |number_parts, assertions|
152
+ new_user = build(:user, :phone_number => number_parts.join)
153
+ new_user.operator.should == assertions["name"]
154
+ end
155
+ end
156
+ end
157
+ end
158
+
159
+ In this example `with_operators` is used to yield a to block with a sample number (yielded as `number_parts`) and a hash of assertions (yielded as `assertions`) made about that number. The assertions are defined in `custom_operators_spec.yaml` which is in the same directory as this spec file.
160
+
161
+ Sample numbers and assertions are only yielded for the operators defined in `ASSERTED_REGISTERED_OPERATORS`
162
+
121
163
  ## Contributing
122
164
 
123
165
  1. Fork it
@@ -0,0 +1,95 @@
1
+ module Torasup
2
+ module Test
3
+ module Helpers
4
+ private
5
+
6
+ def yaml_file(filename)
7
+ raise "Override this method to return the full path of the yaml spec"
8
+ end
9
+
10
+ def load_yaml_file(file_to_load)
11
+ file_to_load ? YAML.load_file(file_to_load) : {}
12
+ end
13
+
14
+ def pstn_spec(filename)
15
+ File.join(File.dirname(__FILE__), "../../../spec/support/#{filename}")
16
+ end
17
+
18
+ def pstn_data(custom_spec = nil)
19
+ return @pstn_data if @pstn_data
20
+ if custom_spec == true
21
+ custom_spec = pstn_spec("custom_pstn_spec.yaml")
22
+ elsif custom_spec
23
+ custom_spec = yaml_file(custom_spec)
24
+ end
25
+ data = load_yaml_file(pstn_spec("pstn_spec.yaml"))
26
+ @pstn_data = custom_spec ? data.deeper_merge(load_yaml_file(custom_spec)) : data
27
+ end
28
+
29
+ def with_operators(options = {}, &block)
30
+ operator_assertions = {}
31
+ with_pstn_data(options) do |country_id, country_data, country_prefix|
32
+ operator_assertions[country_prefix] = {}
33
+ default_assertions = {"country_code" => country_prefix}
34
+ with_operator_data(country_id, options) do |operator, operator_data|
35
+ default_assertions.merge!("id" => operator).merge!(operator_data["assertions"])
36
+ with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, area|
37
+ operator_assertions[country_prefix][area_code] = {}
38
+ local_number = ("0" * (6 - area_code_prefix.length))
39
+ unresolved_number = area_code_prefix + local_number
40
+ operator_assertions[country_prefix][area_code][unresolved_number] = default_assertions.merge(
41
+ "area_code" => area_code, "prefix" => area_code_prefix, "local_number" => local_number
42
+ )
43
+ end
44
+ with_operator_prefixes(operator_data) do |prefix|
45
+ operator_assertions[country_prefix][prefix] = {}
46
+ local_number = ("0" * 6)
47
+ operator_assertions[country_prefix][prefix][local_number] = default_assertions.merge(
48
+ "prefix" => prefix, "area_code" => nil
49
+ )
50
+ end
51
+ end
52
+ end
53
+ operator_assertions.each do |country_prefix, country_assertions|
54
+ country_assertions.each do |area_code_or_prefix, area_code_or_prefix_assertions|
55
+ area_code_or_prefix_assertions.each do |unresolved_local_number, assertions|
56
+ yield [country_prefix, area_code_or_prefix, unresolved_local_number], assertions
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ def with_pstn_data(options = {}, &block)
63
+ pstn_data(options[:with_custom_pstn_data]).each do |country_id, country_data|
64
+ next if options[:only_registered] && !options[:only_registered].include?(country_id)
65
+ yield country_id, country_data, country_data["prefix"]
66
+ end
67
+ end
68
+
69
+ def with_operator_data(country_id, options = {}, &block)
70
+ country_data(country_id, options[:with_custom_pstn_data])["operators"].each do |operator, operator_data|
71
+ next if options[:only_registered] && !options[:only_registered][country_id].include?(operator)
72
+ yield operator, operator_data
73
+ end
74
+ end
75
+
76
+ def with_operator_area_codes(country_data, operator_data, &block)
77
+ (operator_data["area_code_prefixes"] || {}).each do |area_code_prefix|
78
+ country_data["area_codes"].each do |area_code, area|
79
+ yield area_code_prefix, area_code, area
80
+ end
81
+ end
82
+ end
83
+
84
+ def with_operator_prefixes(operator_data, &block)
85
+ operator_data["prefixes"].each do |prefix|
86
+ yield prefix
87
+ end
88
+ end
89
+
90
+ def country_data(country_id, custom_file = nil)
91
+ pstn_data(custom_file)[country_id.to_s] || {}
92
+ end
93
+ end
94
+ end
95
+ end
@@ -1,3 +1,3 @@
1
1
  module Torasup
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/torasup.rb CHANGED
@@ -1,3 +1,8 @@
1
+ require 'yaml'
2
+ require 'phony'
3
+ require 'countries'
4
+ require 'deep_merge/rails_compat'
5
+
1
6
  require "torasup/version"
2
7
  require "torasup/configuration"
3
8
  require "torasup/phone_number"
@@ -5,10 +10,10 @@ require "torasup/operator"
5
10
  require "torasup/location"
6
11
 
7
12
  module Torasup
8
- require 'yaml'
9
- require 'phony'
10
- require 'countries'
11
- require 'deep_merge/rails_compat'
13
+
14
+ module Test
15
+ autoload :Helpers, 'torasup/test/helpers'
16
+ end
12
17
 
13
18
  class << self
14
19
  def configure(&block)
@@ -3,13 +3,10 @@
3
3
 
4
4
  ---
5
5
  kh:
6
- area_codes:
7
- '45': New Province
8
6
  operators:
9
7
  hello:
10
8
  assertions:
11
- name: Hello
12
- my_custom_property: hello-foo
9
+ name: "Hello"
13
10
  prefixes:
14
11
  - '15'
15
12
  - '16'
@@ -1,13 +1,14 @@
1
1
  module PstnHelpers
2
+ include Torasup::Test::Helpers
3
+
2
4
  private
3
5
 
4
- def load_yaml_file(file_to_load)
5
- file_to_load ? YAML.load_file(File.join(File.dirname(__FILE__), "/#{file_to_load}")) : {}
6
+ def clear_pstn_data
7
+ @pstn_data = nil
6
8
  end
7
9
 
8
- def pstn_data(with_custom_pstn_data = false)
9
- data = load_yaml_file("pstn_spec.yaml")
10
- with_custom_pstn_data ? data.deeper_merge(load_yaml_file("custom_pstn_spec.yaml")) : data
10
+ def yaml_file(filename)
11
+ File.join(File.dirname(__FILE__), "../../spec/support/#{filename}")
11
12
  end
12
13
 
13
14
  def with_phone_numbers(options = {}, &block)
@@ -43,82 +44,25 @@ module PstnHelpers
43
44
  end
44
45
  end
45
46
 
46
- def with_operators(options = {}, &block)
47
- operator_assertions = {}
48
- with_pstn_data(options) do |country_id, country_data, country_prefix|
49
- operator_assertions[country_prefix] = {}
50
- default_assertions = {"country_code" => country_prefix}
51
- with_operator_data(country_data) do |operator, operator_data|
52
- default_assertions.merge!("id" => operator).merge!(operator_data["assertions"])
53
- with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, area|
54
- operator_assertions[country_prefix][area_code] = {}
55
- local_number = ("0" * (6 - area_code_prefix.length))
56
- unresolved_number = area_code_prefix + local_number
57
- operator_assertions[country_prefix][area_code][unresolved_number] = default_assertions.merge(
58
- "area_code" => area_code, "prefix" => area_code_prefix, "local_number" => local_number
59
- )
60
- end
61
- with_operator_prefixes(operator_data) do |prefix|
62
- operator_assertions[country_prefix][prefix] = {}
63
- local_number = ("0" * 6)
64
- operator_assertions[country_prefix][prefix][local_number] = default_assertions.merge(
65
- "prefix" => prefix, "area_code" => nil
66
- )
67
- end
68
- end
69
- end
70
- operator_assertions.each do |country_prefix, country_assertions|
71
- country_assertions.each do |area_code_or_prefix, area_code_or_prefix_assertions|
72
- area_code_or_prefix_assertions.each do |unresolved_local_number, assertions|
73
- yield country_prefix, area_code_or_prefix, unresolved_local_number, assertions
74
- end
75
- end
76
- end
77
- end
78
-
79
47
  def sample_operator
80
48
  country = pstn_data.first
81
49
  [country.first, country.last["operators"].first.first]
82
50
  end
83
51
 
84
- def prefixes(country_id, operator)
52
+ def prefixes(country_id, *operators)
85
53
  prefix_data = []
86
54
  country_data = pstn_data[country_id]
87
55
  country_prefix = country_data["prefix"]
88
- operator_data = country_data["operators"][operator]
89
- with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code|
90
- prefix_data << (country_prefix + area_code + area_code_prefix)
91
- end
92
- with_operator_prefixes(operator_data) do |prefix|
93
- prefix_data << (country_prefix + prefix)
94
- end
95
- prefix_data
96
- end
97
-
98
- def with_pstn_data(options = {}, &block)
99
- pstn_data(options[:with_custom_pstn_data]).each do |country_id, country_data|
100
- yield country_id, country_data, country_data["prefix"]
101
- end
102
- end
103
-
104
- def with_operator_data(country_data, &block)
105
- country_data["operators"].each do |operator, operator_data|
106
- yield operator, operator_data
107
- end
108
- end
109
-
110
- def with_operator_prefixes(operator_data, &block)
111
- operator_data["prefixes"].each do |prefix|
112
- yield prefix
113
- end
114
- end
115
-
116
- def with_operator_area_codes(country_data, operator_data, &block)
117
- (operator_data["area_code_prefixes"] || {}).each do |area_code_prefix|
118
- country_data["area_codes"].each do |area_code, area|
119
- yield area_code_prefix, area_code, area
56
+ operators.each do |operator|
57
+ operator_data = country_data["operators"][operator]
58
+ with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code|
59
+ prefix_data << (country_prefix + area_code + area_code_prefix)
60
+ end
61
+ with_operator_prefixes(operator_data) do |prefix|
62
+ prefix_data << (country_prefix + prefix)
120
63
  end
121
64
  end
65
+ prefix_data
122
66
  end
123
67
 
124
68
  def configure_with_custom_data(custom_data = true)
@@ -31,12 +31,12 @@ module Torasup
31
31
 
32
32
  shared_examples_for "an operator" do
33
33
  it "should return all the operator metadata" do
34
- with_operators(options) do |country_code, area_code_or_prefix, unresolved_local_number, assertions|
35
- subject = Operator.new(country_code, area_code_or_prefix, unresolved_local_number)
34
+ with_operators(options) do |number_parts, assertions|
35
+ subject = Operator.new(*number_parts)
36
36
  assertions.each do |method, assertion|
37
37
  result = subject.send(method)
38
38
  result_error = result.nil? ? "nil" : "'#{result}'"
39
- result.should(eq(assertion), "expected Operator.new('#{country_code}', '#{area_code_or_prefix}', '#{unresolved_local_number}').#{method} to return '#{assertion}' but got #{result_error}")
39
+ result.should(eq(assertion), "expected Operator.new('#{number_parts}').#{method} to return '#{assertion}' but got #{result_error}")
40
40
  end
41
41
  end
42
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torasup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-03 00:00:00.000000000 Z
12
+ date: 2013-04-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: countries
@@ -111,6 +111,7 @@ files:
111
111
  - lib/torasup/location.rb
112
112
  - lib/torasup/operator.rb
113
113
  - lib/torasup/phone_number.rb
114
+ - lib/torasup/test/helpers.rb
114
115
  - lib/torasup/version.rb
115
116
  - spec/spec_helper.rb
116
117
  - spec/support/custom_pstn.yaml
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
137
138
  version: '0'
138
139
  segments:
139
140
  - 0
140
- hash: -1036375537
141
+ hash: -60913261
141
142
  required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  none: false
143
144
  requirements:
@@ -146,10 +147,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
147
  version: '0'
147
148
  segments:
148
149
  - 0
149
- hash: -1036375537
150
+ hash: -60913261
150
151
  requirements: []
151
152
  rubyforge_project:
152
- rubygems_version: 1.8.25
153
+ rubygems_version: 1.8.24
153
154
  signing_key:
154
155
  specification_version: 3
155
156
  summary: ! '"Retuns metadata about a phone number such as operator, area code and