area 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,8 @@ 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"]
35
+
34
36
  def self.code?(code)
35
37
  if code.to_s.length == 3
36
38
  return code
@@ -47,4 +49,29 @@ module Area
47
49
  end
48
50
  end
49
51
 
52
+ def self.zip?(code)
53
+ if code.to_s.length == 5
54
+ return code
55
+ else
56
+ raise ArgumentError, "You must provide zip code", caller
57
+ end
58
+ end
59
+
60
+ def self.state_or_territory?(state)
61
+ if @regions.include?(state.upcase)
62
+ return state
63
+ else
64
+ raise ArgumentError, "You must provide a valid US state abbreviation or territory name", caller
65
+ end
66
+ end
67
+
68
+ def self.zip_or_territory?(state)
69
+ if @regions.include?(state.upcase) or Area.zip?(state)
70
+ return state
71
+ else
72
+ raise ArgumentError, "You must provide a valid US state abbreviation or zipcode.", caller
73
+ end
74
+ end
75
+
76
+
50
77
  end
@@ -1,5 +1,18 @@
1
1
  class Array
2
2
 
3
+ # Public: Convert an lat/lon pair as an Array to a region.
4
+ #
5
+ # options - An optional hash indicating if you would like just the city or just the state.
6
+ #
7
+ # Examples
8
+ #
9
+ # ['40.71209', '-73.95427'].to_region
10
+ # # => "Brooklyn, NY"
11
+ #
12
+ # ['40.71209', '-73.95427'].to_region(:state => true)
13
+ # # => "NY"
14
+ #
15
+ # Returns a String representation of the lat/lon pair.
3
16
  def to_region(options = {})
4
17
  if row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
5
18
  if options[:city]
@@ -12,6 +25,14 @@ class Array
12
25
  end
13
26
  end
14
27
 
28
+ # Public: Convert a lat/lon pair to a zip code.
29
+ #
30
+ # Examples
31
+ #
32
+ # [40.71209, -73.95427].to_zip
33
+ # #=> "11211"
34
+ #
35
+ # Returns a String of converted places.
15
36
  def to_zip
16
37
  Area.zip_codes.find do |row|
17
38
  if row[3] and row[4]
@@ -28,6 +49,14 @@ class Array
28
49
  end
29
50
  end
30
51
 
52
+ # Public: Convert a lat/lon pair to its GMT offset.
53
+ #
54
+ # Examples
55
+ #
56
+ # [40.71209, -73.95427].to_gmt_offset
57
+ # #=> "-5"
58
+ #
59
+ # Returns a String representation of the GMT offset.
31
60
  def to_gmt_offset
32
61
  row = Area.zip_codes.find {|row| row[3] == self[0].to_s and row[4] == self[1].to_s }
33
62
  row[5] if row
@@ -1,5 +1,14 @@
1
1
  class Integer
2
2
 
3
+
4
+ # Public: Convert an area code or to a US state or region.
5
+ #
6
+ # Examples
7
+ #
8
+ # 646.to_region
9
+ # #=> NY
10
+ #
11
+ # Returns a String representation of the converted area code.
3
12
  def to_region(options = {})
4
13
  if Area.code?(self) # presume an area code
5
14
  row = Area.area_codes.find{|row| row.first == self.to_s }
@@ -7,29 +16,69 @@ class Integer
7
16
  end
8
17
  end
9
18
 
19
+
20
+ # Public: Convert an area code or zipcode to its latitude and longitude.
21
+ #
22
+ # Examples
23
+ #
24
+ # 11211.to_latlon
25
+ # #=> "40.71209, -73.95427"
26
+ #
27
+ # Returns a String representation of the lat/lon pair.
10
28
  def to_latlon
11
- if Area.code_or_zip?(self)
29
+ if Area.zip?(self)
30
+ warn "[DEPRECATION] using `to_latlon` with an integer representation of a zipcode is deprecated and will be removed in future versions. Please use a string instead."
12
31
  row = Area.zip_codes.find {|row| row.first == self.to_s }
13
32
  row[3] + ', ' + row[4] if row
14
33
  end
15
34
  end
16
35
 
36
+
37
+ # Public: Convert a zipcode to its latitude.
38
+ #
39
+ # Examples
40
+ #
41
+ # 11211.to_lat
42
+ # #=> "40.71209"
43
+ #
44
+ # Returns a String representation of the latitude.
17
45
  def to_lat
18
- if Area.code_or_zip?(self)
46
+ if Area.zip?(self)
47
+ warn "[DEPRECATION] using `to_lat` with an integer representation of a zipcode is deprecated and will be removed in future versions. Please use a string instead."
19
48
  row = Area.zip_codes.find {|row| row.first == self.to_s }
