area 0.2.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Rakefile +5 -1
  2. data/data/zipcodes.csv +43205 -0
  3. data/lib/area.rb +99 -9
  4. data/readme.md +66 -6
  5. metadata +53 -6
@@ -1,27 +1,117 @@
1
- require 'csv'
1
+ if RUBY_VERSION.to_f >= 1.9 # ;_;
2
+ require 'csv'
3
+ else
4
+ require 'rubygems'
5
+ require 'faster_csv'
6
+ end
2
7
 
3
8
  module Area
4
- DATA = CSV.read(File.open(File.join(File.dirname(__FILE__), '..', 'data', 'areacodes.csv')))
5
- end
6
9
 
10
+ zip_path = File.open(File.join(File.dirname(__FILE__), '..', 'data', 'zipcodes.csv'))
11
+ area_path = File.open(File.join(File.dirname(__FILE__), '..', 'data', 'areacodes.csv'))
12
+
13
+ # there is probably a better way to do this...
14
+ if RUBY_VERSION.to_f >= 1.9
15
+ AREA_CODES = CSV.read(area_path)
16
+ ZIP_CODES = CSV.read(zip_path)
17
+ else
18
+ AREA_CODES = FasterCSV.parse(area_path)
19
+ ZIP_CODES = FasterCSV.parse(zip_path)
20
+ end
21
+ end
22
+
7
23
  class Integer
8
- def to_region
9
- Area::DATA.each do |row|
24
+ def to_region(options = {})
25
+ if self.to_s.length == 3 # an area code
26
+ Area::AREA_CODES.each do |row|
27
+ if row.first == self.to_s
28
+ return row.last
29
+ end
30
+ end
31
+ else # more than 3 digits, assume a zipcode
32
+ Area::ZIP_CODES.each do |row|
33
+ if row.first == self.to_s
34
+ if options[:city]
35
+ return row[1]
36
+ elsif options[:state]
37
+ return row[2]
38
+ else
39
+ return row[1] + ', ' + row[2]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ def to_latlon
47
+ Area::ZIP_CODES.each do |row|
48
+ if row.first == self.to_s
49
+ return row[3] + ', ' + row[4]
50
+ end
51
+ end
52
+ end
53
+
54
+ def to_lat
55
+ Area::ZIP_CODES.each do |row|
56
+ if row.first == self.to_s
57
+ return row[3]
58
+ end
59
+ end
60
+ end
61
+
62
+ def to_lon
63
+ Area::ZIP_CODES.each do |row|
10
64
  if row.first == self.to_s
11
- return row.last
65
+ return row[4]
12
66
  end
13
67
  end
14
68
  end
69
+
70
+ def to_gmt_offset
71
+ Area::ZIP_CODES.each do |row|
72
+ if row.first == self.to_s
73
+ return row[5]
74
+ end
75
+ end
76
+ end
77
+
15
78
  end
16
79
 
17
80
  class String
18
81
  def to_area
19
82
  @area_codes = []
20
- Area::DATA.each do |row|
21
- if row[1] == self
83
+ Area::AREA_CODES.each do |row|
84
+ if row[1].upcase == self
22
85
  @area_codes.push(row.first)
23
86
  end
24
87
  end
25
- @area_codes
88
+ if @area_codes.length == 1
89
+ return @area_codes.first
90
+ else
91
+ return @area_codes
92
+ end
93
+ end
94
+
95
+ def to_zip
96
+ @zip_codes = []
97
+ Area::ZIP_CODES.each do |row|
98
+ if row[1] and row[1].downcase == self.downcase
99
+ @zip_codes.push(row.first)
100
+ end
101
+ end
102
+ if @zip_codes.length == 1
103
+ return @zip_codes.first
104
+ else
105
+ return @zip_codes
106
+ end
26
107
  end
108
+
109
+ def to_gmt_offset
110
+ Area::ZIP_CODES.each do |row|
111
+ if row[2].upcase == self.to_s.upcase
112
+ return row[5]
113
+ end
114
+ end
115
+ end
116
+
27
117
  end
data/readme.md CHANGED
@@ -2,20 +2,80 @@
2
2
 
3
3
  # Area
4
4
 
