1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
module ISO3166
class Country
extend CountryClassMethods
include Emoji
attr_reader :data
ISO3166::DEFAULT_COUNTRY_HASH.each do |method_name, _type|
define_method method_name do
data[method_name.to_s]
end
end
ISO3166::DEFAULT_COUNTRY_HASH['geo'].each do |method_name, _type|
define_method method_name do
data['geo'][method_name.to_s]
end
end
def initialize(country_data)
@country_data_or_code = country_data
reload
end
def valid?
!(data.nil? || data.empty?)
end
alias zip postal_code
alias zip? postal_code
alias postal_code? postal_code
alias zip_format postal_code_format
alias languages languages_official
def latitude_dec
if RUBY_VERSION =~ /^3\.\d\.\d/
warn "DEPRECATION WARNING: The Country#latitude_dec method has been deprecated and will be removed in 5.0. Please use Country#latitude instead.", uplevel: 1, category: :deprecated
else
warn "DEPRECATION WARNING: The Country#latitude_dec method has been deprecated and will be removed in 5.0. Please use Country#latitude instead.", uplevel: 1
end
latitude
end
def longitude_dec
if RUBY_VERSION =~ /^3\.\d\.\d/
warn "DEPRECATION WARNING: The Country#longitude_dec method has been deprecated and will be removed in 5.0. Please use Country#longitude instead.", uplevel: 1, category: :deprecated
else
warn "DEPRECATION WARNING: The Country#longitude_dec method has been deprecated and will be removed in 5.0. Please use Country#longitude instead.", uplevel: 1
end
longitude
end
def ==(other)
other.respond_to?(:alpha2) && other.alpha2 == alpha2
end
def eql?(other)
self == other
end
def hash
[alpha2, alpha3].hash
end
def <=>(other)
to_s <=> other.to_s
end
# +true+ if this Country has any Subdivisions.
def subdivisions?
!subdivisions.empty?
end
# @return [Array<ISO3166::Subdivision>] the list of subdivisions for this Country.
def subdivisions
@subdivisions ||= if data['subdivisions']
self.class.create_subdivisions(data['subdivisions'])
else
self.class.subdivisions(alpha2)
end
end
# @param locale [String] The locale to use for translations.
# @return [Array<Array>] This Country's subdivision pairs of names and codes.
def subdivision_names_with_codes(locale = 'en')
subdivisions.map { |k, v| [v.translations[locale] || v.name, k] }
end
alias states subdivisions
# +true+ if this country is a member of the European Union.
def in_eu?
data['eu_member'].nil? ? false : data['eu_member']
end
# +true+ if this country is a member of the European Economic Area.
def in_eea?
data['eea_member'].nil? ? false : data['eea_member']
end
# +true+ if this country is a member of the European Single Market.
def in_esm?
data['esm_member'].nil? ? in_eea? : data['esm_member']
end
def to_s
data['iso_short_name']
end
# @return [Array<String>] the list of names for this Country in all loaded locales.
def translated_names
data['translations'].values
end
# @param locale [String] The locale to use for translations.
# @return [String] the name of this Country in the selected locale.
def translation(locale = 'en')
data['translations'][locale.to_s.downcase]
end
# @return [String] the “common name” of this Country in English.
def common_name
ISO3166.configuration.locales = ISO3166.configuration.locales.append(:en).uniq
translation('en')
end
# @return [Array<String>] TThe list of names for this Country, in this Country's locales.
def local_names
ISO3166.configuration.locales = (ISO3166.configuration.locales + languages.map(&:to_sym)).uniq
reload
@local_names ||= languages.map { |language| translations[language] }
end
# @return [String] The name for this Country, in this Country's locale.
def local_name
@local_name ||= local_names.first
end
# @return [String] This Country's ISO Short Name
# @deprecated Use {#iso_short_name} instead.
def name
if RUBY_VERSION =~ /^3\.\d\.\d/
warn "DEPRECATION WARNING: The Country#name method has been deprecated. Please use Country#iso_short_name instead or refer to the README file for more information on this change.", uplevel: 1, category: :deprecated
else
warn "DEPRECATION WARNING: The Country#name method has been deprecated. Please use Country#iso_short_name instead or refer to the README file for more information on this change.", uplevel: 1
end
iso_short_name
end
# @return [Array<String>] Array of unofficial, slang names or aliases for this Country
# @deprecated Use {#unofficial_names} instead.
def names
if RUBY_VERSION =~ /^3\.\d\.\d/
warn "DEPRECATION WARNING: The Country#names method has been deprecated. Please use Country#unofficial_names instead or refer to the README file for more information on this change.", uplevel: 1, category: :deprecated
else
warn "DEPRECATION WARNING: The Country#names method has been deprecated. Please use Country#unofficial_names instead or refer to the README file for more information on this change.", uplevel: 1
end
unofficial_names
end
# @!attribute alpha2
# @return [String] the ISO3166 alpha-2 code for this Country
#
# @!attribute alpha3
# @return [String] the ISO3166 alpha-3 code for this Country
#
# @!attribute address_format
# @return [String] a template for formatting addresses in this Country.
#
# @!attribute continent
# @return [String] the continent for this Country
#
# @!attribute country_code
# @return [String] the country calling code for this Country
#
# @!attribute currency_code
# @return [String] the ISO 4217 currency code for this Country
#
# @!attribute gec
# @return [String] the "Geopolitical Entities and Codes", formerly FIPS 10-4 code for this Country
#
# @!attribute geo
# @return [Hash] the hash of coordinates for this Country.
#
# @!attribute international_prefix
# @return [String] the phone prefix used in this Country for dialing international numbers
#
# @!attribute ioc
# @return [String] The International Olympic Committee code for for this Country
#
# @!attribute national_destination_code_lengths
# @return [Array<Integer>] Lengths of phone number destination codes
#
# @!attribute national_number_lengths
# @return [Array<Integer>] Lengths of phone numbers
#
# @!attribute national_prefix
# @return [String] the phone prefix used in this Country for dialing national numbers
#
# @!attribute nanp_prefix
# @return [String] the NANP prefix code
#
# @!attribute nationality
# @return [String] the nationality for this Country, in English
#
# @!attribute number
# @return [String] The ISO 3166-1 numeric code for this Country
#
# @!attribute languages_official
# @return [Array<String>] the list of official languages (locale codes) for this Country
#
# @!attribute languages_spoken
# @return [Array<String>] the list of spoken languages (locale codes) for this Country
#
# @!attribute translations
# @return [Hash] The hash of country name translations for this Country.
#
# @!attribute postal_code
# @return [Boolean] Does this Country uses postal codes in addresses
#
# @!attribute postal_code_format
# @return [String] The regex for valid postal codes in this Country
#
# @!attribute region
# @return [String] The Region this country is in. Approximately matches the United Nations geoscheme
#
# @!attribute unofficial_names
# @return [Array<String>] Array of unofficial, slang names or aliases for this Country
#
# @!attribute start_of_week
# @return [String] The starting day of the week ( +'monday'+ or +'sunday'+ )
#
# @!attribute subregion
# @return [String] The Subegion this country is in. Approximately matches the United Nations geoscheme's Subregions
#
# @!attribute un_locode
# @return [String] The UN/LOCODE prefix for this Country
#
# @!attribute vat_rates
# @return [Hash] the hash of VAT Rates for this Country
#
# @!attribute world_region
# @return [String] The "World Region" this country is in: +"AMER"+ , +"APAC"+ or +"EMEA"+
private
def reload
@data = if @country_data_or_code.is_a?(Hash)
@country_data_or_code
else
ISO3166::Data.new(@country_data_or_code).call
end
end
end
end
|