20
49
  row[3] if row
21
50
  end
22
51
  end
23
52
 
53
+
54
+ # Public: Convert a zipcode to its longitude.
55
+ #
56
+ # Examples
57
+ #
58
+ # 11211.to_lon
59
+ # #=> "40.71209"
60
+ #
61
+ # Returns a String representation of the longitude.
24
62
  def to_lon
25
- if Area.code_or_zip?(self)
63
+ if Area.zip?(self)
64
+ warn "[DEPRECATION] using `to_lon` with an integer representaion of a zipcode is deprecated and will be removed in future versions. Please use a string instead."
26
65
  row = Area.zip_codes.find {|row| row.first == self.to_s }
27
66
  row[4] if row
28
67
  end
29
68
  end
30
69
 
70
+
71
+ # Public: Convert a zipcode to its GMT offset.
72
+ #
73
+ # Examples
74
+ #
75
+ # 11211.to_gmt_offset
76
+ # #=> "-5"
77
+ #
78
+ # Returns a String representation of the GMT offset.
31
79
  def to_gmt_offset
32
- if Area.code_or_zip?(self)
80
+ if Area.zip?(self)
81
+ warn "[DEPRECATION] using `to_gmt` with an integer representaion of a zipcode is deprecated and will be removed in future versions. Please use a string instead."
33
82
  row = Area.zip_codes.find {|row| row.first == self.to_s }
34
83
  row[5] if row
35
84
  end
@@ -1,12 +1,33 @@
1
1
  class String
2
2
 
3
+ # Public: Convert a string to an area code.
4
+ #
5
+ # Examples
6
+ #
7
+ # "CT".to_area
8
+ # # => ["203", "860"]
9
+ #
10
+ # Returns an Array of converted area codes.
3
11
  def to_area
4
- @area_codes = Area.area_codes.find_all {|row| row[1].upcase == self }.map {|a| a.first }
5
- @area_codes.length == 1 ? @area_codes.first : @area_codes
12
+ if Area.state_or_territory?(self)
13
+ @area_codes = Area.area_codes.find_all {|row| row[1].upcase == self }.map {|a| a.first }
14
+ end
6
15
  end
7
16
 
17
+
18
+ # Public: Convert an area code or zipcode to a US state or region.
19
+ #
20
+ # Examples
21
+ #
22
+ # "646".to_region
23
+ # #=> NY
24
+ #
25
+ # "11211".to_region
26
+ # #=> "Brooklyn, NY",
27
+ #
28
+ # Returns a String of converted area codes or zipcodes.
8
29
  def to_region(options = {})
9
- if self.to_s.length == 3 # an area code
30
+ if self.to_s.length == 3 # an area code
10
31
  row = Area.area_codes.find {|row| row.first == self.to_s }
11
32
  return row.last if row
12
33
  elsif self.to_s.length == 5
@@ -26,21 +47,90 @@ class String
26
47
  end
27
48
  end
28
49
 
50
+
51
+ # Public: Convert a place to a zip code.
52
+ #
53
+ # Examples
54
+ #
55
+ # "long island city, ny".to_zip
56
+ # #=> ["11101", "11109", "11120"]
57
+ #
58
+ # "hastings on hudson".to_zip
59
+ # #=> ["10706"]
60
+ #
61
+ # Returns an Array of converted places.
29
62
  def to_zip
30
63
  if self.match(',')
31
- @area = self.split(',')
32
- @area.collect! { |a| a.strip }
33
-
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 }
64
+ area = self.split(',')
65
+ area.collect! { |a| a.strip }
66
+ @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 }
35
67
  else
36
68
  @zip_codes = Area.zip_codes.find_all {|row| row[1] != nil and row[1].downcase == self.downcase }.map {|a| a.first }
37
69
  end
38
- @zip_codes.length == 1 ? @zip_codes.first : @zip_codes
39
70
  end
40
71
 
