A fast phone number lib for Ruby (binds to Google's C++ libphonenumber)

Overview

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

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.

Comments
  • Can't install on apple silicon

    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
    
    opened by jmschneider 4
  • heroku installation/usage

    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?

    opened by gagalago 3
  • valid_for_country? returns true for valid phones with invalid country codes.

    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.

    bug 
    opened by desheikh 3
  • #valid not returning same response as valid_for_country?

    #valid not returning same response as valid_for_country?

    Hello! For a Canadian Alberta number: 8255649738

    MiniPhone.valid_for_country?('8255649738', 'CA') => true
    
    MiniPhone.parse('8255649738', 'CA').valid? => false
    MiniPhone.parse('8255649738', 'CA').region_code => 'US'
    

    Based on https://libphonenumber.appspot.com/phonenumberparser?number=8255649738&country=CA the response should be true and CA.

    opened by desheikh 2
  • Unable to locate package libphonenumber on Ubuntu

    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
    
    documentation 
    opened by mensfeld 1
  • Invalid country being returned for Virgin Islands

    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
    
    opened by tsaifi9 0
Owner
Ian Ker-Seymer
Ian Ker-Seymer
This is another way to bypass the restriction of calling dl functions for system lib.

简介 在Android7.0以及以上的版本中,dlfcn.h头文件中dlopen, dlsym函数已经无法在系统库上使用。 比较常见的规避方法是,先通过maps文件找到so文件对应起始地址,然后通过解析elf 文件,得到函数的偏移量,起始地址加上偏移量就算出函数的真实地址。 具体实现方式是:Noug

Wind 58 Nov 11, 2022
a cpp lib for csv reading and writing

CSV Reader and Writer Author : csl E-Mail : [email protected] OverView Comma separated values (CSV, sometimes called character separated values, becau

null 0 Apr 3, 2022
Standards compliant, fast, secure markdown processing library in C

Hoedown Hoedown is a revived fork of Sundown, the Markdown parser based on the original code of the Upskirt library by Natacha Porté. Features Fully s

Hoedown 920 Nov 26, 2022
fast javascript bundler :package:

Fast JavaScript Bundler https://fjbundler.com What? It is what it says it is. However, this bundler aims to be a monolithic does-it-all type of bundle

Sebastian Karlsson 103 Oct 27, 2022
Fast comparison-based sort algorithm

nanosort Algorithm nanosort aims to be a fast comparison-based sorting algorithm, tuned for POD types of reasonably small sizes. nanosort implements a

Arseny Kapoulkine 37 Aug 2, 2022
A fast image processing library with low memory needs.

libvips : an image processing library Introduction libvips is a demand-driven, horizontally threaded image processing library. Compared to similar lib

libvips 7.4k Nov 28, 2022
Tau is a fast syntax highlighter capable of emitting HTML.

tau - a reasonably fast (wip) syntax highlighter. Tau is a fast syntax highlighter capable of emitting HTML. It highlights the following languages: py

Palaiologos 12 Nov 16, 2022
Very fast Markdown parser and HTML generator implemented in WebAssembly, based on md4c

Very fast Markdown parser and HTML generator implemented in WebAssembly, based on md4c

Rasmus 1.2k Nov 25, 2022
The goal of insidesp is to do fast point in polygon classification, the sp way.

insidesp The goal of insidesp is to do fast point in polygon classification, the sp way. We are comparing a few ways of implementing this, essentially

diminutive 2 Nov 12, 2021
Fast regular expression grep for source code with incremental index updates

Fast regular expression grep for source code with incremental index updates

Arseny Kapoulkine 260 Nov 22, 2022
Fast Binary Encoding is ultra fast and universal serialization solution for C++, C#, Go, Java, JavaScript, Kotlin, Python, Ruby, Swift

Fast Binary Encoding (FBE) Fast Binary Encoding allows to describe any domain models, business objects, complex data structures, client/server request

Ivan Shynkarenka 646 Dec 1, 2022
Using Visual Studio C++ to read IP addresses and comport number (Serial number) on Windows platform

Using Visual Studio C++ to read IP addresses on Windows platform

zhuhuijin 0 Feb 2, 2022
Kaprekar constant, number 6174, number 495.

Kaprekar Constant Demos Kaprekar constant, number 6174, number 495. This repository contains 1 Visual Studio solution, which managing 2 Visual Studio

MatrixLife 1 Dec 25, 2021
this lib with 26 template container and 10 kinds of algorithm, it is a good lib for study and usage

simple stl this lib simplify the achievement detail of common container, but add the container variety, the whole code partily follow Google Style. Em

wujiazheng 7 Mar 10, 2022
A tool to unlock apps sideloading on Windows Phone having unlocked bootloader.

Xapload Unlocker A tool for unlocking app sideloading in Windows Phone having unlocked bootloader. Description Using this tool you can revive your Wi

Fadil Fadz 6 Nov 28, 2022
Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers.

What is it? Google's common Java, C++ and JavaScript library for parsing, formatting, and validating international phone numbers. The Java version is

Google 14.4k Nov 25, 2022
Android example to get the rgb and disparity images from the OAK-D device connected to a phone.

depthai-android-jni-example (WIP) Android example to get the rgb and disparity images from the OAK-D device connected to a phone. DepthaiAndroidFast.m

Ibai Gorordo 30 Nov 27, 2022
A fast character conversion and transliteration library based on the scheme defined for Japan National Tax Agency (国税庁) 's corporate number (法人番号) system.

jntajis-python Documentation: https://jntajis-python.readthedocs.io/ What's JNTAJIS-python? JNTAJIS-python is a transliteration library, specifically

Open Collector, Inc. 15 Nov 12, 2022
ZSV/lib: a fast CSV parsing library and standalone utility

Please note: this code is still alpha / pre-production. Everything here should be considered preliminary. If you like ZSVlib, please give it a star! Z

null 84 Nov 28, 2022
ANSI C library for NURBS, B-Splines, and Bézier curves with interfaces for C++, C#, D, Go, Java, Lua, Octave, PHP, Python, R, and Ruby.

TinySpline TinySpline is a small, yet powerful library for interpolating, transforming, and querying arbitrary NURBS, B-Splines, and Bézier curves. Th

Marcel Steinbeck 876 Nov 26, 2022