area_codex 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in area_codex.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Chris Cummer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # AreaCodex
2
+
3
+ A ruby gem for determining whether or not a given phone area code is in
4
+ a particular country or region.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'area_codex'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install area_codex
19
+
20
+ ## Usage
21
+
22
+ ### By Country
23
+
24
+ Currently only Canada, Mexico and the United States are supported.
25
+
26
+ AreaCodex::Country.new(:canada).include?(604) # => true
27
+ AreaCodex::Country.new(:canada).exclude?(213) # => true
28
+
29
+ ### By Region
30
+
31
+ Currently only North America is supported.
32
+
33
+ AreaCodex::Region.new(:north_america).include?(604) # => true
34
+ AreaCodex::Region.new(:north_america).exclude?(213) # => false
35
+
36
+ ## Contributing
37
+
38
+ 1. Fork it
39
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
40
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
41
+ 4. Push to the branch (`git push origin my-new-feature`)
42
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'lib/area_codex'
7
+ t.test_files = FileList['test/lib/area_codex/*_test.rb']
8
+ t.verbose = true
9
+ end
10
+ task default: :test
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ require 'area_codex/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "area_codex"
9
+ gem.version = AreaCodex::VERSION
10
+ gem.authors = ["Chris Cummer"]
11
+ gem.email = ["chris@unbounce.com"]
12
+ gem.description = %q{Phone number area code lookup by region.}
13
+ gem.summary = %q{A list of phone number area codes by country. Currently only supports Canada, the United States, and Mexico.}
14
+ gem.homepage = ""
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'shoulda'
23
+ end
data/lib/area_codex.rb ADDED
@@ -0,0 +1,9 @@
1
+ module AreaCodex
2
+ end
3
+
4
+ require 'area_codex/concerns/with_area_codes'
5
+ require 'area_codex/base'
6
+ require 'area_codex/area_code_list'
7
+ require 'area_codex/country'
8
+ require 'area_codex/region'
9
+ require 'area_codex/version'
@@ -0,0 +1,34 @@
1
+ require 'set'
2
+
3
+ module AreaCodex
4
+ class AreaCodeList
5
+ extend Forwardable
6
+
7
+ attr_reader :area_codes
8
+
9
+ def initialize(*area_code_files)
10
+ @area_codes = Set.new
11
+ load_area_codes(area_code_files.flatten)
12
+ end
13
+
14
+ def_delegator :@area_codes, :include?, :include?
15
+
16
+ def exclude?(area_code)
17
+ !include?(area_code)
18
+ end
19
+
20
+ private
21
+
22
+ def load_area_codes(area_code_files)
23
+ area_code_files.each do |file_name|
24
+ file_path = File.expand_path("../../data/#{file_name}", __FILE__)
25
+
26
+ if File.exists?( file_path )
27
+ File.open( file_path ) do |file|
28
+ @area_codes.merge( Set.new( file.readlines.map { |code| code.chop.to_i } ) )
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ module AreaCodex
2
+ class Base
3
+
4
+ def initialize
5
+ @area_code_list = AreaCodeList.new(area_code_files)
6
+ end
7
+
8
+ def area_code_files
9
+ raise 'Subclasses must define this method.'
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module AreaCodex
2
+ module WithAreaCodes
3
+ extend Forwardable
4
+
5
+ def_delegator :@area_code_list, :include?, :include?
6
+ def_delegator :@area_code_list, :exclude?, :exclude?
7
+ def_delegator :@area_code_list, :area_codes, :area_codes
8
+ end
9
+ end
@@ -0,0 +1,20 @@
1
+ module AreaCodex
2
+ class Country < Base
3
+ include WithAreaCodes
4
+
5
+ def initialize(name)
6
+ @name = normalize(name)
7
+ super()
8
+ end
9
+
10
+ private
11
+
12
+ def normalize(country_name)
13
+ country_name.to_s.downcase.gsub(' ', '_').to_sym
14
+ end
15
+
16
+ def area_code_files
17
+ "#{@name}.txt"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ module AreaCodex
2
+ class Region < Base
3
+ include WithAreaCodes
4
+
5
+ NORTH_AMERICA = [:canada, :mexico, :united_states]
6
+
7
+ def initialize(name)
8
+ @name = constantize(name)
9
+ super()
10
+ end
11
+
12
+ private
13
+
14
+ def constantize(region_name)
15
+ name = region_name.to_s.upcase.gsub(' ', '_')
16
+ klass = self.class
17
+ klass.const_defined?(name) ? klass.const_get(name) : klass.const_missing(name)
18
+ end
19
+
20
+ def area_code_files
21
+ @name.map { |sym| "#{sym}.txt" }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module AreaCodex
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ 204
2
+ 226
3
+ 236
4
+ 250
5
+ 289
6
+ 306
7
+ 403
8
+ 416
9
+ 418
10
+ 431
11
+ 438
12
+ 450
13
+ 506
14
+ 514
15
+ 519
16
+ 581
17
+ 587
18
+ 604
19
+ 613
20
+ 647
21
+ 705
22
+ 709
23
+ 778
24
+ 807
25
+ 819
26
+ 867
27
+ 902
28
+ 905
@@ -0,0 +1,21 @@
1
+ 222
2
+ 229
3
+ 322
4
+ 444
5
+ 461
6
+ 508
7
+ 614
8
+ 622
9
+ 656
10
+ 664
11
+ 665
12
+ 669
13
+ 671
14
+ 682
15
+ 686
16
+ 744
17
+ 833
18
+ 871
19
+ 987
20
+ 998
21
+ 999
@@ -0,0 +1,83 @@
1
+ 201
2
+ 202
3
+ 207
4
+ 208
5
+ 211
6
+ 217
7
+ 218
8
+ 231
9
+ 242
10
+ 246
11
+ 278
12
+ 285
13
+ 302
14
+ 304
15
+ 307
16
+ 308
17
+ 309
18
+ 311
19
+ 315
20
+ 320
21
+ 341
22
+ 345
23
+ 369
24
+ 401
25
+ 402
26
+ 406
27
+ 411
28
+ 413
29
+ 417
30
+ 442
31
+ 456
32
+ 470
33
+ 475
34
+ 500
35
+ 507
36
+ 509
37
+ 511
38
+ 518
39
+ 555
40
+ 600
41
+ 603
42
+ 605
43
+ 607
44
+ 608
45
+ 611
46
+ 618
47
+ 627
48
+ 628
49
+ 649
50
+ 670
51
+ 684
52
+ 700
53
+ 701
54
+ 710
55
+ 711
56
+ 712
57
+ 715
58
+ 719
59
+ 740
60
+ 764
61
+ 800
62
+ 802
63
+ 806
64
+ 808
65
+ 811
66
+ 812
67
+ 814
68
+ 831
69
+ 849
70
+ 869
71
+ 870
72
+ 880
73
+ 881
74
+ 882
75
+ 898
76
+ 900
77
+ 906
78
+ 907
79
+ 908
80
+ 911
81
+ 927
82
+ 957
83
+ 976
@@ -0,0 +1,34 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestAreaCodeList < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @single = 'canada.txt'
7
+ @multiple = ['canada.txt', 'mexico.txt']
8
+ end
9
+
10
+ def list(file_names)
11
+ AreaCodex::AreaCodeList.new(file_names)
12
+ end
13
+
14
+ def test_initialize
15
+ [@single, @multiple].each do |list_size|
16
+ assert_equal( true, list(list_size).area_codes.instance_of?(Set) )
17
+ assert_equal( false, list(list_size).area_codes.empty? )
18
+ end
19
+ end
20
+
21
+ def test_include?
22
+ [@single, @multiple].each do |list_size|
23
+ assert_equal( true, list(list_size).include?(604) )
24
+ assert_equal( false, list(list_size).include?(666) )
25
+ end
26
+ end
27
+
28
+ def test_exclude?
29
+ [@single, @multiple].each do |list_size|
30
+ assert_equal( false, list(list_size).exclude?(604) )
31
+ assert_equal( true, list(list_size).exclude?(666) )
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,39 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestCounty < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @fake_name = 'Freeside'
7
+ @real_name = 'Canada'
8
+ end
9
+
10
+ def country(name)
11
+ AreaCodex::Country.new(name)
12
+ end
13
+
14
+ def test_initialize
15
+ assert_equal( true, country(@fake_name).instance_of?(AreaCodex::Country) )
16
+ assert_equal( true, country(@real_name).instance_of?(AreaCodex::Country) )
17
+ end
18
+
19
+ def test_include?
20
+ [@fake_name, @fake_name.to_sym].each do |name|
21
+ assert_equal( false, country(name).include?(604) )
22
+ assert_equal( false, country(name).include?(666) )
23
+ end
24
+
25
+ [@real_name, @real_name.to_s].each do |name|
26
+ assert_equal( true, country(name).include?(604) )
27
+ assert_equal( false, country(name).include?(666) )
28
+ end
29
+ end
30
+
31
+ def test_exclude?
32
+ assert_equal( true, country(@fake_name).exclude?(604) )
33
+ assert_equal( true, country(@fake_name).exclude?(666) )
34
+
35
+ assert_equal( false, country(@real_name).exclude?(604) )
36
+ assert_equal( true, country(@real_name).exclude?(666) )
37
+ end
38
+
39
+ end
@@ -0,0 +1,32 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestRegion < MiniTest::Unit::TestCase
4
+
5
+ def setup
6
+ @fake_name = 'Freeside'
7
+ @real_name = 'North America'
8
+ end
9
+
10
+ def region(name)
11
+ AreaCodex::Region.new(name)
12
+ end
13
+
14
+ def test_initialize
15
+ assert_raises( NameError ) { AreaCodex::Region.new(@fake_name) }
16
+ assert_equal( true, region(@real_name).instance_of?(AreaCodex::Region) )
17
+ end
18
+
19
+ def test_include?
20
+ [@real_name, @real_name.to_sym].each do |name|
21
+ assert_equal( true, region(name).include?(604) )
22
+ assert_equal( false, region(name).include?(666) )
23
+ end
24
+ end
25
+
26
+ def test_exclude?
27
+ [@real_name, @real_name.to_sym].each do |name|
28
+ assert_equal( false, region(name).exclude?(604) )
29
+ assert_equal( true, region(name).exclude?(666) )
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class TestVersion < MiniTest::Unit::TestCase
4
+
5
+ def test_version
6
+ assert_equal( false, AreaCodex::VERSION.nil? )
7
+ end
8
+
9
+ end
@@ -0,0 +1,20 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+
4
+ require File.expand_path('../../lib/area_codex.rb', __FILE__)
5
+
6
+ require 'rubygems'
7
+ require 'bundler'
8
+
9
+ begin
10
+ Bundler.setup(:default, :development)
11
+ rescue Bundler::BundlerError => e
12
+ $stderr.puts e.message
13
+ $stderr.puts "Run `bundle install` to install missing gems"
14
+ exit e.status_code
15
+ end
16
+
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require 'area_codex'
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: area_codex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Cummer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: shoulda
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Phone number area code lookup by region.
47
+ email:
48
+ - chris@unbounce.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - area_codex.gemspec
59
+ - lib/area_codex.rb
60
+ - lib/area_codex/area_code_list.rb
61
+ - lib/area_codex/base.rb
62
+ - lib/area_codex/concerns/with_area_codes.rb
63
+ - lib/area_codex/country.rb
64
+ - lib/area_codex/region.rb
65
+ - lib/area_codex/version.rb
66
+ - lib/data/canada.txt
67
+ - lib/data/mexico.txt
68
+ - lib/data/united_states.txt
69
+ - test/lib/area_codex/area_code_list_test.rb
70
+ - test/lib/area_codex/country_test.rb
71
+ - test/lib/area_codex/region_test.rb
72
+ - test/lib/area_codex/version_test.rb
73
+ - test/test_helper.rb
74
+ homepage: ''
75
+ licenses: []
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ segments:
87
+ - 0
88
+ hash: -2225134602112427650
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ segments:
96
+ - 0
97
+ hash: -2225134602112427650
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: A list of phone number area codes by country. Currently only supports Canada,
104
+ the United States, and Mexico.
105
+ test_files:
106
+ - test/lib/area_codex/area_code_list_test.rb
107
+ - test/lib/area_codex/country_test.rb
108
+ - test/lib/area_codex/region_test.rb
109
+ - test/lib/area_codex/version_test.rb
110
+ - test/test_helper.rb