Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Joost Hietbrink committed May 1, 2012
0 parents commit 2065287
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in phony_number.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2012 Joost Hietbrink

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# PhonyNumber

This Gem adds useful methods to your Rails app to validate, display and save phone numbers.
It uses the super awesome Phony gem (https://github.com/floere/phony).

## Installation

Add this line to your application's Gemfile:

gem 'phony_number'

And then execute:

$ bundle

Or install it yourself as:

$ gem install phony_number

## Usage

In your model add:

class SomeModel <

phony_normalize_numbers :phone_number, :default_country_code => 'US'

end

Use the Phony.plausible method to validate an attribute:

validate :phone_number, :phony_number => true

In your views use:

<%= "some number string variable".phony_formatted(:format => :international, :spaces => '-') %>

## Contributing

1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
82 changes: 82 additions & 0 deletions lib/phony_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
require 'phony'
require "phony_number/string_extensions"
require "phony_number/version"

module PhonyNumber

# Quick fix to get country_phone_number (phone number) for all relevant countries.
# TODO: Replace with some gem or something.
COUNTRY_NUMBER = {
'NL' => '31',
'BE' => '32',
'DE' => '49',
'GB' => '44',
'FR' => '33',
'ES' => '34',
'IT' => '39',
'US' => '1',
'AU' => '61',
'LU' => '352'
}

# This method requires a country_code attribute (eg. NL) and phone_number to be set.
# Options:
# :country_code => Some code that can be used as default.
def self.normalize_number(number, options = {})
return if number.blank?
number = Phony.normalize(number) # TODO: Catch errors
if country_number = COUNTRY_NUMBER[options[:country_code] || options[:default_country_code]]
# Add country_number if missing
number = "#{country_number}#{number}" not number =~ /^(00|\+)?#{country_number}/
end
number = Phony.normalize(number)
rescue
number # If all goes wrong .. we still return the original input.
end

# This module is added to AR.
module ActiveRecordExtension

def self.extended(base)
base.send :include, InstanceMethods
base.extend ClassMethods
end

module InstanceMethods

private

# This methods sets the attribute to the normalized version.
# It also adds the country_code (number), eg. 31 for NL numbers.
def set_phony_normalized_numbers(attributes, options = {})
options[:country_code] ||= self.country_code if self.respond_to?(:country_code)
attributes.each do |attribute|
write_attribute(attribute, PhonyNumber.normalize_number(read_attribute(attribute), options))
end
end

end

module ClassMethods

# Use this method on the class level like:
# phony_normalize_numbers :phone_number, :fax_number, :default_country_code => 'NL'
#
# It checks your model object for a a country_code attribute (eg. 'NL') to do the normalizing so make sure
# you've geocoded before calling this method!
def phony_normalize_numbers(*attributes)
options = attributes.last.is_a?(Hash) ? attributes.last : {}
attributes.each do |attribute|
# Add before validation that saves a normalized version of the phone number
self.before_validation do
set_phony_normalized_numbers(attributes, options)
end
end
end

end

end

end
ActiveRecord::Base.extend PhonyNumber::ActiveRecordExtension
12 changes: 12 additions & 0 deletions lib/phony_number/string_extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class String

# Add a method to the String class so we can easily format phone numbers.
# This enables:
# "31612341234".phony_formatted # => '06 12341234'
# "31612341234".phony_formatted(:spaces => '-') # => '06-12341234'
def phony_formatted(options = {})
options[:format] ||= :national
Phony.formatted(self, options)
end

end
3 changes: 3 additions & 0 deletions lib/phony_number/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module PhonyNumber
VERSION = "0.0.1"
end
12 changes: 12 additions & 0 deletions lib/validators/phony_number_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Uses the Phony.plausible method to validate an attribute.
# Usage:
# validate :phone_number, :phony_number => true
class PhonyNumberValidator < ActiveModel::EachValidator

# Validates a String using Phony.plausible? method.
def validate_each(record, attribute, value)
return if value.blank?
record.errors[attribute] << (options[:message] || "is an invalid number") if not Phony.plausible?(value)
end

end
20 changes: 20 additions & 0 deletions phony_number.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/phony_number/version', __FILE__)

Gem::Specification.new do |gem|
gem.authors = ["Joost Hietbrink"]
gem.email = ["[email protected]"]
gem.description = %q{TODO: Write a gem description}
gem.summary = %q{TODO: Write a gem summary}
gem.homepage = ""

gem.files = `git ls-files`.split($\)
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.name = "phony_number"
gem.require_paths = ["lib"]
gem.version = PhonyNumber::VERSION

gem.add_dependency "phony", "~> 1.6.7"
gem.add_dependency "activerecord", "~> 3.0"
end

0 comments on commit 2065287

Please sign in to comment.