phony_number 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.
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +42 -0
- data/Rakefile +2 -0
- data/lib/phony_number/string_extensions.rb +12 -0
- data/lib/phony_number/version.rb +3 -0
- data/lib/phony_number.rb +82 -0
- data/lib/validators/phony_number_validator.rb +12 -0
- data/phony_number.gemspec +20 -0
- metadata +79 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Joost Hietbrink
|
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
|
+
# PhonyNumber
|
2
|
+
|
3
|
+
This Gem adds useful methods to your Rails app to validate, display and save phone numbers.
|
4
|
+
It uses the super awesome Phony gem (https://github.com/floere/phony).
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'phony_number'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install phony_number
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
In your model add:
|
23
|
+
|
24
|
+
class SomeModel < ActiveRecord::Base
|
25
|
+
phony_normalize_numbers :phone_number, :default_country_code => 'US'
|
26
|
+
end
|
27
|
+
|
28
|
+
Use the Phony.plausible method to validate an attribute:
|
29
|
+
|
30
|
+
validate :phone_number, :phony_number => true
|
31
|
+
|
32
|
+
In your views use:
|
33
|
+
|
34
|
+
<%= "some number string variable".phony_formatted(:format => :international, :spaces => '-') %>
|
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 'Added 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,12 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
# Add a method to the String class so we can easily format phone numbers.
|
4
|
+
# This enables:
|
5
|
+
# "31612341234".phony_formatted # => '06 12341234'
|
6
|
+
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
|
7
|
+
def phony_formatted(options = {})
|
8
|
+
options[:format] ||= :national
|
9
|
+
Phony.formatted(self, options)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
data/lib/phony_number.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'phony'
|
2
|
+
require "phony_number/string_extensions"
|
3
|
+
require "phony_number/version"
|
4
|
+
|
5
|
+
module PhonyNumber
|
6
|
+
|
7
|
+
# Quick fix to get country_phone_number (phone number) for all relevant countries.
|
8
|
+
# TODO: Replace with some gem or something.
|
9
|
+
COUNTRY_NUMBER = {
|
10
|
+
'NL' => '31',
|
11
|
+
'BE' => '32',
|
12
|
+
'DE' => '49',
|
13
|
+
'GB' => '44',
|
14
|
+
'FR' => '33',
|
15
|
+
'ES' => '34',
|
16
|
+
'IT' => '39',
|
17
|
+
'US' => '1',
|
18
|
+
'AU' => '61',
|
19
|
+
'LU' => '352'
|
20
|
+
}
|
21
|
+
|
22
|
+
# This method requires a country_code attribute (eg. NL) and phone_number to be set.
|
23
|
+
# Options:
|
24
|
+
# :country_code => Some code that can be used as default.
|
25
|
+
def self.normalize_number(number, options = {})
|
26
|
+
return if number.blank?
|
27
|
+
number = Phony.normalize(number) # TODO: Catch errors
|
28
|
+
if country_number = COUNTRY_NUMBER[options[:country_code] || options[:default_country_code]]
|
29
|
+
# Add country_number if missing
|
30
|
+
number = "#{country_number}#{number}" not number =~ /^(00|\+)?#{country_number}/
|
31
|
+
end
|
32
|
+
number = Phony.normalize(number)
|
33
|
+
rescue
|
34
|
+
number # If all goes wrong .. we still return the original input.
|
35
|
+
end
|
36
|
+
|
37
|
+
# This module is added to AR.
|
38
|
+
module ActiveRecordExtension
|
39
|
+
|
40
|
+
def self.extended(base)
|
41
|
+
base.send :include, InstanceMethods
|
42
|
+
base.extend ClassMethods
|
43
|
+
end
|
44
|
+
|
45
|
+
module InstanceMethods
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
# This methods sets the attribute to the normalized version.
|
50
|
+
# It also adds the country_code (number), eg. 31 for NL numbers.
|
51
|
+
def set_phony_normalized_numbers(attributes, options = {})
|
52
|
+
options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
|
53
|
+
attributes.each do |attribute|
|
54
|
+
write_attribute(attribute, PhonyNumber.normalize_number(read_attribute(attribute), options))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
module ClassMethods
|
61
|
+
|
62
|
+
# Use this method on the class level like:
|
63
|
+
# phony_normalize_numbers :phone_number, :fax_number, :default_country_code => 'NL'
|
64
|
+
#
|
65
|
+
# It checks your model object for a a country_code attribute (eg. 'NL') to do the normalizing so make sure
|
66
|
+
# you've geocoded before calling this method!
|
67
|
+
def phony_normalize_numbers(*attributes)
|
68
|
+
options = attributes.last.is_a?(Hash) ? attributes.last : {}
|
69
|
+
attributes.each do |attribute|
|
70
|
+
# Add before validation that saves a normalized version of the phone number
|
71
|
+
self.before_validation do
|
72
|
+
set_phony_normalized_numbers(attributes, options)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
ActiveRecord::Base.extend PhonyNumber::ActiveRecordExtension
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# Uses the Phony.plausible method to validate an attribute.
|
2
|
+
# Usage:
|
3
|
+
# validate :phone_number, :phony_number => true
|
4
|
+
class PhonyNumberValidator < ActiveModel::EachValidator
|
5
|
+
|
6
|
+
# Validates a String using Phony.plausible? method.
|
7
|
+
def validate_each(record, attribute, value)
|
8
|
+
return if value.blank?
|
9
|
+
record.errors[attribute] << (options[:message] || "is an invalid number") if not Phony.plausible?(value)
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/phony_number/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Joost Hietbrink"]
|
6
|
+
gem.email = ["joost@joopp.com"]
|
7
|
+
gem.description = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
|
8
|
+
gem.summary = %q{This Gem adds useful methods to your Rails app to validate, display and save phone numbers.}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "phony_number"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = PhonyNumber::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "phony", "~> 1.6.7"
|
19
|
+
gem.add_dependency "activerecord", "~> 3.0"
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: phony_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Joost Hietbrink
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: phony
|
16
|
+
requirement: &70331117147140 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.7
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70331117147140
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &70331117145660 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70331117145660
|
36
|
+
description: This Gem adds useful methods to your Rails app to validate, display and
|
37
|
+
save phone numbers.
|
38
|
+
email:
|
39
|
+
- joost@joopp.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- Gemfile
|
45
|
+
- LICENSE
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- lib/phony_number.rb
|
49
|
+
- lib/phony_number/string_extensions.rb
|
50
|
+
- lib/phony_number/version.rb
|
51
|
+
- lib/validators/phony_number_validator.rb
|
52
|
+
- phony_number.gemspec
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.17
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: This Gem adds useful methods to your Rails app to validate, display and save
|
77
|
+
phone numbers.
|
78
|
+
test_files: []
|
79
|
+
has_rdoc:
|