Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
GlobalPhone parses, validates, and formats local and international phone numbers according to the E.164 standard.
Store and display phone numbers in your app. Accept phone number input in national or international format. Convert phone numbers to international strings (+13125551212
) for storage and retrieval. Present numbers in national format ((312) 555-1212
) in your UI.
Designed with the future in mind. GlobalPhone uses format specifications from Google's open-source libphonenumber database. No need to upgrade the library when a new phone format is introduced—just generate a new copy of the database and check it into your app.
Pure Ruby. No dependencies. GlobalPhone is designed for Ruby 1.9.3 and up. (Works in 1.8.7, too—just bring your own json
gem.)
Add the global_phone
gem to your app. For example, using Bundler:
$ echo "gem 'global_phone'" >> Gemfile
$ bundle install
Use global_phone_dbgen
to convert Google's libphonenumber PhoneNumberMetaData.xml
file into a JSON database for GlobalPhone.
$ gem install global_phone_dbgen
$ global_phone_dbgen > db/global_phone.json
Tell GlobalPhone where to find the database. For example, in a Rails app, create an initializer in config/initializers/global_phone.rb
:
require 'global_phone'
GlobalPhone.db_path = Rails.root.join('db/global_phone.json')
Parse an international number string into a GlobalPhone::Number
object:
number = GlobalPhone.parse('+1-312-555-1212')
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
Query the country code and likely territory name of the number:
number.country_code
# => "1"
number.territory.name
# => "US"
Present the number in national and international formats:
number.national_format
# => "(312) 555-1212"
number.international_format
# => "+1 312-555-1212"
Is the number valid? (Note: this is not definitive. For example, the number here is "valid" by format, but there are no US numbers that start with 555. The valid?
method may return false positives, but should not return false negatives unless the database is out of date.)
number.valid?
# => true
Get the number's normalized E.164 international string:
number.international_string
# => "+13125551212"
Parse a number in national format for a given territory:
number = GlobalPhone.parse("(0) 20-7031-3000", :gb)
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=44 name=GB> national_string="2070313000">
Parse an international number using a territory's international dialing prefix:
number = GlobalPhone.parse("00 1 3125551212", :gb)
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=1 name=US> national_string="3125551212">
Set the default territory to Great Britain (territory names are ISO 3166-1 Alpha-2 codes):
GlobalPhone.default_territory_name = :gb
# => :gb
GlobalPhone.parse("(0) 20-7031-3000")
# => #<GlobalPhone::Number territory=#<GlobalPhone::Territory country_code=44 name=GB> national_string="2070313000">
Shortcuts for validating a phone number:
GlobalPhone.validate("+1 312-555-1212")
# => true
GlobalPhone.validate("+442070313000")
# => true
GlobalPhone.validate("(0) 20-7031-3000")
# => false
GlobalPhone.validate("(0) 20-7031-3000", :gb)
# => true
Shortcuts for normalizing a phone number in E.164 format:
GlobalPhone.normalize("(312) 555-1212")
# => "+13125551212"
GlobalPhone.normalize("+442070313000")
# => "+442070313000"
GlobalPhone.normalize("(0) 20-7031-3000")
# => nil
GlobalPhone.normalize("(0) 20-7031-3000", :gb)
# => "+442070313000"
GlobalPhone currently does not parse emergency numbers or SMS short code numbers.
Validation is not definitive and may return false positives, but should not return false negatives unless the database is out of date.
Territory heuristics are imprecise. Parsing a number will usually result in the territory being set to the primary territory of the region. For example, Canadian numbers will be parsed with a territory of US
. (In most cases this does not matter, but if your application needs to perform geolocation using phone numbers, GlobalPhone may not be a good fit.)
The GlobalPhone source code is hosted on GitHub. You can check out a copy of the latest code using Git:
$ git clone https://github.com/sstephenson/global_phone.git
If you've found a bug or have a question, please open an issue on the issue tracker. Or, clone the GlobalPhone repository, write a failing test case, fix the bug, and submit a pull request.
GlobalPhone is heavily inspired by Andreas Gal's PhoneNumber.js library.
1.0.1 (May 29, 2013)
1.0.0 (May 28, 2013)
Copyright © 2013 Sam Stephenson
Released under the MIT license. See LICENSE
for details.
FAQs
Unknown package
We found that globalphone demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.