area 0.7.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,12 +14,37 @@ 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
- AREA_CODES = CSV.read(area_path)
20
- ZIP_CODES = CSV.read(zip_path)
19
+ @area_codes = CSV.read(area_path)
20
+ @zip_codes = CSV.read(zip_path)
21
21
  else
22
- AREA_CODES = FasterCSV.parse(area_path)
23
- ZIP_CODES = FasterCSV.parse(zip_path)
22
+ @area_codes = FasterCSV.parse(area_path)
23
+ @zip_codes = FasterCSV.parse(zip_path)
24
24
  end
25
+
26
+ def self.area_codes
27
+ @area_codes
28
+ end
29
+
30
+ def self.zip_codes
31
+ @zip_codes
32
+ end
33
+
34
+ def self.code?(code)
35
+ if code.to_s.length == 3
36
+ return code
37
+ else
38
+ raise ArgumentError, "You must provide a valid area code", caller
39
+ end
40
+ end
41
+
42
+ def self.code_or_zip?(code)
43
+ if code.to_s.length == 3 or code.to_s.length == 5
44
+ return code
45
+ else
46
+ raise ArgumentError, "You must provide a valid area or zip code", caller
47
+ end
48
+ end
49
+
25
50
  end
@@ -1,22 +1,19 @@
1
1
  class Array
2
2
 
3
3
  def to_region(options = {})
4
- Area::ZIP_CODES.each do |row|
5
- if row[3] == self[0].to_s and row[4] == self[1].to_s
6
- if options[:city]
7
- return row[1]
8
- elsif options[:state]
9
- return row[2]
10
- else
11
- return row[1] + ', ' + row[2]
12
- end
4
+ if row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
5
+ if options[:city]
6
+ return row[1]
7
+ elsif options[:state]
8
+ return row[2]
9
+ else
10
+ return row[1] + ', ' + row[2]
13
11
  end
14
12
  end
15
- nil
16
13
  end
17
14
 
18
15
  def to_zip
19
- Area::ZIP_CODES.each do |row|
16
+ Area.zip_codes.find do |row|
20
17
  if row[3] and row[4]
21
18
  db_lat_len = row[3].split('.').length
22
19
  db_lon_len = row[4].split('.').length
@@ -29,16 +26,11 @@ class Array
29
26
  end
30
27
  end
31
28
  end
32
- nil
33
29
  end
34
30
 
35
31
  def to_gmt_offset
36
- Area::ZIP_CODES.each do |row|
37
- if row[3] == self[0].to_s and row[4] == self[1].to_s
38
- return row[5]
39
- end
40
- end
41
- nil
32
+ row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
33
+ row[5] if row
42
34
  end
43
35
 
44
36
  end
@@ -1,42 +1,38 @@
1
1
  class Integer
2
2
 
3
3
  def to_region(options = {})
4
- if self.to_s.length == 3 # an area code
5
- Area::AREA_CODES.each do |row|
6
- return row.last if row.first == self.to_s
7
- end
8
- else
9
- # puts "not an area code"
4
+ if Area.code?(self) # presume an area code
5
+ row = Area.area_codes.find{|row| row.first == self.to_s }
6
+ row.last if row
10
7
  end
11
- nil
12
8
  end
13
9
 
14
10
  def to_latlon
15
- Area::ZIP_CODES.each do |row|
16
- return row[3] + ', ' + row[4] if row.first == self.to_s
11
+ if Area.code_or_zip?(self)
12
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
13
+ row[3] + ', ' + row[4] if row
17
14
  end
18
- nil
19
15
  end
20
16
 
21
17
  def to_lat
22
- Area::ZIP_CODES.each do |row|
23
- return row[3] if row.first == self.to_s
18
+ if Area.code_or_zip?(self)
19
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
20
+ row[3] if row
24
21
  end
25
- nil
26
22
  end
27
23
 
28
24
  def to_lon
29
- Area::ZIP_CODES.each do |row|
30
- return row[4] if row.first == self.to_s
25
+ if Area.code_or_zip?(self)
26
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
27
+ row[4] if row
31
28
  end
32
- nil
33
29
  end
34
30
 
35
31
  def to_gmt_offset
36
- Area::ZIP_CODES.each do |row|
37
- return row[5] if row.first == self.to_s
32
+ if Area.code_or_zip?(self)
33
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
34
+ row[5] if row
38
35
  end
39
- nil
40
36
  end
41
37
 
42
38
  end
@@ -1,20 +1,16 @@
1
1
  class String
2
2
 
3
3
  def to_area
