llaxta 0.0.3 → 0.0.4
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/CHANGELOG.md +6 -2
- data/README.md +10 -1
- data/lib/llaxta.rb +18 -1
- data/spec/llaxta_spec.rb +33 -3
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8385e27b2c596697ccca7422634a95e73ca4aaff39ee381e61b53893e12e1cd7
|
4
|
+
data.tar.gz: a40f9114345448971881408739a5d3ad678d1033528478b402b661d8cd2c83d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6dbfb89cf4114cfe6042ea769cb16d7d10d51be26b96e262c1a4285ccc0978210bd640900dccd12fea6d0aeaf41509c0eaca09be2e0e0df5a899936ba16ca7e
|
7
|
+
data.tar.gz: 81f89c6c6e5b44c613505bf95a27dcc8171f8ca35e90b8eb62972b36e31e20ae55729e9d0df1968900a77798abcd5779e81dc0712d767e2a025a852f4d0b5ac4
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,7 @@ Get the name of any of the 249 countries listed in English US, Spanish, Brazilia
|
|
2
2
|
|
3
3
|
### Usage
|
4
4
|
|
5
|
-
|
5
|
+
`Llaxta.t` receives the country Alpha2 code and the locale corresponding to the language in which the country name is going to be shown.
|
6
6
|
|
7
7
|
```ruby
|
8
8
|
Llaxta.t("CH", "en_us") # => "Switzerland"
|
@@ -18,3 +18,12 @@ Llaxta.t("CH", "us") # => "Switzerland"
|
|
18
18
|
Llaxta.t("CH", "br") # => "Suíça"
|
19
19
|
Llaxta.t("CH", "cn") # => "瑞士"
|
20
20
|
```
|
21
|
+
|
22
|
+
`Llaxta.alpha2` receives a country name and a locale, and returns the country alpha2 if it can be found through the given country name for the given locale.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
Llaxta.alpha2("Etiópia", "br") # => "ET"
|
26
|
+
Llaxta.alpha2("埃塞俄比亚", "cn") # => "ET"
|
27
|
+
Llaxta.alpha2("Etiopía", "es") # => "ET"
|
28
|
+
Llaxta.alpha2("Ethiopia", "us") # => "ET"
|
29
|
+
```
|
data/lib/llaxta.rb
CHANGED
@@ -4,17 +4,29 @@ require "yaml"
|
|
4
4
|
require "llaxta/exceptions"
|
5
5
|
|
6
6
|
class Llaxta
|
7
|
-
VERSION = Gem::Version.new("0.0.
|
7
|
+
VERSION = Gem::Version.new("0.0.4")
|
8
8
|
|
9
9
|
class << self
|
10
|
+
# @param [String] alpha2
|
11
|
+
# @param [String] locale
|
12
|
+
# @return [String]
|
10
13
|
def t(alpha2, locale)
|
11
14
|
raise Exceptions::AlphaMissing unless alpha2
|
12
15
|
|
13
16
|
dictionary(locale)[alpha2]
|
14
17
|
end
|
15
18
|
|
19
|
+
# @param [String] country_name
|
20
|
+
# @param [String] locale
|
21
|
+
# @return [String]
|
22
|
+
def alpha2(country_name, locale)
|
23
|
+
dictionary(locale).key(country_name)
|
24
|
+
end
|
25
|
+
|
16
26
|
private
|
17
27
|
|
28
|
+
# @param [String] locale
|
29
|
+
# @return [Hash<String, String>]
|
18
30
|
def dictionary(locale)
|
19
31
|
@@locales ||= {}
|
20
32
|
@@locales[locale] ||= new(locale).send(:load_dict)
|
@@ -36,18 +48,23 @@ class Llaxta
|
|
36
48
|
}.freeze
|
37
49
|
private_constant :SUPPORTED_LOCALES
|
38
50
|
|
51
|
+
# @param [String] locale
|
52
|
+
# @return [void]
|
39
53
|
def initialize(locale)
|
40
54
|
raise Exceptions::LocaleMissing unless locale
|
41
55
|
|
42
56
|
@locale = SUPPORTED_LOCALES[locale]
|
43
57
|
end
|
44
58
|
|
59
|
+
# @return [void]
|
60
|
+
# @raise [Exceptions::LocaleNotFound] if unable to load a YAML file given an unexpected locale
|
45
61
|
def load_dict
|
46
62
|
YAML.load_file(locale_path)
|
47
63
|
rescue Errno::ENOENT
|
48
64
|
raise Exceptions::LocaleNotFound.new(locale)
|
49
65
|
end
|
50
66
|
|
67
|
+
# @return [String]
|
51
68
|
def locale_path
|
52
69
|
File.expand_path("../llaxta/locales/#{locale}.yml", __FILE__)
|
53
70
|
end
|
data/spec/llaxta_spec.rb
CHANGED
@@ -43,21 +43,51 @@ RSpec.describe Llaxta do
|
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
+
describe ".alpha2" do
|
47
|
+
context "when country exists in the file for the given language" do
|
48
|
+
context "with locale br" do
|
49
|
+
it { expect(Llaxta.alpha2("Etiópia", "br")).to eq("ET") }
|
50
|
+
end
|
51
|
+
|
52
|
+
context "with locale cn" do
|
53
|
+
it { expect(Llaxta.alpha2("埃塞俄比亚", "cn")).to eq("ET") }
|
54
|
+
end
|
55
|
+
|
56
|
+
context "with locale es" do
|
57
|
+
it { expect(Llaxta.alpha2("Etiopía", "es")).to eq("ET") }
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with locale us" do
|
61
|
+
it { expect(Llaxta.alpha2("Ethiopia", "us")).to eq("ET") }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when country does not exist in the file for the given language" do
|
66
|
+
it { expect(Llaxta.alpha2("NA", "us")).to be_nil }
|
67
|
+
end
|
68
|
+
|
69
|
+
context "with an unsupported locale" do
|
70
|
+
it "raises a LocaleNotFound exception" do
|
71
|
+
expect { Llaxta.alpha2("Chile", "oa") }.to raise_exception Exceptions::LocaleNotFound
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
46
76
|
describe "exceptions" do
|
47
77
|
context "when locale is NOT valid" do
|
48
|
-
it "raises" do
|
78
|
+
it "raises a LocaleNotFound exception" do
|
49
79
|
expect { Llaxta.t("PE", "2GFA") }.to raise_exception Exceptions::LocaleNotFound
|
50
80
|
end
|
51
81
|
end
|
52
82
|
|
53
83
|
context "when alpha is nil" do
|
54
|
-
it "raises" do
|
84
|
+
it "raises a AlphaMissing exception" do
|
55
85
|
expect { Llaxta.t(nil, "br") }.to raise_exception Exceptions::AlphaMissing
|
56
86
|
end
|
57
87
|
end
|
58
88
|
|
59
89
|
context "when locale is nil" do
|
60
|
-
it "raises" do
|
90
|
+
it "raises a LocaleMissing exception" do
|
61
91
|
expect { Llaxta.t("PE", nil) }.to raise_exception Exceptions::LocaleMissing
|
62
92
|
end
|
63
93
|
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: llaxta
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Victor Rodriguez
|
8
8
|
- Sebastian Palma
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2021-06-
|
12
|
+
date: 2021-06-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -93,7 +93,7 @@ licenses:
|
|
93
93
|
metadata:
|
94
94
|
source_code_uri: https://github.com/kucho/llaxta
|
95
95
|
changelog_uri: https://github.com/kucho/llaxta/blob/master/CHANGELOG.md
|
96
|
-
post_install_message:
|
96
|
+
post_install_message:
|
97
97
|
rdoc_options: []
|
98
98
|
require_paths:
|
99
99
|
- lib
|
@@ -108,8 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
108
|
- !ruby/object:Gem::Version
|
109
109
|
version: '0'
|
110
110
|
requirements: []
|
111
|
-
rubygems_version: 3.
|
112
|
-
signing_key:
|
111
|
+
rubygems_version: 3.1.4
|
112
|
+
signing_key:
|
113
113
|
specification_version: 4
|
114
114
|
summary: Translate ISO 3166-1 codes to Country names given a locale
|
115
115
|
test_files:
|