TelephoneNumber는 Google의 libphonenumber 라이브러리를 기반으로 하는 글로벌 전화번호 확인 gem입니다.
우리의 데모를 자유롭게 확인해 보세요!
넘버잭
애플리케이션의 Gemfile에 다음 줄을 추가하세요.
gem 'telephone_number'
그런 다음 다음을 실행합니다.
$ bundle
또는 다음과 같이 직접 설치하십시오.
$ gem install telephone_number
이 라이브러리에는 Ruby 2.4 이상이 필요합니다.
validates :my_attribute_name, telephone_number: {country: proc{|record| record.country}, types: [:fixed_line, :mobile, etc]}
:area_code_optional
:fixed_line
:mobile
:no_international_dialling
:pager
:personal_number
:premium_rate
:shared_cost
:toll_free
:uan
:voicemail
:voip
record.country
:us
, :ca
또는 'DE'
와 같은 유효한 두 글자 국가 코드를 생성해야 합니다.Proc
대신 String
또는 Symbol
전달할 수도 있습니다. 다음을 호출하여 TelephoneNumber
객체를 얻을 수 있습니다.
phone_object = TelephoneNumber.parse("3175082237", :us) ==>
#<TelephoneNumber::Number:0x007fe3bc146cf0
@country=:US,
@e164_number="13175083348",
@national_number="3175083348",
@original_number="3175083348">
그 후에는 다음과 같은 인스턴스 메서드를 사용할 수 있습니다.
valid_types
숫자가 유효한 것으로 간주되는 모든 유형을 반환합니다.
phone_object.valid_types ==> [:fixed_line, :mobile, :toll_free]
valid?
valid_types
가 비어 있는지 여부를 나타내는 부울 값을 반환합니다.
phone_object.valid? ==> true
national_number(formatted: true)
괄호, 대시 등의 특수 문자를 포함하여 국가별 형식의 숫자를 반환합니다. formatted: false
전달하여 특수 문자를 생략할 수 있습니다.
phone_object.national_number ==> "(317) 508-2237"
international_number(formatted: true)
괄호, 대시 등의 특수 문자를 포함하여 국제 형식의 숫자를 반환합니다. formatted: false
전달하여 특수 문자를 생략할 수 있습니다.
phone_object.international_number ==> "+1 317-508-2237"
e164_number(formatted: true)
괄호, 대시 등의 특수 문자를 포함하여 국제 형식의 숫자를 반환합니다. formatted: false
전달하여 특수 문자를 생략할 수 있습니다.
phone_object.e164_number ==> "+13175082237"
country
숫자의 국가와 관련된 데이터가 포함된 객체를 반환합니다.
phone_object.country ===>
#<TelephoneNumber::Country:0x007fb976267410
@country_code="1",
@country_id="US",
...
location
숫자의 위치를 반환합니다. 기본 로케일은 :en
phone_object.location ==> "Indiana"
phone_object.location(:ja) ==> "ソウル特別市"
timezone
숫자의 시간대를 반환합니다.
phone_object.timezone ==> "America/New_York"
또한 다음과 같은 클래스 메서드를 사용할 수 있습니다.
parse
TelephoneNumber 객체를 반환합니다.
TelephoneNumber.parse("3175082237", :US)
E164 형식의 번호를 전달하면 즉시 국가를 결정합니다.
TelephoneNumber.parse("+13175082237")
valid?
특정 숫자가 유효한지 여부를 나타내는 부울 값을 반환합니다.
TelephoneNumber.valid?("3175082237", :US) ==> true
특정 키 세트에 대해 유효성을 검사하려는 경우 키 배열을 전달할 수 있습니다.
TelephoneNumber.valid?("3175082237", :US, [:mobile, :fixed_line]) ==> true
TelephoneNumber.valid?("3175082237", :US, [:toll_free]) ==> false
invalid?
특정 숫자가 유효하지 않은지 여부를 나타내는 부울 값을 반환합니다.
TelephoneNumber.invalid?("3175082237", :US) ==> false
특정 키 세트에 대해 무효화하려는 경우 키 배열을 전달할 수 있습니다.
TelephoneNumber.invalid?("3175082237", :US, [:mobile, :fixed_line]) ==> false
TelephoneNumber.invalid?("3175082237", :US, [:toll_free]) ==> true
Google이 제공하는 데이터를 재정의해야 하는 경우 재정의 파일을 설정하여 이를 수행할 수 있습니다. 이 파일은 Google과 동일한 형식일 뿐만 아니라 Marshal을 사용하여 직렬화될 것으로 예상됩니다.
직렬화된 대체 파일을 생성하려면 다음을 수행하십시오.
ruby bin/console
TelephoneNumber.generate_override_file("/path/to/file")
이 경우 /path/to/file
Google 데이터와 동일한 구조의 맞춤 데이터가 있는 xml 파일을 나타냅니다.
다음을 사용하여 재정의 파일을 설정할 수 있습니다.
TelephoneNumber.override_file = "/path/to_file.dat"
TelephoneNumber에 유효하지 않은 번호가 전달된 후 해당 번호의 형식을 지정하라는 요청을 받으면 원래 전달된 번호의 형식이 지정되지 않은 문자열만 반환됩니다. 이는 잘못된 숫자에 대한 서식 규칙을 찾을 수 없기 때문입니다. 이것이 허용되지 않는 경우에는 TelephoneNumber가 유효하지 않은 번호의 형식을 지정하는 데 사용할 default_format_pattern
및 default_format_string
설정할 수 있습니다.
TelephoneNumber.default_format_pattern = "(\d{3})(\d{3})(\d*)"
TelephoneNumber.default_format_string = "($1) $2-$3"
invalid_number = "1111111111"
phone_object = TelephoneNumber.parse(invalid_number, :US)
phone_object.national_number ==> "(111) 111-1111"
저장소를 확인한 후 bin/setup
실행하여 종속 항목을 설치하세요. 그런 다음 rake test
실행하여 테스트를 실행합니다. 실험할 수 있는 대화형 프롬프트를 보려면 bin/console
실행할 수도 있습니다.
새로운 기능을 개발하는 동안 특정 전화번호를 대상으로 테스트할 수 있습니다. 이를 수행하려면 lib/telephone_number/test_data_generator.rb
에 번호를 추가한 다음 rake data:test:import
실행하십시오. 이 명령은 Google에서 제공하는 데모 애플리케이션에 연결하여 테스트할 올바른 형식을 가져옵니다.
이 gem을 로컬 머신에 설치하려면, bundle exec rake install
실행하세요.
버그 보고서 및 끌어오기 요청은 GitHub(https://github.com/mobi/telephone_number)에서 환영합니다. 이 프로젝트는 협업을 위한 안전하고 환영받는 공간이 되도록 의도되었으며 기여자는 기여자 규약 행동 강령을 준수해야 합니다.
이 gem은 MIT 라이선스 조건에 따라 오픈 소스로 제공됩니다.