4
- @area_codes = []
5
- Area::AREA_CODES.each do |row|
6
- @area_codes.push(row.first) if row[1].upcase == self
7
- end
4
+ @area_codes = Area.area_codes.find_all {|row| row[1].upcase == self }.map {|a| a.first }
8
5
  @area_codes.length == 1 ? @area_codes.first : @area_codes
9
6
  end
10
7
 
11
8
  def to_region(options = {})
12
9
  if self.to_s.length == 3 # an area code
13
- Area::AREA_CODES.each do |row|
14
- return row.last if row.first == self.to_s
15
- end
16
- else # more than 3 digits, assume a zipcode
17
- Area::ZIP_CODES.each do |row|
10
+ row = Area.area_codes.find {|row| row.first == self.to_s }
11
+ return row.last if row
12
+ elsif self.to_s.length == 5
13
+ if row = Area.zip_codes.find {|row| row.first == self.to_s }
18
14
  if row.first == self.to_s
19
15
  if options[:city]
20
16
  return row[1]
@@ -25,34 +21,26 @@ class String
25
21
  end
26
22
  end
27
23
  end
24
+ else
25
+ raise ArgumentError, "You must provide a valid area or zip code", caller
28
26
  end
29
- nil
30
27
  end
31
28
 
32
29
  def to_zip
33
- @zip_codes = []
34
-
35
30
  if self.match(',')
36
31
  @area = self.split(',')
37
32
  @area.collect! { |a| a.strip }
38
33
 
39
- Area::ZIP_CODES.each do |row|
40
- @zip_codes.push(row[0]) if row[1] && row[1].downcase == @area[0].downcase and row[2].downcase == @area[1].downcase
41
- end
34
+ @zip_codes = Area.zip_codes.find_all {|row| row[1] && row[1].downcase == @area[0].downcase and row[2].downcase == @area[1].downcase }.map {|a| a.first }
42
35
  else
43
- Area::ZIP_CODES.each do |row|
44
- @zip_codes.push(row[0]) if row[1] and row[1].downcase == self.downcase
45
- end
36
+ @zip_codes = Area.zip_codes.find_all {|row| row[1] != nil and row[1].downcase == self.downcase }.map {|a| a.first }
46
37
  end
47
38
  @zip_codes.length == 1 ? @zip_codes.first : @zip_codes
48
39
  end
49
40
 
50
41
  def to_gmt_offset
51
- Area::ZIP_CODES.each do |row|
52
- return row[5] if row[2] != nil and
53
- (row[2].upcase == self.to_s.upcase or row[0] == self.to_s)
54
- end
55
- nil
42
+ row = Area.zip_codes.find {|row| row[2] != nil and (row[2].upcase == self.to_s.upcase or row[0] == self.to_s) }
43
+ row[5] if row
56
44
  end
57
45
 
58
46
  end
@@ -1,3 +1,3 @@
1
1
  module Area
2
- VERSION = "0.7.0"
2
+ VERSION = "0.8.0"
3
3
  end
@@ -3,7 +3,6 @@ require 'area'
3
3
 
4
4
  class TestInteger < MiniTest::Unit::TestCase
5
5
 
6
-
7
6
  def test_that_it_converts_area_code_to_region
8
7
  assert_equal "NY", 646.to_region
9
8
  end
@@ -29,11 +28,11 @@ class TestInteger < MiniTest::Unit::TestCase
29
28
  end
30
29
 
31
30
  def test_that_it_handles_bad_area_codes
32
- assert_nil(1234.to_region)
33
- assert_nil(1234.to_latlon)
34
- assert_nil(1234.to_lat)
35
- assert_nil(1234.to_lon)
36
- assert_nil(1234.to_gmt_offset)
31
+ assert_raises(ArgumentError) { 1234.to_region }
32
+ assert_raises(ArgumentError) { 1234.to_latlon }
33
+ assert_raises(ArgumentError) { 1234.to_lat }
34
+ assert_raises(ArgumentError) { 1234.to_lon }
35
+ assert_raises(ArgumentError) { 1234.to_gmt_offset }
37
36
  end
38
37
 
39
38
  end
@@ -73,7 +72,7 @@ class TestString < MiniTest::Unit::TestCase
73
72
 
74
73
  def test_that_it_handles_incorrect_zips
75
74
  assert_equal [], "9888".to_zip
76
- assert_nil "9888".to_region
75
+ assert_raises(ArgumentError) { "9888".to_region }
77
76
  assert_equal [], "9888".to_area
78
77
  assert_nil "9888".to_gmt_offset
79
78
  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.7.0
4
+ version: 0.8.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-01-27 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fastercsv