area 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ notifications:
11
+ email:
12
+ - j@okfoc.us
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/area/version', __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "area"
6
+ s.version = Area::VERSION
7
+ s.author = "Jonathan Vingiano"
8
+ s.email = "jgv@jonathanvingiano.com"
9
+ s.homepage = "http://github.com/jgv/area"
10
+ s.rubyforge_project = "area"
11
+ s.summary = "Convert a region to area code and vice versa."
12
+ s.description = "Area allows you to perform a variety of conversions between places in the United States and area codes or zip codes."
13
+ s.files = `git ls-files`.split("\n")
14
+ s.require_paths = ['lib']
15
+
16
+ s.add_dependency 'fastercsv', '~> 1.5'
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'bundler'
19
+ s.add_development_dependency 'minitest'
20
+ end
@@ -0,0 +1,32 @@
1
+ class Array
2
+
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
13
+ end
14
+ end
15
+ end
16
+
17
+ def to_zip
18
+ Area::ZIP_CODES.each do |row|
19
+ if row[3] == self[0].to_s and row[4] == self[1].to_s
20
+ return row[0]
21
+ end
22
+ end
23
+ end
24
+
25
+ def to_gmt_offset
26
+ Area::ZIP_CODES.each do |row|
27
+ if row[3] == self[0].to_s and row[4] == self[1].to_s
28
+ return row[5]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,57 @@
1
+ class Integer
2
+
3
+ def to_region(options = {})
4
+ if self.to_s.length == 3 # an area code
5
+ Area::AREA_CODES.each do |row|
6
+ if row.first == self.to_s
7
+ return row.last
8
+ end
9
+ end
10
+ else # more than 3 digits, assume a zipcode
11
+ Area::ZIP_CODES.each do |row|
12
+ if row.first == self.to_s
13
+ if options[:city]
14
+ return row[1]
15
+ elsif options[:state]
16
+ return row[2]
17
+ else
18
+ return row[1] + ', ' + row[2]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def to_latlon
26
+ Area::ZIP_CODES.each do |row|
27
+ if row.first == self.to_s
28
+ return row[3] + ', ' + row[4]
29
+ end
30
+ end
31
+ end
32
+
33
+ def to_lat
34
+ Area::ZIP_CODES.each do |row|
35
+ if row.first == self.to_s
36
+ return row[3]
37
+ end
38
+ end
39
+ end
40
+
41
+ def to_lon
42
+ Area::ZIP_CODES.each do |row|
43
+ if row.first == self.to_s
44
+ return row[4]
45
+ end
46
+ end
47
+ end
48
+
49
+ def to_gmt_offset
50
+ Area::ZIP_CODES.each do |row|
51
+ if row.first == self.to_s
52
+ return row[5]
53
+ end
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,50 @@
1
+ class String
2
+ def to_area
3
+ @area_codes = []
4
+ Area::AREA_CODES.each do |row|
5
+ if row[1].upcase == self
6
+ @area_codes.push(row.first)
7
+ end
8
+ end
9
+ if @area_codes.length == 1
10
+ return @area_codes.first
11
+ else
12
+ return @area_codes
13
+ end
14
+ end
15
+
16
+ def to_zip
17
+ @zip_codes = []
18
+
19
+ if self.match(',')
20
+ @area = self.split(',')
21
+ @area.collect! { |a| a.strip }
22
+
23
+ Area::ZIP_CODES.each do |row|
24
+ if row[1] && row[1].downcase == @area[0].downcase && row[2].downcase == @area[1].downcase
25
+ @zip_codes.push(row[0])
26
+ end
27
+ end
28
+ else
29
+ Area::ZIP_CODES.each do |row|
30
+ if row[1] and row[1].downcase == self.downcase
31
+ @zip_codes.push(row[0])
32
+ end
33
+ end
34
+ end
35
+
36
+ if @zip_codes.length == 1
37
+ return @zip_codes.first
38
+ else
39
+ return @zip_codes
40
+ end
41
+ end
42
+
43
+ def to_gmt_offset
44
+ Area::ZIP_CODES.each do |row|
45
+ if row[2].upcase == self.to_s.upcase
46
+ return row[5]
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Area
2
+ VERSION = "0.4.2"
3
+ end
@@ -0,0 +1,78 @@
1
+ require 'minitest/autorun'
2
+ require 'area'
3
+
4
+ class TestInteger < MiniTest::Unit::TestCase
5
+
6
+ def test_that_it_converts_area_code_to_region
7
+ assert_equal "NY", 646.to_region
8
+ end
9
+
10
+ def test_that_it_converts_zip_code_to_region
11
+ assert_equal "Brooklyn, NY", 11211.to_region
12
+ end
13
+
14
+ def test_that_it_supports_options
15
+ assert_equal "Brooklyn", 11211.to_region(:city => true)
16
+ assert_equal "NY", 11211.to_region(:state => true)
17
+ end
18
+
19
+ def test_that_it_converts_zip_code_to_latlon
20
+ assert_equal "40.71209, -73.95427", 11211.to_latlon
21
+ end
22
+
23
+ def test_that_it_converts_zip_code_to_lat
24
+ assert_equal "40.71209", 11211.to_lat
25
+ end
26
+
27
+ def test_that_it_converts_zip_code_to_lon
28
+ assert_equal "-73.95427", 11211.to_lon
29
+ end
30
+
31
+ def test_that_it_converts_zip_code_to_gmt_offset
32
+ assert_equal "-5", 11211.to_gmt_offset
33
+ end
34
+
35
+ end
36
+
37
+ class TestString < MiniTest::Unit::TestCase
38
+
39
+ def test_that_it_converts_to_area_code
40
+ assert_equal "907", "AK".to_area
41
+ assert_equal ["203", "860"], "CT".to_area
42
+ end
43
+
44
+ def test_that_it_converts_to_zip_code
45
+ assert_equal "10706", "hastings on hudson".to_zip
46
+ assert_equal ["11101", "11109", "11120"], "long Island city".to_zip
47
+ end
48
+
49
+ def test_that_it_converts_city_state_to_zip_code
50
+ assert_equal ["11101", "11109", "11120"], "long Island city, ny".to_zip
51
+ assert_equal ["11101", "11109", "11120"], "san jose, ca".to_zip
52
+ end
53
+
54
+ def test_that_it_converts_to_offset
55
+ assert_equal "-5", "ny".to_gmt_offset
56
+ end
57
+
58
+ end
59
+
60
+ class TestArray < MiniTest::Unit::TestCase
61
+
62
+ def test_that_it_converts_latlon_to_zip_code
63
+ assert_equal "11211", [40.71209, -73.95427].to_zip
64
+ end
65
+
66
+ def test_that_it_converts_latlon_to_region
67
+ assert_equal "Brooklyn, NY", [40.71209, -73.95427].to_region
68
+ end
69
+
70
+ def test_that_it_converts_latlon_to_region_with_options
71
+ assert_equal "NY", [40.71209, -73.95427].to_region(:state => true)
72
+ end
73
+
74
+ def test_that_it_converts_latlon_to_gmt_offset
75
+ assert_equal "-5", [40.71209, -73.95427].to_gmt_offset
76
+ end
77
+
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.4.1
4
+ version: 0.4.2
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: 2012-06-28 00:00:00.000000000 Z
12
+ date: 2012-07-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: fastercsv
@@ -82,12 +82,21 @@ executables: []
82
82
  extensions: []
83
83
  extra_rdoc_files: []
84
84
  files:
85
+ - .gitignore
86
+ - .travis.yml
87
+ - Gemfile
88
+ - MIT-LICENSE
89
+ - Rakefile
90
+ - area.gemspec
85
91
  - data/areacodes.csv
86
92
  - data/zipcodes.csv
87
93
  - lib/area.rb
88
- - MIT-LICENSE
89
- - Rakefile
94
+ - lib/area/array.rb
95
+ - lib/area/integer.rb
96
+ - lib/area/string.rb
97
+ - lib/area/version.rb
90
98
  - readme.md
99
+ - test/unit/area_test.rb
91
100
  homepage: http://github.com/jgv/area
92
101
  licenses: []
93
102
  post_install_message: