phonelib 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +137 -0
  3. data/Rakefile +36 -0
  4. data/data/PhoneNumberMetaData.xml +25515 -0
  5. data/data/phone_data.yml +9209 -0
  6. data/lib/phonelib/core.rb +86 -0
  7. data/lib/phonelib/phone.rb +166 -0
  8. data/lib/phonelib/validators.rb +47 -0
  9. data/lib/phonelib/version.rb +3 -0
  10. data/lib/phonelib.rb +14 -0
  11. data/lib/tasks/phonelib_tasks.rake +69 -0
  12. data/test/dummy/README.rdoc +261 -0
  13. data/test/dummy/Rakefile +7 -0
  14. data/test/dummy/app/assets/javascripts/application.js +15 -0
  15. data/test/dummy/app/assets/javascripts/phones.js +2 -0
  16. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  17. data/test/dummy/app/assets/stylesheets/phones.css +4 -0
  18. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  19. data/test/dummy/app/controllers/application_controller.rb +3 -0
  20. data/test/dummy/app/controllers/phones_controller.rb +83 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/helpers/phones_helper.rb +2 -0
  23. data/test/dummy/app/models/phone.rb +6 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/test/dummy/app/views/phones/_form.html.erb +25 -0
  26. data/test/dummy/app/views/phones/edit.html.erb +6 -0
  27. data/test/dummy/app/views/phones/index.html.erb +25 -0
  28. data/test/dummy/app/views/phones/new.html.erb +5 -0
  29. data/test/dummy/app/views/phones/show.html.erb +14 -0
  30. data/test/dummy/config/application.rb +59 -0
  31. data/test/dummy/config/boot.rb +10 -0
  32. data/test/dummy/config/database.yml +25 -0
  33. data/test/dummy/config/environment.rb +5 -0
  34. data/test/dummy/config/environments/development.rb +37 -0
  35. data/test/dummy/config/environments/production.rb +67 -0
  36. data/test/dummy/config/environments/test.rb +37 -0
  37. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/test/dummy/config/initializers/inflections.rb +15 -0
  39. data/test/dummy/config/initializers/mime_types.rb +5 -0
  40. data/test/dummy/config/initializers/secret_token.rb +7 -0
  41. data/test/dummy/config/initializers/session_store.rb +8 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/en.yml +5 -0
  44. data/test/dummy/config/routes.rb +60 -0
  45. data/test/dummy/config.ru +4 -0
  46. data/test/dummy/db/development.sqlite3 +0 -0
  47. data/test/dummy/db/migrate/20130121173847_create_phones.rb +9 -0
  48. data/test/dummy/db/migrate/20130122075331_add_possible_number_to_phone.rb +5 -0
  49. data/test/dummy/db/schema.rb +23 -0
  50. data/test/dummy/db/test.sqlite3 +0 -0
  51. data/test/dummy/log/development.log +976 -0
  52. data/test/dummy/log/test.log +4627 -0
  53. data/test/dummy/public/404.html +26 -0
  54. data/test/dummy/public/422.html +26 -0
  55. data/test/dummy/public/500.html +25 -0
  56. data/test/dummy/public/favicon.ico +0 -0
  57. data/test/dummy/script/rails +6 -0
  58. data/test/dummy/test/fixtures/phones.yml +16 -0
  59. data/test/dummy/test/functional/phones_controller_test.rb +50 -0
  60. data/test/dummy/test/unit/helpers/phones_helper_test.rb +4 -0
  61. data/test/dummy/test/unit/phone_test.rb +47 -0
  62. data/test/dummy/tmp/cache/assets/C8C/B80/sprockets%2F371bf96e99717688ed7313a0c53f4212 +0 -0
  63. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  64. data/test/dummy/tmp/cache/assets/CF0/1D0/sprockets%2F6fc757c2c8329244ca95d6909865bbc2 +0 -0
  65. data/test/dummy/tmp/cache/assets/CF2/1D0/sprockets%2F0c27dcea623d22c007e33b8bf5301181 +0 -0
  66. data/test/dummy/tmp/cache/assets/D04/B50/sprockets%2F46c36c6aa77b26dddc3117542d006b94 +0 -0
  67. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  68. data/test/dummy/tmp/cache/assets/D46/C40/sprockets%2F9b88128dae7c1e6c7b28bcf197057c21 +0 -0
  69. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  70. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  71. data/test/dummy/tmp/cache/assets/D6D/E60/sprockets%2F1e022b8d5f5e26ebefd84c611bb56952 +0 -0
  72. data/test/dummy/tmp/cache/assets/D86/1B0/sprockets%2F5525f793871e81fb2bc8ff6dc05d7d8f +0 -0
  73. data/test/dummy/tmp/cache/assets/D8B/9D0/sprockets%2F017fa706d8e37400eafdda73e151d0bc +0 -0
  74. data/test/dummy/tmp/cache/assets/DD6/070/sprockets%2F2c9fb78e23cbc06765e705f8dfc6abe1 +0 -0
  75. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  76. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  77. data/test/dummy/tmp/cache/assets/E57/9D0/sprockets%2Fdc6481f3cacaff37934cae4aa8e0ecb4 +0 -0
  78. data/test/phonelib_test.rb +67 -0
  79. data/test/test_helper.rb +17 -0
  80. metadata +247 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,137 @@
