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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 259da34dd7234726246bba70e468b739ec2e0c1e604383952becc6066d863d1d
4
- data.tar.gz: '07248a7a573b1a34054a87b2b562d3c4ce506d89baaf35fc94189f95b3bbe87c'
3
+ metadata.gz: 8385e27b2c596697ccca7422634a95e73ca4aaff39ee381e61b53893e12e1cd7
4
+ data.tar.gz: a40f9114345448971881408739a5d3ad678d1033528478b402b661d8cd2c83d1
5
5
  SHA512:
6
- metadata.gz: c77627c344abf4022f05f3e8f27c070e326f5b0d65c98d91450d1789f7b94f0d39e87a20f4b5dfcae404796066d9e4cace0b4dfabbf242d5811e32477baa7389
7
- data.tar.gz: b558046d41b352a96905fdd5052ec4da2aa4151b04725d8f8c7fb41eab529c393c80697c87d760d011f9e794dcce7dba0f8f39387bc3aa312f88610f762bb7f5
6
+ metadata.gz: d6dbfb89cf4114cfe6042ea769cb16d7d10d51be26b96e262c1a4285ccc0978210bd640900dccd12fea6d0aeaf41509c0eaca09be2e0e0df5a899936ba16ca7e
7
+ data.tar.gz: 81f89c6c6e5b44c613505bf95a27dcc8171f8ca35e90b8eb62972b36e31e20ae55729e9d0df1968900a77798abcd5779e81dc0712d767e2a025a852f4d0b5ac4
data/CHANGELOG.md CHANGED
@@ -1,4 +1,8 @@
1
- # 0.0.3 (June 9, 2021)
1
+ # 0.0.4 (June 15, 2021)
2
+
3
+ - Add the Llaxta.alpha2 method
4
+
5
+ # 0.0.3 (June 9, 2021)
2
6
 
3
7
  - Restore locale files
4
8
 
@@ -8,4 +12,4 @@
8
12
 
9
13
  # 0.0.1 (June 8, 2021)
10
14
 
11
- - Initial commit
15
+ - Initial commit
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
- There's an only public method called `t` which receives the country Alpha2 code and the locale corresponding to the language in which the country name is going to be shown.
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.3")
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.3
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-09 00:00:00.000000000 Z
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.2.15
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: