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/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, Normalizer, PhoneSearchIndex, Serializers Classes: Error, ExtractedPhoneNumber, ExtractedPhoneNumbersGenerator, PersistenceNotEnabled, PhoneNumberGenerator, Railtie, RegionRequired, SearchColumnNotConfigured, Type

Constant Summary collapse

VERSION =
"0.3.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

.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