countries_data 1.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cbb1f697b19e871725f709a13aa04b9544a9d8b613e8cb737421641c8e1749a6
4
+ data.tar.gz: 413a90d178a542fdaf81c07b73094322c726eca1359c7b7fa96332e9b5d62a9b
5
+ SHA512:
6
+ metadata.gz: 06e5b9208a3a634c819fb9a23378f6f70add7121b1eff348f08de7d84678d814b3dc53fc4e3923ef525e1b60e2ab14d4d3dd9b99ca573cc36d6cf29c0492e1d8
7
+ data.tar.gz: c5e3bb3edfff2f1376064be53b57ea1747b44a031f872ffc933b92370f4ccb3c4b1334bc50baf72cf72993f195a5fc499de726dfdd61386f6ed7950934386a57
@@ -0,0 +1,35 @@
1
+ require 'yaml'
2
+ require_relative './country'
3
+
4
+ module CountriesData
5
+ class Collection
6
+ DATA = YAML.safe_load(File.read(File.join(__dir__, 'data.yml')), symbolize_names: true).freeze
7
+
8
+ COUNTRIES = DATA[:countries].map { |c| Country.new(c) }.freeze
9
+
10
+ IDS_MAP = COUNTRIES.each_with_index.each_with_object({}) do |(country, index), prev|
11
+ prev[country.id] = index
12
+ end
13
+
14
+ PHONE_CODES_MAP = COUNTRIES.each_with_index.each_with_object({}) do |(country, index), prev|
15
+ next prev if country.phone_code.nil?
16
+
17
+ prev[country.phone_code] = [] if prev[country.phone_code].nil?
18
+ prev[country.phone_code].push(index)
19
+ end
20
+
21
+ def self.all
22
+ COUNTRIES
23
+ end
24
+
25
+ def self.find_by_id(id)
26
+ index = IDS_MAP[id.to_s]
27
+ return COUNTRIES[index] unless index.nil?
28
+ end
29
+
30
+ def self.find_by_phone_code(phone_code)
31
+ indexes = PHONE_CODES_MAP[phone_code] || []
32
+ indexes.map { |index| COUNTRIES[index] }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module CountriesData
2
+ class Country
3
+ attr_reader :id
4
+ attr_reader :emoji
5
+ attr_reader :names
6
+ attr_reader :phone_code
7
+
8
+ PHONE_MIN_LENGTH = 5
9
+ PHONE_MAX_LENGTH = 14
10
+
11
+ def initialize(data)
12
+ @id = data[:id]
13
+ @emoji = data[:emoji]
14
+ @names = data[:names]
15
+ @phone_code = data[:phone_code]
16
+ @phone_min_length = data[:phone_min_length]
17
+ @phone_max_length = data[:phone_max_length]
18
+ end
19
+
20
+ def name(locale)
21
+ names[locale.to_sym]
22
+ end
23
+
24
+ def phone_min_length
25
+ @phone_min_length || PHONE_MIN_LENGTH
26
+ end
27
+
28
+ def phone_max_length
29
+ @phone_max_length || PHONE_MAX_LENGTH
30
+ end
31
+ end
32
+ end