1
+ ## Phonelib
2
+
3
+ {<img src="https://travis-ci.org/daddyz/phonelib.png?branch=master" alt="Build Status" />}[http://travis-ci.org/daddyz/phonelib]
4
+
5
+ Phonelib is a gem allowing you to validate phone number. All validations are based on [Google libphonenumber](http://code.google.com/p/libphonenumber/).
6
+ Currently it can make only basic validation and still not include all Google's library functions.
7
+
8
+ ## Information
9
+
10
+ ### RDoc
11
+
12
+ RDoc documentation can be found here
13
+ http://rubydoc.info/github/daddyz/phonelib/master/frames
14
+
15
+ ### Bug reports
16
+
17
+ If you discover a problem with Evercookie gem, let us know about it.
18
+ https://github.com/daddyz/phonelib/issues
19
+
20
+ ### Example application
21
+
22
+ You can see an example of phonelib model validation working in test/dummy application of this gem
23
+
24
+ ## Getting started
25
+
26
+ Phonelib was written and tested on Rails >= 3.1. You can install it by adding in to your Gemfile with:
27
+
28
+ ```ruby
29
+ gem 'phonelib'
30
+ ```
31
+
32
+ Run the bundle command to install it.
33
+
34
+ ### ActiveRecord Integration
35
+
36
+ This gem adds validator for active record.
37
+ Basic usage:
38
+ ```ruby
39
+ validates :attribute, phone: true
40
+ ```
41
+ This will enable Phonelib validator for field "attribute". This validator checks that passed value is valid phone number.
42
+ Please note that passing blank value also fails.
43
+
44
+ Additional options:
45
+ ```ruby
46
+ validates :attribute, phone: { possible: true, allow_blank: true }
47
+ ```
48
+ `possible: true` - enables validation to check whether the passed number is a possible phone number (not strict check).
49
+ Refer to [Google libphonenumber](http://code.google.com/p/libphonenumber/) for more information on it.
50
+
51
+ `allow_blank: true` - when no value passed then validation passes
52
+
53
+ ### Basic usage
54
+
55
+ To check if phone number is valid simply run:
56
+
57
+ ```ruby
58
+ Phonelib.valid?('123456789') #returns true or false
59
+ ```
60
+
61
+ Additional methods:
62
+
63
+ ```ruby
64
+ Phonelib.valid? '123456789' # checks if passed value is valid number
65
+ Phonelib.invalid? '123456789' # checks if passed value is invalid number
66
+ Phonelib.possible? '123456789' # checks if passed value is possible number
67
+ Phonelib.impossible? '123456789' # checks if passed value is impossible number
68
+ ```
69
+
70
+ There is also option to check if provided phone is valid for specified country.
71
+ Country should be specified as two letters country code (like "US" for United States).
72
+
73
+ ```ruby
74
+ Phonelib.valid_for_country? '123456789', 'XX' # checks if passed value is valid number for specified country
75
+ Phonelib.invalid_for_country? '123456789', 'XX' # checks if passed value is invalid number for specified country
76
+ ```
77
+
78
+ Additionally you can run:
79
+
80
+ ```ruby
81
+ phone = Phonelib.parse('123456789')
82
+ ```
83
+
84
+ Returned value is object of `Phonelib::Phone` class which have following methods:
85
+
86
+ ```ruby
87
+ # basic validation methods
88
+ phone.valid?
89
+ phone.invalid?
90
+ phone.possible?
91
+ phone.impossible?
92
+
93
+ # validations for countries
94
+ phone.valid_for_country? 'XX'
95
+ phone.invalid_for_country? 'XX'
96
+ ```
97
+
98
+ You can also fetch matched valid phone types
99
+
100
+ ```ruby
101
+ phone.types # returns array of all valid types
102
+ phone.type # returns first element from array of all valid types
103
+ ```
104
+
105
+ Possible types:
106
+ * `:premiumRate` - Premium Rate
107
+ * `:tollFree` - Toll Free
108
+ * `:sharedCost` - Shared Cost
109
+ * `:voip` - VoIP
110
+ * `:personalNumber` - Personal Number
111
+ * `:pager` - Pager
112
+ * `:uan` - UAN
113
+ * `:voicemail` - VoiceMail
114
+ * `:fixedLine` - Fixed Line
115
+ * `:mobile` - Mobile
116
+ * `:fixedOrMobile` - Fixed Line or Mobile (if both mobile and fixed pattern matches)
117
+
118
+ Also you can fetch all matched countries
119
+
120
+ ```ruby
121
+ phone.countries # returns array of all matched countries
122
+ phone.country # returns first element from array of all matched countries
123
+ ```
124
+
125
+ Phone class has following attributes
126
+
127
+ ```ruby
128
+ phone.original # string that was passed as phone number
129
+ phone.sanitized # sanitized phone number (only digits left)
130
+ phone.national_number # phone number without country code
131
+ ```
132
+
133
+ ### How it works
134
+
135
+ Gem includes data from Google libphonenumber which has regex patterns for validations.
136
+ Valid patterns are more specific to phone type and country.
137
+ Possible patterns as usual are patterns with number of digits in number.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Phonelib'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ require 'rake/testtask'
26
+
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+ task :default => :test
35
+
36
+ load 'tasks/phonelib_tasks.rake'