72
+
73
+ # Public: Convert a zipcode to its GMT offset.
74
+ #
75
+ # Examples
76
+ #
77
+ # "11211".to_gmt_offset
78
+ # #=> "-5"
79
+ #
80
+ # Returns a String representation of the GMT offset.
41
81
  def to_gmt_offset
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
82
+ if Area.zip_or_territory?(self.to_s)
83
+ row = Area.zip_codes.find {|row| row[2] != nil and (row[2].upcase == self.to_s.upcase or row[0] == self.to_s) }
84
+ row[5] if row
85
+ end
86
+ end
87
+
88
+
89
+ # Public: Convert a zipcode to its latitude and longitude.
90
+ #
91
+ # Examples
92
+ #
93
+ # "11211".to_latlon
94
+ # #=> "40.71209, -73.95427"
95
+ #
96
+ # Returns a String representation of the lat/lon pair.
97
+ def to_latlon
98
+ if Area.zip?(self)
99
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
100
+ row[3] + ', ' + row[4] if row
101
+ end
102
+ end
103
+
104
+
105
+ # Public: Convert a zipcode to its latitude.
106
+ #
107
+ # Examples
108
+ #
109
+ # "11211".to_lat
110
+ # #=> "40.71209"
111
+ #
112
+ # Returns a String representation of the latitude.
113
+ def to_lat
114
+ if Area.zip?(self)
115
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
116
+ row[3] if row
117
+ end
118
+ end
119
+
120
+
121
+ # Public: Convert a zipcode to its longitude.
122
+ #
123
+ # Examples
124
+ #
125
+ # "11211".to_lon
126
+ # #=> "40.71209"
127
+ #
128
+ # Returns a String representation of the longitude.
129
+ def to_lon
130
+ if Area.zip?(self)
131
+ row = Area.zip_codes.find {|row| row.first == self.to_s }
132
+ row[4] if row
133
+ end
44
134
  end
45
135
 
46
136
  end
@@ -1,3 +1,3 @@
1
1
  module Area
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
data/readme.md CHANGED
@@ -31,14 +31,14 @@ In your gemfile: `gem 'area'`
31
31
 
32
32
  #### Convert a state to an area code
33
33
  ```` ruby
34
- "AK".to_area #=> "907"
34
+ "AK".to_area #=> ["907"]
35
35
  "CT".to_area #=> ["203", "860"]
36
36
  ```
37
37
 
38
38
  #### Convert a place to a zip code
39
39
  ```` ruby
40
40
  "long island city, ny".to_zip #=> ["11101", "11109", "11120"]
41
- "hastings on hudson".to_zip #=> "10706"
41
+ "hastings on hudson".to_zip #=> ["10706"]
42
42
  ```
43
43
 
44
44
  #### Convert a zip code to a place
@@ -23,10 +23,6 @@ class TestInteger < MiniTest::Unit::TestCase
23
23
  assert_equal "-5", 11211.to_gmt_offset
24
24
  end
25
25
 
26
- def test_that_it_converts_zip_code_string_to_gmt_offset
27
- assert_equal "-5", "11211".to_gmt_offset
28
- end
29
-
30
26
  def test_that_it_handles_bad_area_codes
31
27
  assert_raises(ArgumentError) { 1234.to_region }
32
28
  assert_raises(ArgumentError) { 1234.to_latlon }
@@ -53,12 +49,12 @@ class TestString < MiniTest::Unit::TestCase
53
49
  end
54
50
 
55
51
  def test_that_it_converts_to_area_code
56
- assert_equal "907", "AK".to_area
52
+ assert_equal ["907"], "AK".to_area
57
53
  assert_equal ["203", "860"], "CT".to_area
58
54
  end
59
55
 
60
56
  def test_that_it_converts_to_zip_code
61
- assert_equal "10706", "hastings on hudson".to_zip
57
+ assert_equal ["10706"], "hastings on hudson".to_zip
62
58
  assert_equal ["11101", "11109", "11120"], "long Island city".to_zip
63
59
  end
64
60
 
@@ -73,10 +69,27 @@ class TestString < MiniTest::Unit::TestCase
73
69
  def test_that_it_handles_incorrect_zips
74
70
  assert_equal [], "9888".to_zip
75
71
  assert_raises(ArgumentError) { "9888".to_region }
76
- assert_equal [], "9888".to_area
77
- assert_nil "9888".to_gmt_offset
72
+ assert_raises(ArgumentError) { "9888".to_area }
73
+ assert_raises(ArgumentError) { "9888".to_gmt_offset }
74
+ end
75
+
76
+ def test_that_it_converts_zip_code_to_latlon
77
+ assert_equal "40.71209, -73.95427", "11211".to_latlon
78
+ end
79
+
80
+ def test_that_it_converts_zip_code_to_lat
81
+ assert_equal "40.71209", "11211".to_lat
82
+ end
83
+
84
+ def test_that_it_converts_zip_code_to_lon
85
+ assert_equal "-73.95427", "11211".to_lon
78
86
  end
79
87
 
88
+ def test_that_it_converts_zip_code_to_gmt_offset
89
+ assert_equal "-5", "11211".to_gmt_offset
90
+ end
91
+
92
+
80
93
  end
81
94
 
82
95
  class TestArray < MiniTest::Unit::TestCase
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.8.0
4
+ version: 0.9.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-28 00:00:00.000000000 Z
12
+ date: 2013-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fastercsv