Module: PicoPhone::Rails

Defined in:
lib/pico_phone/rails.rb,
lib/pico_phone/rails/type.rb,
lib/pico_phone/rails/railtie.rb,
lib/pico_phone/rails/version.rb,
lib/pico_phone/rails/extraction.rb,
lib/pico_phone/rails/normalizer.rb,
lib/pico_phone/rails/form_helper.rb,
lib/pico_phone/rails/region_resolution.rb,
lib/pico_phone/rails/phone_search_index.rb,
lib/pico_phone/rails/extracted_phone_number.rb,
lib/pico_phone/rails/serializers/phone_number_serializer.rb,
lib/generators/pico_phone/rails/phone_number/phone_number_generator.rb,
lib/generators/pico_phone/rails/extracted_phone_numbers/extracted_phone_numbers_generator.rb

Defined Under Namespace

Modules: Extraction, FormBuilderExtension, FormHelper, Normalizer, PhoneSearchIndex, Serializers Classes: Error, ExtractedPhoneNumber, ExtractedPhoneNumbersGenerator, PersistenceNotEnabled, PhoneNumberGenerator, Railtie, RegionRequired, SearchColumnNotConfigured, Type

Constant Summary collapse

VERSION =
"0.4.0"

Class Method Summary collapse

Class Method Details

.extract_phone_numbers(text, region:) ⇒ Array<PicoPhone::PhoneNumberMatch>

Scans arbitrary text for phone numbers. Works on any string -- no ActiveRecord model required.

Parameters:

  • text (String, nil)
  • region (String)

    ISO 3166-1 alpha-2 default region for numbers found without a country code

Returns:

  • (Array<PicoPhone::PhoneNumberMatch>)


13
14
15
16
17
# File 'lib/pico_phone/rails/extraction.rb', line 13

def self.extract_phone_numbers(text, region:)
  return [] if text.nil? || text.empty?

  PicoPhone.find_numbers(text, region)
end

.phone_field_display_value(value, region) ⇒ String?

Computes what a phone field should display: the number's national format if it parses validly, the raw string otherwise -- so a user re-editing an invalid or partial number sees exactly what they typed, not a blank or garbled reformat. Shared by FormHelper and FormBuilderExtension.

Parameters:

  • value (String, PicoPhone::PhoneNumber, nil)
  • region (String, nil)

    ISO 3166-1 alpha-2 region for interpreting value when it's a raw string

Returns:

  • (String, nil)


14
15
16
17
18
19
20
21
22
23
# File 'lib/pico_phone/rails/form_helper.rb', line 14

def self.phone_field_display_value(value, region)
  return nil if value.nil?
  return (value.valid? ? value.national : value.to_s) if value.is_a?(PicoPhone::PhoneNumber)

  string = value.to_s
  return string if string.empty?

  phone_number = PicoPhone.parse(string, region)
  phone_number.valid? ? phone_number.national : string
end

.redact_phone_numbers(text, region:, replacement: "[PHONE]") ⇒ String

Replaces every phone number found in text with replacement, preserving everything else in the string untouched.

Parameters:

  • text (String, nil)
  • region (String)

    ISO 3166-1 alpha-2 default region for numbers found without a country code

  • replacement (String) (defaults to: "[PHONE]")

Returns:

  • (String)


26
27
28
29
30
31
32
33
# File 'lib/pico_phone/rails/extraction.rb', line 26

def self.redact_phone_numbers(text, region:, replacement: "[PHONE]")
  return text.to_s if text.nil? || text.empty?

  matches = extract_phone_numbers(text, region: region)
  return text.dup if matches.empty?

  splice_redactions(text, matches, replacement)
end

.resolve_region(region, record) ⇒ String?

Resolves a region: option that may be a plain region code, or -- for apps where the correct region varies per record (multi-tenant, multi-region) -- a Symbol naming an instance method, or a Proc called with the record. Shared by Extraction and PhoneSearchIndex.

Parameters:

  • region (String, Symbol, Proc)
  • record (Object)

Returns:

  • (String, nil)


13
14
15
16
17
18
19
# File 'lib/pico_phone/rails/region_resolution.rb', line 13

def self.resolve_region(region, record)
  case region
  when Symbol then record.public_send(region)
  when Proc then region.call(record)
  else region
  end
end