Class: Country

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

Constant Summary collapse

Data =

Data = File.open( ‘lib/data/countries.yaml’ ) {|yaml| YAML::load( yaml )}

YAML.load_file(File.join(File.dirname(__FILE__), 'data', 'countries.yaml')) || {}
Names =
Data.map {|code,data| data[:name]}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Country

Returns a new instance of Country.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/countries.rb', line 9

def initialize(data)
  @number = data['number']
  @alpha2 = data['alpha2']
  @alpha3 = data['alpha3']
  @name = data['name']
  @names = data['names']
  @latitude = data['latitude']
  @longitude = data['longitude']
  @region = data['region']
  @subregion = data['subregion']
  
  @country_code = data['e164_country_code']
  @national_destination_code_lengths = data['e164_national_destination_code_lengths']
  @national_number_lengths = data['e164_national_number_lengths']
  @international_prefix = data['e164_international_prefix']
  @national_prefix = data['e164_national_prefix']
end

Instance Attribute Details

#alpha2Object (readonly)

Returns the value of attribute alpha2.



6
7
8
# File 'lib/countries.rb', line 6

def alpha2
  @alpha2
end

#alpha3Object (readonly)

Returns the value of attribute alpha3.



6
7
8
# File 'lib/countries.rb', line 6

def alpha3
  @alpha3
end

#country_codeObject (readonly)

Returns the value of attribute country_code.



6
7
8
# File 'lib/countries.rb', line 6

def country_code
  @country_code
end

#international_prefixObject (readonly)

Returns the value of attribute international_prefix.



6
7
8
# File 'lib/countries.rb', line 6

def international_prefix
  @international_prefix
end

#latitudeObject (readonly)

Returns the value of attribute latitude.



6
7
8
# File 'lib/countries.rb', line 6

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude.



6
7
8
# File 'lib/countries.rb', line 6

def longitude
  @longitude
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/countries.rb', line 6

def name
  @name
end

#namesObject (readonly)

Returns the value of attribute names.



6
7
8
# File 'lib/countries.rb', line 6

def names
  @names
end

#national_destination_code_lengthsObject (readonly)

Returns the value of attribute national_destination_code_lengths.



6
7
8
# File 'lib/countries.rb', line 6

def national_destination_code_lengths
  @national_destination_code_lengths
end

#national_number_lengthsObject (readonly)

Returns the value of attribute national_number_lengths.



6
7
8
# File 'lib/countries.rb', line 6

def national_number_lengths
  @national_number_lengths
end

#national_prefixObject (readonly)

Returns the value of attribute national_prefix.



6
7
8
# File 'lib/countries.rb', line 6

def national_prefix
  @national_prefix
end

#numberObject (readonly)

Returns the value of attribute number.



6
7
8
# File 'lib/countries.rb', line 6

def number
  @number
end

#regionObject (readonly)

Returns the value of attribute region.



6
7
8
# File 'lib/countries.rb', line 6

def region
  @region
end

#subregionObject (readonly)

Returns the value of attribute subregion.



6
7
8
# File 'lib/countries.rb', line 6

def subregion
  @subregion
end

Class Method Details

.[](query) ⇒ Object



31
32
33
# File 'lib/countries.rb', line 31

def self.[](query)
  self.search(query)
end

.search(query) ⇒ Object



27
28
29
# File 'lib/countries.rb', line 27

def self.search(query)
  Country.new(Data[query])
end

Instance Method Details

#subdivisionsObject



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

def subdivisions
  @subdivisions ||= subdivisions? ? YAML.load_file(File.join(File.dirname(__FILE__), 'data', 'subdivisions', "#{alpha2}.yaml")) : {}
end

#subdivisions?Boolean

Returns:

  • (Boolean)


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

def subdivisions?
  File.exist?(File.join(File.dirname(__FILE__), 'data', 'subdivisions', "#{alpha2}.yaml"))
end