Class: Phoner::Country
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.
-
#char_3_code ⇒ Object
Returns the value of attribute char_3_code.
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#full_number_length ⇒ Object
Returns the value of attribute full_number_length.
-
#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.
-
#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
- #matches_local_number?(string, default_area_code) ⇒ Boolean
- #number_parts(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/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/country.rb', line 2 def char_2_code @char_2_code end |
#char_3_code ⇒ Object
Returns the value of attribute char_3_code
2 3 4 |
# File 'lib/country.rb', line 2 def char_3_code @char_3_code end |
#country_code ⇒ Object
Returns the value of attribute country_code
2 3 4 |
# File 'lib/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/country.rb', line 2 def full_number_length @full_number_length end |
#local_number_format ⇒ Object
Returns the value of attribute local_number_format
2 3 4 |
# File 'lib/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/country.rb', line 2 def mobile_format @mobile_format end |
#name ⇒ Object
Returns the value of attribute name
2 3 4 |
# File 'lib/country.rb', line 2 def name @name end |
#number_format ⇒ Object
Returns the value of attribute number_format
2 3 4 |
# File 'lib/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
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/country.rb', line 40 def self.detect(string, default_country_code, default_area_code) Country.find_all_by_phone_code(default_country_code).each do |country| return country if country.matches_local_number?(string, default_area_code) end # find if the number has a country code Country.all.each do |country| return country if country.matches_full_number?(string) end return nil end |
.find_all_by_phone_code(code) ⇒ Object
22 23 24 25 |
# File 'lib/country.rb', line 22 def self.find_all_by_phone_code(code) return [] if code.nil? @@all.select {|c| c.country_code == code } end |
.find_by_country_code(code) ⇒ Object
27 28 29 30 31 |
# File 'lib/country.rb', line 27 def self.find_by_country_code(code) return nil if code.nil? @@all.each {|c| return c if c.char_3_code.downcase == code.downcase } nil end |
.find_by_name(name) ⇒ Object
33 34 35 36 37 |
# File 'lib/country.rb', line 33 def self.find_by_name(name) return nil if name.nil? @@all.each {|c| return c if c.name.downcase == name.downcase } nil end |
.load ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/country.rb', line 5 def self.load return @@all if @@all.present? data_file = File.join(File.dirname(__FILE__), '..', 'data', 'phone_countries.yml') @@all = [] YAML.load(File.read(data_file)).each_pair do |key, c| next unless c[:area_code] && c[:local_number_format] @@all << Country.new(c[:name], c[:country_code], c[:char_2_code], c[:char_3_code], c[:area_code], c[:local_number_format], c[:mobile_format], c[:full_number_length], c[:number_format]) end @@all end |
Instance Method Details
#is_mobile?(number) ⇒ Boolean
52 53 54 55 |
# File 'lib/country.rb', line 52 def is_mobile?(number) return true if mobile_format.nil? number =~ mobile_number_regex ? true : false end |
#matches_full_number?(string) ⇒ Boolean
63 64 65 |
# File 'lib/country.rb', line 63 def matches_full_number?(string) string =~ full_number_regexp && string =~ number_format_regex end |
#matches_local_number?(string, default_area_code) ⇒ Boolean
57 58 59 60 61 |
# File 'lib/country.rb', line 57 def matches_local_number?(string, default_area_code) ((string =~ full_number_regexp || string =~ area_code_number_regexp) && string =~ number_format_regex) || ((string =~ number_regex) && (default_area_code =~ area_code_regex)) end |
#number_parts(number, default_area_code) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/country.rb', line 67 def number_parts(number, default_area_code) number_part = if default_area_code number.match(number_regex) $1 else nil end if number_part.nil? matches = number.match(area_code_number_regexp) area_part = $1 number_part = matches.to_a.last end if number_part.nil? matches = number.match(full_number_regexp) country_part, area_part = $1, $2 number_part = matches.to_a.last end area_part ||= default_area_code raise "Could not determine area code" if area_part.nil? raise "Could not determine number" if number_part.nil? {:number => number_part, :area_code => area_part, :country_code => country_code, :country => self} end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/country.rb', line 18 def to_s name end |