Class: ISO3166::Country

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

Defined Under Namespace

Classes: TooManyArguments

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml_node) ⇒ Country

Returns a new instance of Country.



41
42
43
# File 'lib/iso3166/country.rb', line 41

def initialize(xml_node)
  @xml_node = xml_node
end

Instance Attribute Details

#xml_nodeObject (readonly)

Returns the value of attribute xml_node.



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

def xml_node
  @xml_node
end

Class Method Details

.[](code) ⇒ Object



29
30
31
# File 'lib/iso3166/country.rb', line 29

def self.[](code)
  new(code)
end

.allObject



33
34
35
36
37
# File 'lib/iso3166/country.rb', line 33

def self.all
  ISO3166::XMLData.all_names_with_codes.map do |name, alpha2|
    new(alpha2)
  end
end

.all_names_with_codesObject



25
26
27
# File 'lib/iso3166/country.rb', line 25

def self.all_names_with_codes
  ISO3166::XMLData.all_names_with_codes
end

.find_by(alpha2: nil, alpha3: nil) ⇒ Object

Raises:



15
16
17
18
19
20
21
22
23
# File 'lib/iso3166/country.rb', line 15

def self.find_by(alpha2: nil, alpha3: nil)
  raise TooManyArguments, "submit either alpha2 or alpha3" if alpha2 && alpha3

  if alpha2
    new(alpha2)
  elsif alpha3 && (xml_node = ISO3166::XMLData.find_by_alpha3(alpha3))
    new(nil, xml_node: xml_node)
  end
end

.new(code, xml_node: nil) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/iso3166/country.rb', line 7

def self.new(code, xml_node: nil)
  return super(xml_node) if xml_node

  if (xml_node = ISO3166::XMLData.find(code))
    super(xml_node)
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



92
93
94
# File 'lib/iso3166/country.rb', line 92

def ==(other)
  other.respond_to?(:alpha2) && other.alpha2 == alpha2
end

#alpha2Object



45
46
47
# File 'lib/iso3166/country.rb', line 45

def alpha2
  @_alpha2 ||= xml_node.at_xpath("./alpha-2-code").text
end

#alpha3Object



49
50
51
# File 'lib/iso3166/country.rb', line 49

def alpha3
  @_alpha3 ||= xml_node.at_xpath("./alpha-3-code").text
end

#currency_codeObject



88
89
90
# File 'lib/iso3166/country.rb', line 88

def currency_code
  @_currency_code ||= yml_data["currency_code"]
end

#full_nameObject



57
58
59
# File 'lib/iso3166/country.rb', line 57

def full_name
  @_full_name ||= xml_node.at_xpath("./full-name[@lang3code='eng']").text
end

#in_eu?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/iso3166/country.rb', line 69

def in_eu?
  !!yml_data["eu_member"]
end

#nameObject



53
54
55
# File 'lib/iso3166/country.rb', line 53

def name
  @_name ||= xml_node.at_xpath("./short-name[@lang3code='eng']").text
end

#numberObject



65
66
67
# File 'lib/iso3166/country.rb', line 65

def number
  @_number ||= xml_node.at_xpath("./numeric-code").text
end

#postal_code_formatObject



73
74
75
# File 'lib/iso3166/country.rb', line 73

def postal_code_format
  yml_data["postal_code_format"]
end

#postal_code_regexpObject



77
78
79
80
81
82
# File 'lib/iso3166/country.rb', line 77

def postal_code_regexp
  pattern = yml_data["postal_code_regexp"]
  return if pattern.nil?

  @_postal_code_regexp ||= Regexp.new(pattern)
end

#translationObject



61
62
63
# File 'lib/iso3166/country.rb', line 61

def translation
  @_translation ||= name.sub(" (the)", "")
end

#yml_dataObject



84
85
86
# File 'lib/iso3166/country.rb', line 84

def yml_data
  @_yml_data ||= ISO3166::YMLData.find(alpha2) || {}
end