area 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,3 @@
1
- Area Code,Region
2
1
  201,NJ
3
2
  202,DC
4
3
  203,CT
@@ -14,7 +14,7 @@ module Area
14
14
  zip_path = File.open(File.join(File.dirname(__FILE__), '..', 'data', 'zipcodes.csv'))
15
15
  area_path = File.open(File.join(File.dirname(__FILE__), '..', 'data', 'areacodes.csv'))
16
16
 
17
- # there is probably a better way to do this...
17
+ # there is probably a better way to do this...
18
18
  if RUBY_VERSION.to_f >= 1.9
19
19
  @area_codes = CSV.read(area_path)
20
20
  @zip_codes = CSV.read(zip_path)
@@ -31,7 +31,11 @@ module Area
31
31
  @zip_codes
32
32
  end
33
33
 
34
- @regions = ["REGION", "NJ", "DC", "CT", "MB", "AL", "WA", "ME", "ID", "CA", "TX", "NY", "PA", "OH", "IL", "MN", "IN", "LA", "ON", "MS", "GA", "MI", "FL", "MD", "BAHAMAS", "BARBADOS", "BC", "NC", "WI", "ANGUILLA", "ANTIGUA AND BARBUDA", "KY", "VA", "BRITISH VIRGIN ISLANDS", "DE", "CO", "WV", "SK", "WY", "NE", "MO", "KS", "IA", "MA", "USVI", "CAYMAN ISLANDS", "RI", "AB", "OK", "MT", "QC", "TN", "UT", "BERMUDA", "RESERVED", "GRENADA", "AR", "AZ", "OR", "NM", "NB", "NH", "SD", "TURKS & CAICOS ISLANDS", "MONTSERRAT", "MP", "GU", "AS", "ND", "NV", "NL", "US GOVERNMENT", "ST. LUCIA", "DOMINICA", "ST. VINCENT & GRENADINES", "PR", "TOLL FREE", "VT", "SC", "HI", "DOMINICAN REPUBLIC", "YT", "TRINIDAD & TOBAGO", "ST. KITTS & NEVIS", "JAMAICA", "TOLL CALLS", "NS", "AK"]
34
+ def self.regions
35
+ regions = []
36
+ @area_codes.map{|row| regions << row.last.upcase }
37
+ return regions
38
+ end
35
39
 
36
40
  def self.code?(code)
37
41
  if code.to_s.length == 3
@@ -58,7 +62,7 @@ module Area
58
62
  end
59
63
 
60
64
  def self.state_or_territory?(state)
61
- if @regions.include?(state.upcase)
65
+ if self.regions.include?(state.upcase)
62
66
  return state
63
67
  else
64
68
  raise ArgumentError, "You must provide a valid US state abbreviation or territory name", caller
@@ -66,7 +70,7 @@ module Area
66
70
  end
67
71
 
68
72
  def self.zip_or_territory?(state)
69
- if @regions.include?(state.upcase) or Area.zip?(state)
73
+ if self.regions.include?(state.upcase) or self.zip?(state)
70
74
  return state
71
75
  else
72
76
  raise ArgumentError, "You must provide a valid US state abbreviation or zipcode.", caller
@@ -14,14 +14,19 @@ class Array
14
14
  #
15
15
  # Returns a String representation of the lat/lon pair.
16
16
  def to_region(options = {})
17
- if row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
18
- if options[:city]
19
- return row[1]
20
- elsif options[:state]
21
- return row[2]
22
- else
23
- return row[1] + ', ' + row[2]
17
+ if self[0].is_a?(String) and self[1].is_a?(String)
18
+ if row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
19
+ if options[:city]
20
+ return row[1]
21
+ elsif options[:state]
22
+ return row[2]
23
+ else
24
+ return row[1] + ', ' + row[2]
25
+ end
24
26
  end
27
+ else
28
+ warn "[DEPRECATION] using to_region on an array of integers has been deprecated. Use a string instead."
29
+ return nil
25
30
  end
26
31
  end
27
32
 
@@ -1,3 +1,3 @@
1
1
  module Area
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
@@ -1,5 +1,7 @@
1
1
  require 'minitest/autorun'
2
- require 'area'
2
+ require 'minitest/benchmark' if ENV["BENCH"]
3
+ require 'minitest/pride'
4
+ require File.expand_path('../../../lib/area.rb', __FILE__)
3
5
 
4
6
  class TestInteger < MiniTest::Unit::TestCase
5
7
 
@@ -31,6 +33,14 @@ class TestInteger < MiniTest::Unit::TestCase
31
33
  assert_raises(ArgumentError) { 1234.to_gmt_offset }
32
34
  end
33
35
 
36
+ # Benchmarks
37
+
38
+ def bench_to_region
39
+ assert_performance_constant 0.9999 do |n|
40
+ n.times { 646.to_region }
41
+ end
42
+ end
43
+
34
44
  end
35
45
 
36
46
  class TestString < MiniTest::Unit::TestCase
@@ -43,7 +53,7 @@ class TestString < MiniTest::Unit::TestCase
43
53
  assert_equal "Brooklyn, NY", "11211".to_region
44
54
  end
45
55
 
46
- def test_that_it_supports_options
56
+ def test_that_it_supports_options_for_zipcodes
47
57
  assert_equal "Brooklyn", "11211".to_region(:city => true)
