torasup 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,2 +1,3 @@
1
1
  rvm:
2
2
  - 1.9.3
3
+ - 2.0.0
data/README.md CHANGED
@@ -61,7 +61,7 @@ Or install it yourself as:
61
61
 
62
62
  ### Overriding Data
63
63
 
64
- Sometimes it maybe necessary to override the data that Torasup provides. For example you may want to provide custom attributes for different operators. In order to achieve this you can provide a custom [psdn](http://en.wikipedia.org/wiki/Public_switched_telephone_network) data file. See the format of the [pstn data files](https://github.com/dwilkie/torasup/tree/master/lib/torasup/data) for more info. e.g.
64
+ Sometimes it maybe necessary to override the data that Torasup provides. For example you may want to provide custom attributes for different operators. In order to achieve this you can provide a custom [psdn](http://en.wikipedia.org/wiki/Public_switched_telephone_network) data file. Custom files also support interpolations using the `%{interpolation}` [I18n syntax](http://guides.rubyonrails.org/i18n.html#interpolation). See the format of sample custom [pstn data file](https://github.com/dwilkie/torasup/blob/master/spec/support/custom_pstn.yaml) for more info. e.g.
65
65
 
66
66
  # my_pstn_data.yaml
67
67
  ---
@@ -73,6 +73,7 @@ Sometimes it maybe necessary to override the data that Torasup provides. For exa
73
73
  metadata:
74
74
  name: Hello
75
75
  my_custom_property: hello-foo
76
+ my_custom_interpolated_property: "hello-%{interpolation}"
76
77
  prefixes:
77
78
  - '15'
78
79
  - '16'
@@ -97,6 +98,9 @@ Sometimes it maybe necessary to override the data that Torasup provides. For exa
97
98
  > op.my_custom_property
98
99
  => "hello-foo"
99
100
 
101
+ > op.my_custom_interpolated_property(:interpolation => "bar")
102
+ => "hello-bar"
103
+
100
104
  ### Registering Operators
101
105
 
102
106
  Sometimes you may only be interested in certain prefixes. For example let's say you want to match phone numbers from a certain operator from the database. You can register operators for this purpose. e.g.
@@ -7,8 +7,15 @@ module Torasup
7
7
  parse_phone_number(area_code_or_prefix, unresolved_local_number)
8
8
  end
9
9
 
10
- def method_missing(method)
11
- Torasup.prefix_data(full_prefix)[method.to_s]
10
+ def method_missing(method, interpolations = {})
11
+ value = Torasup.prefix_data(full_prefix)[method.to_s]
12
+ if value
13
+ interpolated_result = value.dup
14
+ interpolations.each do |interpolation, interpolated_value|
15
+ interpolated_result.gsub!("%{#{interpolation}}", interpolated_value)
16
+ end
17
+ interpolated_result
18
+ end
12
19
  end
13
20
 
14
21
  def self.registered_prefixes
@@ -32,19 +32,19 @@ module Torasup
32
32
  operator_assertions[country_prefix] = {}
33
33
  default_assertions = {"country_code" => country_prefix}
34
34
  with_operator_data(country_id, options) do |operator, operator_data|
35
- default_assertions.merge!("id" => operator).merge!(operator_data["assertions"])
35
+ default_operator_assertions = operator_data["assertions"].merge("id" => operator).merge(default_assertions)
36
36
  with_operator_area_codes(country_data, operator_data) do |area_code_prefix, area_code, area|
37
37
  operator_assertions[country_prefix][area_code] = {}
38
38
  local_number = ("0" * (6 - area_code_prefix.length))
39
39
  unresolved_number = area_code_prefix + local_number
40
- operator_assertions[country_prefix][area_code][unresolved_number] = default_assertions.merge(
40
+ operator_assertions[country_prefix][area_code][unresolved_number] = default_operator_assertions.merge(
41
41
  "area_code" => area_code, "prefix" => area_code_prefix, "local_number" => local_number
42
42
  )
43
43
  end
44
44
  with_operator_prefixes(operator_data) do |prefix|
45
45
  operator_assertions[country_prefix][prefix] = {}
46
46
  local_number = ("0" * 6)
47
- operator_assertions[country_prefix][prefix][local_number] = default_assertions.merge(
47
+ operator_assertions[country_prefix][prefix][local_number] = default_operator_assertions.merge(
48
48
  "prefix" => prefix, "area_code" => nil
49
49
  )
50
50
  end
@@ -90,6 +90,16 @@ module Torasup
90
90
  def country_data(country_id, custom_file = nil)
91
91
  pstn_data(custom_file)[country_id.to_s] || {}
92
92
  end
93
+
94
+ def interpolated_assertion(assertion, interpolations = {})
95
+ if assertion
96
+ interpolated_result = assertion.dup
97
+ interpolations.each do |interpolation, value|
98
+ interpolated_result.gsub!("%{#{interpolation}}", value)
99
+ end
100
+ interpolated_result
101
+ end
102
+ end
93
103
  end
94
104
  end
95
105
  end
@@ -1,3 +1,3 @@
1
1
  module Torasup
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -8,6 +8,7 @@ kh:
8
8
  metadata:
9
9
  name: Hello
10
10
  my_custom_property: hello-foo
11
+ my_custom_interpolated_property: "hello-%{interpolation}"
11
12
  prefixes:
12
13
  - '15'
13
14
  - '16'
@@ -1,5 +1,5 @@
1
1
  # this file is used to test the functionality of overriding the standard pstn data
2
- # with the data in spec/support/custom_pstn.data
2
+ # with the data in spec/support/custom_pstn.yaml
3
3
 
4
4
  ---
5
5
  kh:
@@ -7,6 +7,8 @@ kh:
7
7
  hello:
8
8
  assertions:
9
9
  name: "Hello"
10
+ my_custom_property: "hello-foo"
11
+ my_custom_interpolated_property: "hello-%{interpolation}"
10
12
  prefixes:
11
13
  - '15'
12
14
  - '16'
@@ -34,9 +34,11 @@ module Torasup
34
34
  with_operators(options) do |number_parts, assertions|
35
35
  subject = Operator.new(*number_parts)
36
36
  assertions.each do |method, assertion|
37
- result = subject.send(method)
37
+ args = []
38
+ args << {:interpolation => "interpolation"} unless subject.respond_to?(method)
39
+ result = subject.send(method, *args)
38
40
  result_error = result.nil? ? "nil" : "'#{result}'"
39
- result.should(eq(assertion), "expected Operator.new('#{number_parts}').#{method} to return '#{assertion}' but got #{result_error}")
41
+ result.should(eq(interpolated_assertion(assertion, :interpolation => "interpolation")), "expected Operator.new('#{number_parts}').#{method} to return '#{assertion}' but got #{result_error}")
40
42
  end
41
43
  end
42
44
  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.2
4
+ version: 0.0.3
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-24 00:00:00.000000000 Z
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: countries
@@ -138,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
138
  version: '0'
139
139
  segments:
140
140
  - 0
141
- hash: -60913261
141
+ hash: -283226445
142
142
  required_rubygems_version: !ruby/object:Gem::Requirement
143
143
  none: false
144
144
  requirements:
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
147
  version: '0'
148
148
  segments:
149
149
  - 0
150
- hash: -60913261
150
+ hash: -283226445
151
151
  requirements: []
152
152
  rubyforge_project:
153
153
  rubygems_version: 1.8.24