Skip to content

Commit

Permalink
Add conversion methods for country codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mechnicov committed May 14, 2024
1 parent b71eae3 commit ef14cc7
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,14 @@ ISO3166::Country.collect_likely_countries_by_subdivision_name("San José",:commo
=> ["Costa Rica", "Uruguay"]
```

### Conversions

```ruby
ISO3166::Country.from_alpha3_to_alpha2('USA') # => "US"
ISO3166::Country.from_alpha2_to_alpha3('US') # => "USA"

ISO3166::Country.from_alpha2_to_alpha3('--') # => nil
```

## Currencies

Expand Down
1 change: 1 addition & 0 deletions lib/countries/country.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module ISO3166
class Country
extend CountryClassMethods
extend ConversionMethods
extend CountryFinderMethods
include Emoji
include CountrySubdivisionMethods
Expand Down
17 changes: 17 additions & 0 deletions lib/countries/country/conversion_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module ISO3166
module ConversionMethods
# @param alpha2 [String] ISO 3166 alpha-2 country code.
# @return [String] ISO 3166 alpha-3 country code.
def from_alpha2_to_alpha3(alpha2)
find_country_by_alpha2(alpha2)&.alpha3
end

# @param alpha3 [String] ISO 3166 alpha-3 country code.
# @return [String] ISO 3166 alpha-2 country code.
def from_alpha3_to_alpha2(alpha3)
find_country_by_alpha3(alpha3)&.alpha2
end
end
end
1 change: 1 addition & 0 deletions lib/countries/iso3166.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'countries/translations'
require 'countries/country/country_subdivision_methods'
require 'countries/country/class_methods'
require 'countries/country/conversion_methods'
require 'countries/country/finder_methods'
require 'countries/country/emoji'
require 'countries/country'
Expand Down
34 changes: 34 additions & 0 deletions spec/country_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,40 @@
end
end

describe 'conversion methods' do
describe 'from_alpha2_to_alpha3' do
subject { ISO3166::Country.from_alpha2_to_alpha3(alpha2) }

context 'when country exists' do
let(:alpha2) { 'US' }

it { is_expected.to eq('USA') }
end

context 'when country does not exist' do
let(:alpha2) { '..' }

it { is_expected.to be_nil }
end
end

describe 'from_alpha3_to_alpha2' do
subject { ISO3166::Country.from_alpha3_to_alpha2(alpha3) }

context 'when country exists' do
let(:alpha3) { 'USA' }

it { is_expected.to eq('US') }
end

context 'when country does not exist' do
let(:alpha3) { '...' }

it { is_expected.to be_nil }
end
end
end

describe 'hash finder methods' do
context "when search name in 'iso_short_name'" do
subject { ISO3166::Country.find_by_iso_short_name('Poland') }
Expand Down

0 comments on commit ef14cc7

Please sign in to comment.