class SpainPhone::Phone

Attributes

phone_number[RW]

Public Class Methods

new(phone_number) click to toggle source
# File lib/spain_phone.rb, line 26
def initialize phone_number
  @phone_number = phone_number.to_s.gsub(/\D/, '')
end

Public Instance Methods

area_code() click to toggle source
# File lib/spain_phone.rb, line 35
def area_code
  return @phone_number.to_s[0..2] if valid?
end
country_code() click to toggle source
# File lib/spain_phone.rb, line 39
def country_code
  "+34"
end
international() click to toggle source
# File lib/spain_phone.rb, line 43
def international
  [country_code, @phone_number].join
end
phone_type() click to toggle source
# File lib/spain_phone.rb, line 47
def phone_type
  return nil unless valid?
  if area_code.match(/^80[1-9]|^90[1-9]/)
    "premium"
  elsif area_code.match(/^800|^900/)
    "toll-free"
  else
    @phone_number.match(/^[67]\d{8}$/).nil? ? "landline" : "mobile"
  end
end
province() click to toggle source
# File lib/spain_phone.rb, line 58
def province
  return nil unless phone_type == "landline"
  AREA_CODES.each do |key, value|
    value.each do |area|
      return key if area.to_s == area_code
    end
  end
end
valid?() click to toggle source
# File lib/spain_phone.rb, line 30
def valid?
  return false if @phone_number.length != 9
  return !@phone_number.match(/^[9678][0-9]{8}$/).nil?
end