5
- Hi. This gem allows you to convert a region in the United States to an area code and vice versa. Area uses public data and does not rely on any external services or have any dependencies.
5
+ Hi. This gem allows you to perform the following conversions:
6
6
 
7
- ## Usage
7
+ * An area code to a region (state)
8
+ * A state to an area code
9
+ * A zip code to a place (control granularity with city and state options)
10
+ * A zipcode to a lat/lon
11
+ * A zipcode to just a lat
12
+ * A zipcode to just a lon
13
+ * A zipcode to its GMT offset
14
+ * A state to its GMT offset
15
+
16
+ Area uses public domain data and does not rely on any external services.
17
+
18
+ ## Installation
8
19
 
9
20
  In your gemfile: `gem 'area'`
10
21
 
11
- In your code:
22
+ ## Usaage
12
23
 
24
+ #### Convert an area code to a state/region
13
25
  ``` ruby
14
26
  646.to_region #=> NY
15
- "AK".to_area #=> ["907"]
27
+ ```
28
+
29
+ #### Convert a state to an area code
30
+ ```` ruby
31
+ "AK".to_area #=> "907"
16
32
  "CT".to_area #=> ["203", "860"]
17
33
  ```
18
34
 
19
- ### Testing and Contributing
35
+ #### Convert a zip code to a place
36
+ ```` ruby
37
+ "11211".to_region #=> "Brooklyn, NY"
38
+ "11211".to_region(:city => true) #=> "Brooklyn"
39
+ "11211".to_region(:state => true) #=> "NY"
40
+ ```
41
+
42
+ #### Convert a zip code to a lat/lon
43
+ ```` ruby
44
+ "11211".to_latlon #=> "40.71209, -73.95427"
45
+ ```
46
+
47
+ #### Convert a zip code to a lat
48
+ ```` ruby
49
+ "11211".to_lat #=> "40.71209"
50
+ ```
51
+
52
+ #### Convert a zip code to a lon
53
+ ```` ruby
54
+ "11211".to_lon #=> "40.71209, -73.95427"
55
+ ```
56
+
57
+ #### Get the GMT offset of a zipcode
58
+ ```` ruby
59
+ "11211".to_gmt_offset #=> "-5"
60
+ ```
61
+
62
+ #### Get the GMT offset of a state
63
+ ```` ruby
64
+ "NY".to_gmt_offset #=> "-5" # by state
65
+ ```
66
+
67
+ ## Testing and Contributing and Stuff
68
+
69
+ Contributions are more than welcome. I'm [testing](http://travis-ci.org/jgv/area) with minitest. This gem supports:
70
+
71
+ * Ruby 1.8.7
72
+ * Ruby 1.9.2
73
+ * Ruby 1.9.3
74
+ * JRuby 1.8
75
+ * JRuby 1.9
76
+ * Rubinius 1.8
77
+ * Rubinius 1.9
78
+
79
+ ## Copyright
20
80
 
21
- Contributions are more than welcome. I'm testing with minitest.
81
+ Copyright (c) 2012 Jonathan Vingiano. See [LICENSE](https://github.com/jgv/area/blob/master/MIT-LICENSE) for details.
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.2.2
4
+ version: '0.3'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,6 +11,22 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2012-06-23 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fastercsv
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.5'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: rake
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -27,16 +43,47 @@ dependencies:
27
43
  - - ! '>='
28
44
  - !ruby/object:Gem::Version
29
45
  version: '0'
30
- description: Area allows you to convert a region in the United States to an area code
31
- and vice versa. Area uses public data and does not rely on any external services
32
- or have any dependencies.
33
- email:
34
- - jgv@jonathanvingiano.com
46
+ - !ruby/object:Gem::Dependency
47
+ name: bundler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: minitest
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Area allows you to perform a variety of conversions between places in
79
+ the United States and area codes or zip codes.
80
+ email: jgv@jonathanvingiano.com
35
81
  executables: []
36
82
  extensions: []
37
83
  extra_rdoc_files: []
38
84
  files:
39
85
  - data/areacodes.csv
86
+ - data/zipcodes.csv
40
87
  - lib/area.rb
41
88
  - MIT-LICENSE
42
89
  - Rakefile