Class: Phonie::Country
- Inherits:
-
Object
- Object
- Phonie::Country
- Extended by:
- Forwardable
- Defined in:
- lib/phonie/country.rb
Instance Attribute Summary collapse
-
#char_2_code ⇒ Object
readonly
Returns the value of attribute char_2_code.
-
#country_code ⇒ Object
readonly
Returns the value of attribute country_code.
-
#iso_3166_code ⇒ Object
readonly
Returns the value of attribute iso_3166_code.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
Class Method Summary collapse
- .all ⇒ Object
- .all_by_country_code ⇒ Object
- .all_by_name ⇒ Object
- .all_by_phone_code ⇒ Object
-
.detect(phone_number, default_country_code, default_area_code) ⇒ Object
detect country from the passed phone number.
- .find_all_by_phone_code(code) ⇒ Object
- .find_by_country_code(code) ⇒ Object
- .find_by_name(name) ⇒ Object
Instance Method Summary collapse
-
#initialize(params) ⇒ Country
constructor
A new instance of Country.
- #national_dialing_prefix ⇒ Object
- #to_s ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(params) ⇒ Country
Returns a new instance of Country.
10 11 12 13 14 15 16 17 |
# File 'lib/phonie/country.rb', line 10 def initialize(params) @name = params[:name] @country_code = params[:country_code] @char_2_code = params[:char_2_code] @iso_3166_code = params[:iso_3166_code] @parser = Phonie::Parser.new(params) @national_dialing_prefix = params[:national_dialing_prefix] end |
Instance Attribute Details
#char_2_code ⇒ Object (readonly)
Returns the value of attribute char_2_code.
8 9 10 |
# File 'lib/phonie/country.rb', line 8 def char_2_code @char_2_code end |
#country_code ⇒ Object (readonly)
Returns the value of attribute country_code.
8 9 10 |
# File 'lib/phonie/country.rb', line 8 def country_code @country_code end |
#iso_3166_code ⇒ Object (readonly)
Returns the value of attribute iso_3166_code.
8 9 10 |
# File 'lib/phonie/country.rb', line 8 def iso_3166_code @iso_3166_code end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/phonie/country.rb', line 8 def name @name end |
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
8 9 10 |
# File 'lib/phonie/country.rb', line 8 def parser @parser end |
Class Method Details
.all ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/phonie/country.rb', line 22 def self.all @@all ||= begin YAML.load_file(Phonie.configuration.data_file_path).collect do |country_params| Country.new(country_params) end.select {|country| country.valid? } end end |
.all_by_country_code ⇒ Object
34 35 36 |
# File 'lib/phonie/country.rb', line 34 def self.all_by_country_code @@all_by_country_name ||= Hash[*all.map{|c| [c.iso_3166_code.downcase, c] }.flatten] end |
.all_by_name ⇒ Object
38 39 40 |
# File 'lib/phonie/country.rb', line 38 def self.all_by_name @@all_by_name ||= Hash[*all.map{|c| [c.name.downcase, c] }.flatten] end |
.all_by_phone_code ⇒ Object
30 31 32 |
# File 'lib/phonie/country.rb', line 30 def self.all_by_phone_code @@all_by_phone_code ||= all.inject(Hash.new){|h, c| (h[c.country_code] ||= []) << c; h } end |
.detect(phone_number, default_country_code, default_area_code) ⇒ Object
detect country from the passed phone number
55 56 57 58 59 60 61 62 63 |
# File 'lib/phonie/country.rb', line 55 def self.detect(phone_number, 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 |c| c.possible_valid_number?(phone_number, default_area_code) end # then search all for a full match country || all.find {|country| country.is_valid_number?(phone_number) } end |
.find_all_by_phone_code(code) ⇒ Object
42 43 44 |
# File 'lib/phonie/country.rb', line 42 def self.find_all_by_phone_code(code) all_by_phone_code[code] || [] end |
.find_by_country_code(code) ⇒ Object
46 47 48 |
# File 'lib/phonie/country.rb', line 46 def self.find_by_country_code(code) all_by_country_code[code.downcase] if code end |
.find_by_name(name) ⇒ Object
50 51 52 |
# File 'lib/phonie/country.rb', line 50 def self.find_by_name(name) all_by_name[name.downcase] if name end |
Instance Method Details
#national_dialing_prefix ⇒ Object
69 70 71 72 73 |
# File 'lib/phonie/country.rb', line 69 def national_dialing_prefix return nil if @national_dialing_prefix == "None" @national_dialing_prefix end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/phonie/country.rb', line 65 def to_s name end |
#valid? ⇒ Boolean
75 76 77 |
# File 'lib/phonie/country.rb', line 75 def valid? !!(name && parser.valid?) end |