Class: Ronin::DB::PhoneNumber

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Model, Model::Importable
Defined in:
lib/ronin/db/phone_number.rb

Overview

Represents a phone number.

Since:

  • 0.2.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Model::Importable

included

Methods included from Model

included

Instance Attribute Details

#area_codeString?

The phone number area code (three digits after the country code).

Returns:

  • (String, nil)


71
# File 'lib/ronin/db/phone_number.rb', line 71

attribute :area_code, :string

#country_codeString?

The phone number country code (the first one or two digits).

Returns:

  • (String, nil)


60
# File 'lib/ronin/db/phone_number.rb', line 60

attribute :country_code, :string

#created_atTime

Tracks when the phone number was first created.

Returns:

  • (Time)


104
# File 'lib/ronin/db/phone_number.rb', line 104

attribute :created_at, :datetime

#idInteger

The primary key of the phone number.

Returns:

  • (Integer)


42
# File 'lib/ronin/db/phone_number.rb', line 42

attribute :id, :integer

#line_numberString

The phone number line number (last four digits).

Returns:

  • (String)


93
# File 'lib/ronin/db/phone_number.rb', line 93

attribute :line_number, :string

#notesArray<Note>

The associated notes.

Returns:



140
# File 'lib/ronin/db/phone_number.rb', line 140

has_many :notes, dependent: :destroy

#numberString

The phone number.

Returns:

  • (String)


48
# File 'lib/ronin/db/phone_number.rb', line 48

attribute :number, :string

#organization_departmentOrganizationDepartment?

The organization department that uses the email address.

Returns:



128
# File 'lib/ronin/db/phone_number.rb', line 128

has_one :organization_department, dependent: :nullify

#organization_membersOrganizationMember?

The organization member that use the phone number.

Returns:



134
# File 'lib/ronin/db/phone_number.rb', line 134

has_one :organization_member, dependent: :nullify

#organization_phone_numberOrganiationPhoneNumber?

The association of the organization that use the phone number.

Returns:

  • (OrganiationPhoneNumber, nil)


122
# File 'lib/ronin/db/phone_number.rb', line 122

has_one :organization_phone_number, dependent: :destroy

#peopleArray<Person>

The people that use this phone number.

Returns:



116
# File 'lib/ronin/db/phone_number.rb', line 116

has_many :people, through: :personal_phone_numbers

#personal_phone_numbersArray<PersonalPhoneNumber>

The association of people that use this phone number.

Returns:



110
# File 'lib/ronin/db/phone_number.rb', line 110

has_many :personal_phone_numbers, dependent: :destroy

#prefixString

The phone number prefix (three digits after the area code).

Returns:

  • (String)


82
# File 'lib/ronin/db/phone_number.rb', line 82

attribute :prefix, :string

Class Method Details

.for_organization(name) ⇒ Array<PhoneNumber>

Queries all phone numbers associated with the organization name.

Parameters:

  • name (String)

    The organization's name.

Returns:

  • (Array<PhoneNumber>)

    The phone numbers associated with the organization.

Since:

  • 0.2.0



224
225
226
227
228
229
230
# File 'lib/ronin/db/phone_number.rb', line 224

def self.for_organization(name)
  joins(organization_phone_number: :organization).where(
    organization_phone_number: {
      ronin_organizations: {name: name}
    }
  )
end

.for_person(full_name) ⇒ Array<PhoneNumber>

Queries all phone numbers associated with the person.

Parameters:

  • full_name (String)

    The person's full name.

Returns:

  • (Array<PhoneNumber>)

    The phone numbers associated with the person.

Since:

  • 0.2.0



209
210
211
# File 'lib/ronin/db/phone_number.rb', line 209

def self.for_person(full_name)
  joins(:people).where(people: {full_name: full_name})
end

.import(number) ⇒ PhoneNumber

Imports an phone number.

Parameters:

  • number (String)

    The phone number to import.

