Class: SpainPhone::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/spain_phone.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(phone_number) ⇒ Phone



26
27
28
# File 'lib/spain_phone.rb', line 26

def initialize phone_number
  @phone_number = phone_number.to_s.gsub(/\D/, '')
end

Instance Attribute Details

#phone_numberObject

Returns the value of attribute phone_number.



24
25
26
# File 'lib/spain_phone.rb', line 24

def phone_number
  @phone_number
end

Instance Method Details

#area_codeObject



35
36
37
# File 'lib/spain_phone.rb', line 35

def area_code
  return @phone_number.to_s[0..2] if valid?
end

#country_codeObject



39
40
41
# File 'lib/spain_phone.rb', line 39

def country_code
  "+34"
end

#internationalObject



43
44
45
# File 'lib/spain_phone.rb', line 43

def international
  [country_code, @phone_number].join
end

#phone_typeObject



47
48
49
50
51
52
53
54
55
56
# 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

#provinceObject



58
59
60
61
62
63
64
65
# 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?Boolean



30
31
32
33
# 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