48
58
  assert_equal "NY", "11211".to_region(:state => true)
49
59
  end
@@ -89,35 +99,124 @@ class TestString < MiniTest::Unit::TestCase
89
99
  assert_equal "-5", "11211".to_gmt_offset
90
100
  end
91
101
 
102
+ # Benchmarks
103
+
104
+ def bench_area_code_to_region
105
+ assert_performance_constant 0.9999 do |n|
106
+ n.times { "646".to_region }
107
+ end
108
+ end
109
+
110
+ def bench_zip_code_to_region
111
+ assert_performance_constant 0.9999 do |n|
112
+ n.times { "11211".to_region }
113
+ end
114
+ end
115
+
116
+ def bench_zip_code_to_region_with_state_option
117
+ assert_performance_constant 0.9999 do |n|
118
+ n.times { "11211".to_region(:state => true) }
119
+ end
120
+ end
121
+
122
+ def bench_zip_code_to_region_with_city_option
123
+ assert_performance_constant 0.9999 do |n|
124
+ n.times { "11211".to_region(:city => true) }
125
+ end
126
+ end
127
+
128
+ def bench_to_area_code
129
+ assert_performance_constant 0.9999 do |n|
130
+ n.times { "NY".to_area }
131
+ end
132
+ end
133
+
134
+ def bench_to_zip_code
135
+ assert_performance_constant 0.9999 do |n|
136
+ n.times { "long island city".to_zip }
137
+ end
138
+ end
139
+
140
+ def bench_city_state_to_zip_code
141
+ assert_performance_constant 0.9999 do |n|
142
+ n.times { "long island city, ny".to_zip }
143
+ end
144
+ end
145
+
146
+ def bench_to_offset
147
+ assert_performance_constant 0.9999 do |n|
148
+ n.times { "ny".to_gmt_offset }
149
+ end
150
+ end
92
151
 
93
152
  end
94
153
 
95
154
  class TestArray < MiniTest::Unit::TestCase
96
155
 
97
156
  def test_that_it_converts_latlon_to_zip_code
98
- assert_equal "11211", [40.71209, -73.95427].to_zip
157
+ assert_equal "11211", ["40.71209", "-73.95427"].to_zip
99
158
  end
100
159
 
101
160
  def test_that_it_converts_latlon_to_region
102
- assert_equal "Brooklyn, NY", [40.71209, -73.95427].to_region
161
+ assert_equal "Brooklyn, NY", ["40.71209", "-73.95427"].to_region
103
162
  end
104
163
 
105
164
  def test_that_it_converts_latlon_to_region_with_options
106
- assert_equal "NY", [40.71209, -73.95427].to_region(:state => true)
165
+ assert_equal "NY", ["40.71209", "-73.95427"].to_region(:state => true)
107
166
  end
108
167
 
109
168
  def test_that_it_converts_latlon_to_gmt_offset
110
- assert_equal "-5", [40.71209, -73.95427].to_gmt_offset
169
+ assert_equal "-5", ["40.71209", "-73.95427"].to_gmt_offset
111
170
  end
112
171
 
113
172
  def test_that_it_handles_latlon_precision
114
- assert_equal "11211", [40.71209123228157, -73.95488409019887].to_zip
173
+ assert_equal "11211", ["40.71209123228157", "-73.95488409019887"].to_zip
115
174
  end
116
175
 
117
176
  def test_that_it_handles_incorrect_values
118
- assert_nil [12.12345, -40.23423].to_zip
119
- assert_nil [12.12345, -40.23423].to_region
120
- assert_nil [12.12345, -40.23423].to_gmt_offset
177
+ assert_nil ["12.12345", "-40.23423"].to_zip
178
+ assert_nil ["12.12345", "-40.23423"].to_region
179
+ assert_nil ["12.12345", "-40.23423"].to_gmt_offset
180
+ end
181
+
182
+ # Benchmarks
183
+
184
+ def bench_to_zip_code
185
+ assert_performance_constant 0.9999 do |n|
186
+ n.times { ["40.71209", "-73.95427"].to_zip }
187
+ end
188
+ end
189
+
190
+ def bench_to_region
191
+ assert_performance_constant 0.9999 do |n|
192
+ n.times { ["40.71209", "-73.95427"].to_region }
193
+ end
194
+ end
195
+
196
+ def bench_to_region_with_options
197
+ assert_performance_constant 0.9999 do |n|
198
+ n.times { ["40.71209", "-73.95427"].to_region(:state => true) }
199
+ end
200
+ end
201
+
202
+ def bench_to_gmt_offset
203
+ assert_performance_constant 0.9999 do |n|
204
+ n.times { ["40.71209", "-73.95427"].to_gmt_offset }
205
+ end
206
+ end
207
+
208
+ def bench_precision_handling
209
+ assert_performance_constant 0.9999 do |n|
210
+ n.times { ["40.71209123228157", "-73.95488409019887"].to_zip }
211
+ end
212
+ end
213
+
214
+ end
215
+
216
+ class TestArea < MiniTest::Unit::TestCase
217
+
218
+ def test_that_regions_is_an_array
219
+ assert_instance_of Array, Area.regions
121
220
  end
122
221
 
123
222
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: area
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
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-02-11 00:00:00.000000000 Z
12
+ date: 2013-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fastercsv