Class: Phonie::Country
Constant Summary collapse
- COUNTRIES =
self.load
- COUNTRIES_BY_PHONE_CODE =
COUNTRIES.inject(Hash.new){|h, c| (h[c.country_code] ||= []) << c; h }
- COUNTRIES_BY_COUNTRY_CODE =
- COUNTRIES_BY_NAME =
Instance Attribute Summary collapse
-
#area_code ⇒ Object
Returns the value of attribute area_code.
-
#char_2_code ⇒ Object
Returns the value of attribute char_2_code.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#full_number_length ⇒ Object
Returns the value of attribute full_number_length.
-
#iso_3166_code ⇒ Object
Returns the value of attribute iso_3166_code.
-
#local_number_format ⇒ Object
Returns the value of attribute local_number_format.
-
#mobile_format ⇒ Object
Returns the value of attribute mobile_format.
-
#name ⇒ Object
Returns the value of attribute name.
-
#national_dialing_prefix ⇒ Object
Returns the value of attribute national_dialing_prefix.
-
#number_format ⇒ Object
Returns the value of attribute number_format.
Class Method Summary collapse
-
.detect(string, default_country_code, default_area_code) ⇒ Object
detect country from the string entered.
- .find_all_by_phone_code(code) ⇒ Object
- .find_by_country_code(code) ⇒ Object
- .find_by_name(name) ⇒ Object
- .load ⇒ Object
Instance Method Summary collapse
- #is_mobile?(number) ⇒ Boolean
-
#matches_full_number?(string) ⇒ Boolean
true if string contains country_code + area_code + local_number.
-
#matches_local_number?(string, default_area_code) ⇒ Boolean
true if string contains only the local_number, but the default_area_code is valid.
-
#matches_local_number_with_area_code?(string) ⇒ Boolean
true if string contains area_code + local_number.
- #parse(number, default_area_code) ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#area_code ⇒ Object
Returns the value of attribute area_code
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def area_code @area_code end |
#char_2_code ⇒ Object
Returns the value of attribute char_2_code
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def char_2_code @char_2_code end |
#country_code ⇒ Object
Returns the value of attribute country_code
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def country_code @country_code end |
#full_number_length ⇒ Object
Returns the value of attribute full_number_length
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def full_number_length @full_number_length end |
#iso_3166_code ⇒ Object
Returns the value of attribute iso_3166_code
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def iso_3166_code @iso_3166_code end |
#local_number_format ⇒ Object
Returns the value of attribute local_number_format
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def local_number_format @local_number_format end |
#mobile_format ⇒ Object
Returns the value of attribute mobile_format
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def mobile_format @mobile_format end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def name @name end |
#national_dialing_prefix ⇒ Object
Returns the value of attribute national_dialing_prefix
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def national_dialing_prefix @national_dialing_prefix end |
#number_format ⇒ Object
Returns the value of attribute number_format
2 3 4 |
# File 'lib/phonie/country.rb', line 2 def number_format @number_format end |
Class Method Details
.detect(string, default_country_code, default_area_code) ⇒ Object
detect country from the string entered
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/phonie/country.rb', line 32 def self.detect(string, default_country_code, default_area_code) # use the default_country_code to try for a quick match country = find_all_by_phone_code(default_country_code).find do |country| country.matches_full_number?(string) || country.matches_local_number_with_area_code?(string) || country.matches_local_number?(string, default_area_code) end # then search all for a full match country || COUNTRIES.find {|country| country.matches_full_number?(string) } end |
.find_all_by_phone_code(code) ⇒ Object
19 20 21 |
# File 'lib/phonie/country.rb', line 19 def self.find_all_by_phone_code(code) COUNTRIES_BY_PHONE_CODE[code] || [] end |
.find_by_country_code(code) ⇒ Object
23 24 25 |
# File 'lib/phonie/country.rb', line 23 def self.find_by_country_code(code) COUNTRIES_BY_COUNTRY_CODE[code.downcase] if code end |
.find_by_name(name) ⇒ Object
27 28 29 |
# File 'lib/phonie/country.rb', line 27 def self.find_by_name(name) COUNTRIES_BY_NAME[name.downcase] if name end |
.load ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/phonie/country.rb', line 3 def self.load data_file = File.join(File.dirname(__FILE__), 'data', 'phone_countries.yml') all = [] YAML.load(File.read(data_file)).each do |c| next unless c[:area_code] && c[:local_number_format] all << Country.new(c[:name], c[:country_code], c[:char_2_code], c[:iso_3166_code], c[:area_code], c[:local_number_format], c[:mobile_format], c[:full_number_length], c[:number_format], c[:national_dialing_prefix]) end all end |
Instance Method Details
#is_mobile?(number) ⇒ Boolean
48 49 50 51 |
# File 'lib/phonie/country.rb', line 48 def is_mobile?(number) return true if mobile_format.nil? number =~ mobile_number_regex ? true : false end |
#matches_full_number?(string) ⇒ Boolean
true if string contains country_code + area_code + local_number
54 55 56 |
# File 'lib/phonie/country.rb', line 54 def matches_full_number?(string) string =~ full_number_regex && string =~ number_format_regex end |
#matches_local_number?(string, default_area_code) ⇒ Boolean
true if string contains only the local_number, but the default_area_code is valid
64 65 66 |
# File 'lib/phonie/country.rb', line 64 def matches_local_number?(string, default_area_code) string =~ number_regex && default_area_code =~ area_code_regex end |
#matches_local_number_with_area_code?(string) ⇒ Boolean
true if string contains area_code + local_number
59 60 61 |
# File 'lib/phonie/country.rb', line 59 def matches_local_number_with_area_code?(string) string =~ area_code_number_regex && string =~ number_format_regex end |
#parse(number, default_area_code) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/phonie/country.rb', line 68 def parse(number, default_area_code) if md = number.match(full_number_regex) {:area_code => md[2], :number => md[-1]} elsif md = number.match(area_code_number_regex) {:area_code => md[1], :number => md[-1]} elsif md = number.match(number_regex) {:area_code => default_area_code, :number => md[1]} else {} end end |
#to_s ⇒ Object
44 45 46 |
# File 'lib/phonie/country.rb', line 44 def to_s name end |