-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
181 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,9 @@ | ||
module MsisdnHelpers | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
delegate :normalize_number, to: :class | ||
|
||
validates :msisdn, | ||
presence: true, | ||
phony_plausible: true | ||
|
||
before_validation :normalize_msisdn | ||
end | ||
|
||
class_methods do | ||
def where_msisdn(value) | ||
where(msisdn: normalize_number(value)) | ||
end | ||
NUMBER_FORMAT = /\A\d+\z/ | ||
|
||
def normalize_number(value) | ||
PhonyRails.normalize_number(value) | ||
end | ||
end | ||
|
||
private | ||
|
||
def normalize_msisdn | ||
self.msisdn = normalize_number(msisdn) | ||
included do | ||
validates :msisdn, presence: true, format: { with: NUMBER_FORMAT } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class PhoneNumberParser | ||
attr_reader :parser | ||
|
||
def initialize(parser: Phony) | ||
@parser = parser | ||
end | ||
|
||
def valid?(value) | ||
return false if value.starts_with?("0") | ||
|
||
parser.plausible?(value) | ||
end | ||
|
||
def split(value) | ||
raise ArgumentError, "Not E-164 number" unless valid?(value) | ||
|
||
parser.split(value) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
class PhoneNumberType < ActiveRecord::Type::String | ||
AREA_CODE_COUNTRY_PREFIXES = [ "1" ].freeze | ||
|
||
PhoneNumber = Struct.new(:value, :country_code, :area_code, :e164, keyword_init: true) do | ||
def to_s | ||
value | ||
end | ||
|
||
def e164? | ||
e164 | ||
end | ||
|
||
def sip? | ||
sip | ||
end | ||
|
||
def ==(other) | ||
if other.is_a?(self.class) | ||
value == other.value | ||
else | ||
value == other | ||
end | ||
end | ||
|
||
def possible_countries | ||
@possible_countries ||= e164? ? ISO3166::Country.find_all_country_by_country_code(country_code) : ISO3166::Country.all | ||
end | ||
|
||
def country | ||
possible_countries.first if possible_countries.one? | ||
end | ||
end | ||
|
||
attr_reader :parser | ||
|
||
def initialize(**options) | ||
super | ||
|
||
@parser = options.fetch(:parser) { PhoneNumberParser.new } | ||
end | ||
|
||
def cast(value) | ||
return if value.blank? | ||
return value if value.is_a?(PhoneNumber) | ||
|
||
value = value.gsub(/\D/, "") | ||
return if value.blank? | ||
|
||
return PhoneNumber.new(value:, e164: false) unless parser.valid?(value) | ||
|
||
country_code, area_code, = parser.split(value) | ||
PhoneNumber.new( | ||
value:, | ||
e164: true, | ||
country_code: country_code, | ||
area_code: (area_code if country_code.in?(AREA_CODE_COUNTRY_PREFIXES)) | ||
) | ||
end | ||
|
||
def serialize(value) | ||
cast(value)&.value | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Rails.application.config.to_prepare do | ||
ActiveRecord::Type.register(:phone_number, PhoneNumberType) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.