Code Monkey home page Code Monkey logo

mini_phone's Introduction

MiniPhone

A Ruby gem which plugs directly into Google's native C++ libphonenumber for extremely fast and robust phone number parsing, validation, and formatting. On average, most methods are 70x to 80x faster than other Ruby phone number libraries.

Usage

Checking if a phone number is valid

MiniPhone.valid?('+14043841399')                     # true
MiniPhone.valid_for_country?('(404) 384-1399', 'US') # true
MiniPhone.valid_for_country?('(404) 384-1399', 'GB') # false

Formatting a number

MiniPhone.default_country = 'US'

phone_number = MiniPhone.parse('404-384-1399')

phone_number.e164                 # +14043841399
phone_number.national             # (404) 384-1399
phone_number.raw_national         # 4043841399
phone_number.dasherized_national  # 404-384-1399
phone_number.international        # +1 404-384-1399
phone_number.rfc3966              # tel:+1-404-384-1384

Checking if a phone number is possible

phone_number = MiniPhone.parse('-12')

phone_number.possible? # false

Getting the type of a phone number

MiniPhone.parse('+12423570000').type # :mobile
MiniPhone.parse('+12423651234').type # :fixed_line

The possible types are directly mapped from this enum:

:fixed_line
:mobile
:fixed_line_or_mobile
:toll_free
:premium_rate
:shared_cost
:voip
:personal_number
:pager
:uan
:voicemail
:unknown

Compatibility with PhoneLib

MiniPhone aims to be compatible with Phonelib. In many cases it can be a drop in replacement. It has a smaller feature set, so it is not a replacement for every use case. If there is a feature you need, open an issue and we will try to support it.

Benchmarks

On average, most methods are 40x to 50x faster than other libraries. To run the benchmarks locally, execute: bundle exec rake bench

Results from my Linux (5.10.6-arch1-10)

Comparison:
   MiniPhone:    valid?:    33382.0 i/s
    Phonelib:    valid?:      422.8 i/s - 78.95x  (± 0.00) slower
TelephoneNumber: valid?:      164.3 i/s - 203.13x  (± 0.00) slower

Comparison:
     MiniPhone:  e164:    41187.5 i/s
      Phonelib:  e164:      579.0 i/s - 71.14x  (± 0.00) slower
TelephoneNumber: e164:      228.8 i/s - 179.99x  (± 0.00) slower

Installation

  1. Install libphonenumber

    brew install libphonenumber # mac
    apt-get install -y libphonenumber-dev # debian / ubuntu
  2. Add this line to your application's Gemfile:

    gem 'mini_phone'
  3. And then execute:

    bundle install

    Or install it yourself as:

    gem install mini_phone

Installation on Heroku

  1. In addition to the steps above add the apt buildpack to your Heroku app:

    heroku buildpacks:add --index 1 heroku-community/apt
  2. Create Aptfile in your repo with the following content:

    libphonenumber-dev
    

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ianks/mini_phone. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the MiniPhone project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

mini_phone's People

Contributors

espen avatar ianks avatar psparrow avatar remcodm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mini_phone's Issues

Unable to locate package libphonenumber on Ubuntu

Any hints? it seems libphonenumber is no longer in ubuntu repos

apt-get install -y libphonenumber
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package libphonenumber

Can't install on apple silicon

I just upgraded to a M1 Pro mac and I've been having trouble getting this to work. Homebrew installs libphonenumber fine but when I run bundle install I get the "It looks like libphonenumber is not installed on this system" message. I tried adding both of the following (one at a time) to my ~/.bundle/config to help it find the library:

BUNDLE_BUILD__MINI_PHONE: "--with-opt-dir=/opt/homebrew/opt/libphonenumber"
BUNDLE_BUILD__MINI_PHONE: "--with-phonenumber-dir=/opt/homebrew/opt/libphonenumber"

In both cases, I got the following:

current directory: /Users/username/.asdf/installs/ruby/2.7.4/lib/ruby/gems/2.7.0/gems/mini_phone-1.1.7/ext/mini_phone
/Users/username/.asdf/installs/ruby/2.7.4/bin/ruby -I /Users/username/.asdf/installs/ruby/2.7.4/lib/ruby/2.7.0 -r ./siteconf20211027-46412-gqe3fb.rb extconf.rb --with-phonenumber-dir\=/opt/homebrew/opt/libphonenumber
checking for -lphonenumber... yes
creating Makefile

current directory: /Users/username/.asdf/installs/ruby/2.7.4/lib/ruby/gems/2.7.0/gems/mini_phone-1.1.7/ext/mini_phone
make "DESTDIR=" clean

current directory: /Users/username/.asdf/installs/ruby/2.7.4/lib/ruby/gems/2.7.0/gems/mini_phone-1.1.7/ext/mini_phone
make "DESTDIR="
compiling mini_phone.cc
In file included from mini_phone.cc:2:
/opt/homebrew/opt/libphonenumber/include/phonenumbers/phonemetadata.pb.h:10:10: fatal error: 'google/protobuf/port_def.inc' file not found
#include <google/protobuf/port_def.inc>
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [mini_phone.o] Error 1

make failed, exit code 2

valid_for_country? returns true for valid phones with invalid country codes.

Hello! When using a valid UK phone number: +447975777666'

MiniPhone.valid_for_country?('+447975777666', 'US')

or

MiniPhone.parse('+447975777666', 'US').valid?