Returns:

Since:

  • 0.2.0



194
195
196
# File 'lib/ronin/db/phone_number.rb', line 194

def self.import(number)
  create(parse(number))
end

.lookup(number) ⇒ PhoneNumber?

Looks up the phone number.

Parameters:

  • number (String)

    The phone number to query.

Returns:

Since:

  • 0.2.0



153
154
155
# File 'lib/ronin/db/phone_number.rb', line 153

def self.lookup(number)
  find_by(number: number)
end

.parse(number) ⇒ Hash{Symbol => String,nil}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parses the phone number.

Parameters:

  • number (String)

    The raw phone number to parse.

Returns:

  • (Hash{Symbol => String,nil})

    The parsed attributes of the phone number.

Since:

  • 0.2.0



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/ronin/db/phone_number.rb', line 168

def self.parse(number)
  if (match = number.match(/\A(?:(?:\+?(?<country_code>\d{1,3})[\s.-])?(?:\((?<area_code>\d{3})\)|(?<area_code>\d{3}))[\s.-])?(?<prefix>\d{3})[\s.-](?<line_number>\d{4})\z/))
    {
      number: number,

      country_code: match[:country_code],
      area_code:    match[:area_code],
      prefix:       match[:prefix],
      line_number:  match[:line_number]
    }
  else
    raise(ArgumentError,"invalid phone number: #{number.inspect}")
  end
end

.similar_to(number) ⇒ Array<PhoneNumber>

Finds all similar phone numbers with the matching phone number components.

Parameters:

  • number (String)

    The phone number to parse and search for.

Returns:

Since:

  • 0.2.0



244
245
246
247
248
249
250
# File 'lib/ronin/db/phone_number.rb', line 244

def self.similar_to(number)
  attributes = parse(number)
  attributes.delete(:number)
  attributes.compact!

  where(**attributes)
end

.with_area_code(area_code) ⇒ Array<PhoneNumber>

Finds all phone numbers with the matching area code.

Parameters:

  • area_code (String)

    The area code to search for.

Returns:

  • (Array<PhoneNumber>)

    The phone numbers with the matching area code.

Since:

  • 0.2.0



278
279
280
# File 'lib/ronin/db/phone_number.rb', line 278

def self.with_area_code(area_code)
  where(area_code: area_code)
end

.with_country_code(country_code) ⇒ Array<PhoneNumber>

Finds all phone numbers with the matching country code.

Parameters:

  • country_code (String)

    The country code to search for.

Returns:

  • (Array<PhoneNumber>)

    The phone numbers with the matching country code.

Since:

  • 0.2.0



263
264
265
# File 'lib/ronin/db/phone_number.rb', line 263

def self.with_country_code(country_code)
  where(country_code: country_code)
end

.with_line_number(line_number) ⇒ Array<PhoneNumber>

Finds all phone numbers with the matching line number.

Parameters:

  • line_number (String)

    The line number to search for.

Returns:

  • (Array<PhoneNumber>)

    The phone numbers with the matching line number.

Since:

  • 0.2.0



308
309
310
# File 'lib/ronin/db/phone_number.rb', line 308

def self.with_line_number(line_number)
  where(line_number: line_number)
end

.with_prefix(prefix) ⇒ Array<PhoneNumber>

Finds all phone numbers with the matching prefix.

Parameters:

  • prefix (String)

    The prefix to search for.

Returns:

  • (Array<PhoneNumber>)

    The phone numbers with the matching prefix.

Since:

  • 0.2.0



293
294
295
# File 'lib/ronin/db/phone_number.rb', line 293

def self.with_prefix(prefix)
  where(prefix: prefix)
end

Instance Method Details

#to_sString

Converts the phone number to a String.

Returns:

  • (String)

Since:

  • 0.2.0



317
318
319
# File 'lib/ronin/db/phone_number.rb', line 317

def to_s
  number
end