class Dialable::NANP

Attributes

areacode[RW]
country[RW]
extension[RW]
line[RW]
location[RW]
pattern[R]
prefix[RW]
relative_timezones[RW]
service_codes[R]
timezones[RW]

Public Class Methods

new(parts = {}, options = {}) click to toggle source
# File lib/dialable.rb, line 26
def initialize(parts = {}, options = {})
  self.areacode  = parts.fetch(:areacode)
  self.prefix    = parts.fetch(:prefix)
  self.line      = parts.fetch(:line)
  self.extension = parts.fetch(:extension)
  @service_codes = options.fetch(:service_codes) { Dialable::ServiceCodes::NANP }
  @patterns      = options.fetch(:patterns)      { Dialable::Patterns::NANP }
end
parse(string, parser = Dialable::Parsers::NANP) click to toggle source
# File lib/dialable.rb, line 16
def self.parse(string, parser = Dialable::Parsers::NANP)
  parsed = parser.call(string)

  if parsed
    return Dialable::NANP.new(parsed)
  else
    fail InvalidNANPError, "Not a valid NANP Phone Number."
  end
end

Public Instance Methods

areacode=(raw_code) click to toggle source
# File lib/dialable.rb, line 35
def areacode=(raw_code)
  code = AreaCodes::NANP.fetch(raw_code.to_i) { fail InvalidNANPError, "#{code} is not a valid NANP Area Code." }
  @areacode = raw_code
  @location = code.fetch(:location) { nil }
  @country = code.fetch(:country) { nil }
  @timezones = code.fetch(:timezones) { [] }
  @relative_timezones = @timezones.map { |timezone| timezone_offset(timezone) }
end
erc?() click to toggle source
# File lib/dialable.rb, line 44
def erc?
  @service_codes.key?(@areacode)
end
raw_timezone() click to toggle source
# File lib/dialable.rb, line 85
def raw_timezone
  @raw_timezones.first if @raw_timezones
end
relative_timezone() click to toggle source
# File lib/dialable.rb, line 89
def relative_timezone
  @relative_timezones.first if @relative_timezones
end
timezone() click to toggle source
# File lib/dialable.rb, line 77
def timezone
  @timezones.first if @timezones
end
timezone=(tz) click to toggle source
# File lib/dialable.rb, line 81
def timezone=(tz)
  @timezones = [tz] if tz
end
to_dialable() click to toggle source
# File lib/dialable.rb, line 62
def to_dialable
  rtn = "#{@prefix}#{@line}"
  rtn = "#{@areacode}#{rtn}" if @areacode
  rtn
end
to_digits() click to toggle source
# File lib/dialable.rb, line 55
def to_digits
  rtn = "#{@prefix}#{@line}"
  rtn = "#{@areacode}#{rtn}" if @areacode
  rtn = "#{rtn} x#{@extension}" if @extension
  rtn
end
to_hash() click to toggle source
# File lib/dialable.rb, line 68
def to_hash
  {
    :areacode => @areacode,
    :prefix => @prefix,
    :line => @line,
    :extension => @extension
  }
end
to_s() click to toggle source
# File lib/dialable.rb, line 48
def to_s
  rtn = "#{@prefix}-#{@line}"
  rtn = "#{@areacode}-#{rtn}" if @areacode
  rtn = "#{rtn} x#{@extension}" if @extension
  rtn
end

Private Instance Methods

timezone_offset(raw) click to toggle source
# File lib/dialable.rb, line 95
def timezone_offset(raw)
  TZInfo::Timezone.get(raw).offsets_up_to(0).first.utc_total_offset / 3600
end