both return true.

This is because we are using IsValidNumber instead of IsValidNumberForRegion here:
https://github.com/ianks/mini_phone/blob/master/ext/mini_phone/mini_phone.cc#L55

This behavior feels somewhat unexpected as the country code argument is essentially overridden when either method is called with a phone number with a country code prefix.

I think we might want to change the behaviour to use the country_code if it is passed in. Happy to help push up a pr if that makes sense.

heroku installation/usage

I tried to use this gem on an application hosted on heroku but as this gem depend one libphonenumber, I was not able to build the application on heroku. Do you know how I can be able to do it?

Ruby >3.1.2 issue

Not sure if there are any changes in Ruby 3.1.3 but I am unable to install mini_phone in Ruby 3.1.3 and higher.

gem install mini_phone
Building native extensions. This could take a while...
ERROR:  Error installing mini_phone:
  ERROR: Failed to build gem native extension.

    current directory: /Users/espen/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/mini_phone-1.1.8/ext/mini_phone
/Users/espen/.asdf/installs/ruby/3.1.3/bin/ruby -I /Users/espen/.asdf/installs/ruby/3.1.3/lib/ruby/site_ruby/3.1.0 extconf.rb
checking for -lphonenumber... yes
creating Makefile

current directory: /Users/espen/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/mini_phone-1.1.8/ext/mini_phone
make DESTDIR\= sitearchdir\=./.gem.20230105-58078-6ghzqg sitelibdir\=./.gem.20230105-58078-6ghzqg clean

current directory: /Users/espen/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/mini_phone-1.1.8/ext/mini_phone
make DESTDIR\= sitearchdir\=./.gem.20230105-58078-6ghzqg sitelibdir\=./.gem.20230105-58078-6ghzqg
compiling mini_phone.cc
linking shared-object mini_phone/mini_phone.bundle
Undefined symbols for architecture arm64:
  "google::protobuf::internal::LogMessage::LogMessage(google::protobuf::LogLevel, char const*, int)", referenced from:
      void google::protobuf::internal::RepeatedPtrFieldBase::MergeFrom<google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler>(google::protobuf::internal::RepeatedPtrFieldBase const&) in mini_phone.o
  "google::protobuf::internal::LogMessage::~LogMessage()", referenced from:
      void google::protobuf::internal::RepeatedPtrFieldBase::MergeFrom<google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler>(google::protobuf::internal::RepeatedPtrFieldBase const&) in mini_phone.o
  "google::protobuf::internal::LogMessage::operator<<(char const*)", referenced from:
      void google::protobuf::internal::RepeatedPtrFieldBase::MergeFrom<google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler>(google::protobuf::internal::RepeatedPtrFieldBase const&) in mini_phone.o
  "google::protobuf::internal::GenericSwap(google::protobuf::MessageLite*, google::protobuf::MessageLite*)", referenced from:
      i18n::phonenumbers::PhoneNumber::Swap(i18n::phonenumbers::PhoneNumber*) in mini_phone.o
  "google::protobuf::internal::LogFinisher::operator=(google::protobuf::internal::LogMessage&)", referenced from:
      void google::protobuf::internal::RepeatedPtrFieldBase::MergeFrom<google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler>(google::protobuf::internal::RepeatedPtrFieldBase const&) in mini_phone.o
  "google::protobuf::internal::ArenaStringPtr::Set(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, google::protobuf::Arena*)", referenced from:
      google::protobuf::internal::ArenaStringPtr::Set(char const*, google::protobuf::Arena*) in mini_phone.o
  "google::protobuf::internal::RepeatedPtrFieldBase::DestroyProtos()", referenced from:
      google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::~RepeatedPtrField() in mini_phone.o
  "google::protobuf::internal::RepeatedPtrFieldBase::InternalExtend(int)", referenced from:
      google::protobuf::internal::RepeatedPtrFieldBase::MergeFromInternal(google::protobuf::internal::RepeatedPtrFieldBase const&, void (google::protobuf::internal::RepeatedPtrFieldBase::*)(void**, void**, int, int)) in mini_phone.o
  "google::protobuf::internal::RepeatedPtrFieldBase::AddOutOfLineHelper(void*)", referenced from:
      google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler::Type* google::protobuf::internal::RepeatedPtrFieldBase::Add<google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler>(google::protobuf::RepeatedPtrField<i18n::phonenumbers::NumberFormat>::TypeHandler::Type const*) in mini_phone.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mini_phone.bundle] Error 1

make failed, exit code 2

Gem files will remain installed in /Users/espen/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/gems/mini_phone-1.1.8 for inspection.
Results logged to /Users/espen/.asdf/installs/ruby/3.1.3/lib/ruby/gems/3.1.0/extensions/arm64-darwin-22/3.1.0/mini_phone-1.1.8/gem_make.out

I get this with Ruby 3.1.3 and 3.2.0. It works on 3.1.2.

Invalid country being returned for Virgin Islands

Running into an issue with validating numbers from Virgin Islands

Problem:

  • country is returning US for Virgin Islands

Expectation:

  • country should return VI for Virgin Islands

Setup:

virgin_islands_phone_number = "+13407759575"
phone = MiniPhone.parse(virgin_islands_phone_number)


phone.valid? # => false
p.country # => "US"


MiniPhone.valid_for_country?(virgin_islands_phone_number, 'VI') # true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.