iso3166-countries 0.0.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +1 -1
- data/.gitignore +2 -0
- data/README.md +34 -6
- data/data/README.md +9 -0
- data/data/extended.yml +579 -0
- data/data/sample.xml +909 -0
- data/iso3166-countries.gemspec +1 -2
- data/lib/iso3166/countries.rb +20 -2
- data/lib/iso3166/countries/version.rb +1 -1
- data/lib/iso3166/country.rb +94 -0
- data/lib/iso3166/xml_data.rb +42 -0
- data/lib/iso3166/yml_data.rb +27 -0
- metadata +23 -3
data/iso3166-countries.gemspec
CHANGED
@@ -26,6 +26,5 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
27
|
spec.require_paths = ["lib"]
|
28
28
|
|
29
|
-
|
30
|
-
# spec.add_dependency "example-gem", "~> 1.0"
|
29
|
+
spec.add_dependency "nokogiri", "~> 1.11"
|
31
30
|
end
|
data/lib/iso3166/countries.rb
CHANGED
@@ -1,10 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "nokogiri"
|
4
|
+
require "yaml"
|
3
5
|
require_relative "countries/version"
|
6
|
+
require_relative "xml_data"
|
7
|
+
require_relative "yml_data"
|
8
|
+
require_relative "country"
|
4
9
|
|
5
10
|
module ISO3166
|
6
11
|
module Countries
|
7
|
-
class
|
8
|
-
|
12
|
+
class << self
|
13
|
+
def data_path
|
14
|
+
@data_path ||
|
15
|
+
File.join(__dir__, "..", "..", "data", "sample.xml")
|
16
|
+
end
|
17
|
+
|
18
|
+
def yml_data_path
|
19
|
+
File.join(__dir__, "..", "..", "data", "extended.yml")
|
20
|
+
end
|
21
|
+
|
22
|
+
def data_path=(path)
|
23
|
+
ISO3166::XMLData.reset!
|
24
|
+
@data_path = path
|
25
|
+
end
|
26
|
+
end
|
9
27
|
end
|
10
28
|
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ISO3166
|
4
|
+
class Country
|
5
|
+
class TooManyArguments < StandardError; end
|
6
|
+
|
7
|
+
def self.new(code, xml_node: nil)
|
8
|
+
return super(xml_node) if xml_node
|
9
|
+
|
10
|
+
if (xml_node = ISO3166::XMLData.find(code))
|
11
|
+
super(xml_node)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.find_by(alpha2: nil, alpha3: nil)
|
16
|
+
raise TooManyArguments, "submit either alpha2 or alpha3" if alpha2 && alpha3
|
17
|
+
|
18
|
+
if alpha2
|
19
|
+
new(alpha2)
|
20
|
+
elsif alpha3 && (xml_node = ISO3166::XMLData.find_by_alpha3(alpha3))
|
21
|
+
new(nil, xml_node: xml_node)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.all_names_with_codes
|
26
|
+
ISO3166::XMLData.all_names_with_codes
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.[](code)
|
30
|
+
new(code)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.all
|
34
|
+
ISO3166::XMLData.all_names_with_codes.map do |name, alpha2|
|
35
|
+
new(alpha2)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
attr_reader :xml_node
|
40
|
+
|
41
|
+
def initialize(xml_node)
|
42
|
+
@xml_node = xml_node
|
43
|
+
end
|
44
|
+
|
45
|
+
def alpha2
|
46
|
+
@_alpha2 ||= xml_node.at_xpath("./alpha-2-code").text
|
47
|
+
end
|
48
|
+
|
49
|
+
def alpha3
|
50
|
+
@_alpha3 ||= xml_node.at_xpath("./alpha-3-code").text
|
51
|
+
end
|
52
|
+
|
53
|
+
def name
|
54
|
+
@_name ||= xml_node.at_xpath("./short-name[@lang3code='eng']").text
|
55
|
+
end
|
56
|
+
|
57
|
+
def full_name
|
58
|
+
@_full_name ||= xml_node.at_xpath("./full-name[@lang3code='eng']").text
|
59
|
+
end
|
60
|
+
|
61
|
+
def translation
|
62
|
+
@_translation ||= name.sub(" (the)", "")
|
63
|
+
end
|
64
|
+
|
65
|
+
def number
|
66
|
+
@_number ||= xml_node.at_xpath("./numeric-code").text
|
67
|
+
end
|
68
|
+
|
69
|
+
def in_eu?
|
70
|
+
!!yml_data["eu_member"]
|
71
|
+
end
|
72
|
+
|
73
|
+
def postal_code_format
|
74
|
+
yml_data["postal_code_format"]
|
75
|
+
end
|
76
|
+
|
77
|
+
def postal_code_regexp
|
78
|
+
yml_data["postal_code_regexp"]
|
79
|
+
end
|
80
|
+
|
81
|
+
def yml_data
|
82
|
+
@_yml_data ||= ISO3166::YMLData.find(alpha2) || {}
|
83
|
+
end
|
84
|
+
|
85
|
+
def currency_code
|
86
|
+
@_currency_code ||= yml_data["currency_code"]
|
87
|
+
end
|
88
|
+
|
89
|
+
def ==(other)
|
90
|
+
other.respond_to?(:alpha2) && other.alpha2 == alpha2
|
91
|
+
end
|
92
|
+
alias_method :eql?, :==
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ISO3166
|
4
|
+
class XMLData
|
5
|
+
@@semaphore = Mutex.new
|
6
|
+
@@data = nil
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def reset!
|
10
|
+
@@data = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def reload!
|
14
|
+
return if @@data
|
15
|
+
|
16
|
+
@@semaphore.synchronize do
|
17
|
+
@@data = File.open(ISO3166::Countries.data_path) { |f| Nokogiri::XML(f) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(code)
|
22
|
+
reload!
|
23
|
+
@@data.at_xpath("//country[@id='#{code}']")
|
24
|
+
end
|
25
|
+
|
26
|
+
def find_by_alpha3(code)
|
27
|
+
reload!
|
28
|
+
@@data.at_xpath("//country[alpha-3-code[text()='#{code}']]")
|
29
|
+
end
|
30
|
+
|
31
|
+
def all_names_with_codes
|
32
|
+
reload!
|
33
|
+
@@data.xpath("//country[status[text()='officially-assigned']]").map do |xml_node|
|
34
|
+
[
|
35
|
+
xml_node.at_xpath("./short-name[@lang3code='eng']").text,
|
36
|
+
xml_node.at_xpath("./alpha-2-code").text
|
37
|
+
]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ISO3166
|
4
|
+
class YMLData
|
5
|
+
@@semaphore = Mutex.new
|
6
|
+
@@data = nil
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def reset!
|
10
|
+
@@data = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def reload!
|
14
|
+
return if @@data
|
15
|
+
|
16
|
+
@@semaphore.synchronize do
|
17
|
+
@@data = YAML.load_file(ISO3166::Countries.yml_data_path)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def find(code)
|
22
|
+
reload!
|
23
|
+
@@data[code.to_s]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iso3166-countries
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ali Ismayilov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-04-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nokogiri
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
13
27
|
description:
|
14
28
|
email:
|
15
29
|
- ali@ismailov.info
|
@@ -27,9 +41,15 @@ files:
|
|
27
41
|
- Rakefile
|
28
42
|
- bin/console
|
29
43
|
- bin/setup
|
44
|
+
- data/README.md
|
45
|
+
- data/extended.yml
|
46
|
+
- data/sample.xml
|
30
47
|
- iso3166-countries.gemspec
|
31
48
|
- lib/iso3166/countries.rb
|
32
49
|
- lib/iso3166/countries/version.rb
|
50
|
+
- lib/iso3166/country.rb
|
51
|
+
- lib/iso3166/xml_data.rb
|
52
|
+
- lib/iso3166/yml_data.rb
|
33
53
|
homepage: https://github.com/viaeurope/iso3166-countries
|
34
54
|
licenses:
|
35
55
|
- MIT
|