countries 4.2.3 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/codeql-analysis.yml +70 -0
  3. data/.github/workflows/tests.yml +1 -5
  4. data/.rubocop.yml +40 -1
  5. data/.rubocop_todo.yml +8 -41
  6. data/CHANGELOG.md +7 -2
  7. data/Gemfile +5 -3
  8. data/README.markdown +22 -5
  9. data/Rakefile +13 -31
  10. data/bin/console +1 -0
  11. data/countries.gemspec +7 -5
  12. data/lib/countries/cache/countries.json +1 -1
  13. data/lib/countries/configuration.rb +2 -0
  14. data/lib/countries/country/class_methods.rb +12 -97
  15. data/lib/countries/country/currency_methods.rb +2 -0
  16. data/lib/countries/country/emoji.rb +2 -3
  17. data/lib/countries/country/finder_methods.rb +81 -0
  18. data/lib/countries/country.rb +3 -40
  19. data/lib/countries/data/countries/KN.yaml +0 -1
  20. data/lib/countries/data.rb +34 -37
  21. data/lib/countries/global.rb +2 -0
  22. data/lib/countries/iso3166.rb +3 -0
  23. data/lib/countries/kwarg_struct.rb +2 -0
  24. data/lib/countries/mongoid.rb +2 -0
  25. data/lib/countries/sources/cldr/downloader.rb +8 -8
  26. data/lib/countries/sources/cldr/subdivision.rb +3 -0
  27. data/lib/countries/sources/cldr/subdivision_updater.rb +23 -17
  28. data/lib/countries/sources/local/cached_loader.rb +3 -0
  29. data/lib/countries/sources/local/subdivision.rb +5 -2
  30. data/lib/countries/sources.rb +2 -0
  31. data/lib/countries/structure.rb +1 -1
  32. data/lib/countries/subdivision.rb +2 -0
  33. data/lib/countries/tasks/geocoding.rake +15 -6
  34. data/lib/countries/tasks/postal_codes.rake +5 -3
  35. data/lib/countries/timezones.rb +5 -2
  36. data/lib/countries/translations.rb +2 -0
  37. data/lib/countries/version.rb +3 -1
  38. data/lib/countries.rb +2 -0
  39. data/spec/00_global_spec.rb +2 -0
  40. data/spec/configuration_spec.rb +11 -5
  41. data/spec/country_spec.rb +71 -22
  42. data/spec/data_spec.rb +24 -18
  43. data/spec/mongoid_spec.rb +2 -2
  44. data/spec/perf_spec.rb +16 -16
  45. data/spec/spec_helper.rb +2 -0
  46. data/spec/subdivision_spec.rb +6 -4
  47. data/spec/thread_safety_spec.rb +4 -3
  48. data/spec/timezone_spec.rb +2 -0
  49. metadata +14 -12
  50. data/lib/countries/setup.rb +0 -18
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './sources/local/cached_loader'
2
4
  require_relative './sources/local/subdivision'
3
5
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
- #
2
+
3
3
  module ISO3166
4
4
  DEFAULT_COUNTRY_HASH = {
5
5
  'address_format' => nil,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ISO3166
2
4
  Subdivision = KwargStruct.new(
3
5
  :name,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'geocoder'
2
4
  require 'retryable'
3
5
 
@@ -16,7 +18,7 @@ def geocode(query, params)
16
18
  Retryable.retryable(tries: 3, sleep: ->(n) { 2**n }) do
17
19
  Geocoder.search(query, params: params).first
18
20
  end
19
- rescue => e
21
+ rescue Geocoder::Error, Geocoder::LookupTimeout => e
20
22
  warn "Attempts exceeded for query #{query}, last error was #{e.message}"
21
23
  nil
22
24
  end
@@ -26,7 +28,7 @@ def load_country_yaml(alpha2)
26
28
  end
27
29
 
28
30
  def save_country_yaml(alpha2, data)
29
- File.open(File.join(ISO3166_ROOT_PATH, 'lib', 'countries', 'data', 'countries', "#{alpha2}.yaml"), 'w+') { |f| f.write data.to_yaml }
31
+ File.write(File.join(ISO3166_ROOT_PATH, 'lib', 'countries', 'data', 'countries', "#{alpha2}.yaml"), data.to_yaml)
30
32
  end
31
33
 
32
34
  def country_codes
@@ -44,7 +46,10 @@ namespace :geocode do
44
46
  data = load_country_yaml(country.alpha2)
45
47
 
46
48
  next unless (result = geocode(country.iso_short_name, {region: country.alpha2}))
47
- puts "WARNING:: Geocoder returned something that was not a country for #{country.alpha2}" unless result.types.include?('country')
49
+
50
+ unless result.types.include?('country')
51
+ puts "WARNING:: Geocoder returned something that was not a country for #{country.alpha2}"
52
+ end
48
53
  geometry = result.geometry
49
54
 
50
55
  # Extract center point data
@@ -55,6 +60,7 @@ namespace :geocode do
55
60
 
56
61
  # Extract bounding box data
57
62
  next unless geometry['bounds']
63
+
58
64
  data[code]['geo']['bounds'] = geometry['bounds']
59
65
  data[code]['geo']['min_latitude'] = geometry['bounds']['southwest']['lat']
60
66
  data[code]['geo']['min_longitude'] = geometry['bounds']['southwest']['lng']
@@ -73,10 +79,10 @@ namespace :geocode do
73
79
  ISO3166::Country.all.select(&:subdivisions?).each do |c|
74
80
  # Iterate subdivisions
75
81
  state_data = c.subdivisions.dup
76
- state_data.reject { |_, data| data['geo'] }.each do |code, data|
82
+ state_data.reject { |_, data| data['geo'] }.each do |code, _|
77
83
  location = "#{c.alpha2}-#{code}"
78
84
 
79
- next unless (result = geocode(location, {region: c.alpha2}))
85
+ next unless (result = geocode(location, { region: c.alpha2 }))
80
86
 
81
87
  geometry = result.geometry
82
88
  if geometry['location']
@@ -84,14 +90,17 @@ namespace :geocode do
84
90
  state_data[code]['geo']['latitude'] = geometry['location']['lat']
85
91
  state_data[code]['geo']['longitude'] = geometry['location']['lng']
86
92
  end
93
+
87
94
  next unless geometry['bounds']
95
+
88
96
  state_data[code]['geo']['min_latitude'] = geometry['bounds']['southwest']['lat']
89
97
  state_data[code]['geo']['min_longitude'] = geometry['bounds']['southwest']['lng']
90
98
  state_data[code]['geo']['max_latitude'] = geometry['bounds']['northeast']['lat']
91
99
  state_data[code]['geo']['max_longitude'] = geometry['bounds']['northeast']['lng']
92
100
  end
93
101
  # Write updated YAML for current country
94
- File.open(File.join(ISO3166_ROOT_PATH, 'lib', 'countries', 'data', 'subdivisions', "#{c.alpha2}.yaml"), 'w+') { |f| f.write state_data.to_yaml }
102
+ out = File.join(ISO3166_ROOT_PATH, 'lib', 'countries', 'data', 'subdivisions', "#{c.alpha2}.yaml")
103
+ File.write(out, state_data.to_yaml)
95
104
  end
96
105
  end
97
106
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :postal_codes do
2
4
  desc 'Retrieve and update postal codes and their format'
3
5
  task :update do
@@ -20,13 +22,13 @@ namespace :postal_codes do
20
22
  json = JSON.parse(response) rescue {}
21
23
  puts ' - Returned empty data. Skipping ' and next if json.empty?
22
24
 
23
- postal_code = ['postal_code', !!json['zip']]
25
+ postal_code = ['postal_code', !json['zip'].nil?]
24
26
  postal_code_format = ['postal_code_format', json['zip']]
25
27
 
26
28
  if postal_code_index
27
29
  data[postal_code_index] = postal_code
28
30
  else
29
- postal_code_index = data.find_index { |d| d[0] == 'nationality' } + 1 || data.size
31
+ postal_code_index = (data.find_index { |d| d[0] == 'nationality' } + 1) || data.size
30
32
  data.insert(postal_code_index, postal_code)
31
33
  end
32
34
 
@@ -42,7 +44,7 @@ namespace :postal_codes do
42
44
 
43
45
  yaml[country_code] = data.to_h
44
46
 
45
- File.open(country_file, 'w') { |file| file.write(yaml.to_yaml) }
47
+ File.write(country_file, yaml.to_yaml)
46
48
  end
47
49
  end
48
50
  end
@@ -1,13 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ISO3166
2
4
  # Extend Country class with support for timezones. Requires the {tzinfo}[https://github.com/tzinfo/tzinfo] gem
3
5
  #
4
6
  # gem 'tzinfo'
5
7
  #
6
8
  module TimezoneExtensions
9
+ # TODO: rename method to tz_country or something similar
7
10
  def timezones
8
- @tz_country ||= TZInfo::Country.get(alpha2)
11
+ @timezones ||= TZInfo::Country.get(alpha2)
9
12
  end
10
13
  end
11
14
  end
12
15
 
13
- ISO3166::Country.send(:include, ISO3166::TimezoneExtensions)
16
+ ISO3166::Country.include ISO3166::TimezoneExtensions
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ISO3166
2
4
  # Extend the hash class to allow locale lookup fall back behavior
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Countries
2
- VERSION = '4.2.3'.freeze
4
+ VERSION = '5.0.0'.freeze
3
5
  end
data/lib/countries.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sixarm_ruby_unaccent'
2
4
 
3
5
  require 'countries/version'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  describe 'global Country class', order: :defined do
2
4
  context "when loaded via 'iso3166' existence" do
3
5
  subject { defined?(Country) }
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
  require 'i18n'
4
5
 
@@ -16,14 +17,14 @@ describe 'ISO3166.configuration' do
16
17
  end
17
18
 
18
19
  it 'locales are assumed from I18n.available_locales' do
19
- I18n.available_locales = [:de, :en]
20
+ I18n.available_locales = %i[de en]
20
21
  ISO3166.reset
21
22
  expect(ISO3166::Country.new('DE').translation(:de)).to eq 'Deutschland'
22
23
  expect(ISO3166::Country.new('DE').translation(:es)).to eq nil
23
24
  end
24
25
 
25
26
  it 'unsupported locales do not affect supported locales' do
26
- I18n.available_locales = [:de, :en, :unsupported]
27
+ I18n.available_locales = %i[de en unsupported]
27
28
  ISO3166.reset
28
29
  expect(ISO3166::Country.new('DE').translation(:de)).to eq 'Deutschland'
29
30
  expect(ISO3166::Country.new('DE').translation(:es)).to eq nil
@@ -36,7 +37,7 @@ describe 'ISO3166.configuration' do
36
37
  end
37
38
 
38
39
  it 'locales can be changed' do
39
- ISO3166.configuration.locales = [:es, :de, :en]
40
+ ISO3166.configuration.locales = %i[es de en]
40
41
  expect(ISO3166::Country.new('DE').translation(:es)).to eq 'Alemania'
41
42
  expect(ISO3166::Country.new('DE').translation(:en)).to eq 'Germany'
42
43
  expect(ISO3166::Country.new('DE').translation(:de)).to eq 'Deutschland'
@@ -44,7 +45,12 @@ describe 'ISO3166.configuration' do
44
45
 
45
46
  it 'locales can be changed with a block' do
46
47
  ISO3166.configure do |config|
47
- config.locales = [:af, :am, :ar, :as, :az, :be, :bg, :bn, :br, :bs, :ca, :cs, :cy, :da, :de, :dz, :el, :en, :eo, :es, :et, :eu, :fa, :fi, :fo, :fr, :ga, :gl, :gu, :he, :hi, :hr, :hu, :hy, :ia, :id, :is, :it, :ja, :ka, :kk, :km, :kn, :ko, :ku, :lt, :lv, :mi, :mk, :ml, :mn, :mr, :ms, :mt, :nb, :ne, :nl, :nn, :oc, :or, :pa, :pl, :ps, :pt, :ro, :ru, :rw, :si, :sk, :sl, :so, :sq, :sr, :sv, :sw, :ta, :te, :th, :ti, :tk, :tl, :tr, :tt, :ug, :uk, :ve, :vi, :wa, :wo, :xh, :zh, :zu]
48
+ config.locales = %i[af am ar as az be bg bn br bs ca cs cy da de dz el en
49
+ eo es et eu fa fi fo fr ga gl gu he hi hr hu hy ia id
50
+ is it ja ka kk km kn ko ku lt lv mi mk ml mn mr ms mt
51
+ nb ne nl nn oc or pa pl ps pt ro ru rw si sk sl so sq
52
+ sr sv sw ta te th ti tk tl tr tt ug uk ve vi wa wo xh
53
+ zh zu]
48
54
  end
49
55
 
50
56
  expect(ISO3166::Country.new('DE').translations.size).to eq 92
data/spec/country_spec.rb CHANGED
@@ -1,4 +1,4 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
4
  NUM_OF_COUNTRIES = 249
@@ -48,7 +48,9 @@ describe ISO3166::Country do
48
48
  end
49
49
 
50
50
  it 'should return alternate names' do
51
- expect(country.unofficial_names).to eq(['United States', 'USA', 'Murica', 'Vereinigte Staaten von Amerika', 'États-Unis', 'Estados Unidos', 'アメリカ合衆国', 'Verenigde Staten'])
51
+ expect(country.unofficial_names).to eq(['United States', 'USA', 'Murica',
52
+ 'Vereinigte Staaten von Amerika', 'États-Unis',
53
+ 'Estados Unidos', 'アメリカ合衆国', 'Verenigde Staten'])
52
54
  end
53
55
 
54
56
  it 'should return translations' do
@@ -85,7 +87,7 @@ describe ISO3166::Country do
85
87
  expect(country.postal_code_format).to_not be_nil
86
88
 
87
89
  regex = Regexp.new(country.postal_code_format)
88
- expect(regex).to match("12345-6789")
90
+ expect(regex).to match('12345-6789')
89
91
 
90
92
  antarctica = ISO3166::Country.search('AQ')
91
93
  expect(antarctica.postal_code_format).to be_nil
@@ -123,7 +125,7 @@ describe ISO3166::Country do
123
125
  let(:country) { ISO3166::Country.search('BE') }
124
126
 
125
127
  it 'should return its local names based on its languages' do
126
- expect(country.local_names).to match_array(%w(België Belgique Belgien))
128
+ expect(country.local_names).to match_array(%w[België Belgique Belgien])
127
129
  end
128
130
 
129
131
  it 'should return its first local name' do
@@ -259,7 +261,7 @@ describe ISO3166::Country do
259
261
  end
260
262
 
261
263
  it 'should return an alphabetized list of subdivision names translated to current locale with codes' do
262
- ISO3166.configuration.locales = [:es, :de, :en]
264
+ ISO3166.configuration.locales = %i[es de en]
263
265
 
264
266
  subdivisions = ISO3166::Country.search('EG').subdivision_names_with_codes(:es)
265
267
  expect(subdivisions).to be_an(Array)
@@ -371,11 +373,11 @@ describe ISO3166::Country do
371
373
  expect(countries.first[0]).to be_a(String)
372
374
  expect(countries.first[0]).to eq('Afghanistan')
373
375
  expect(countries.size).to eq(NUM_OF_COUNTRIES)
374
- expect(countries.any?{|pair| !pair[0].html_safe?}).to eq(false)
376
+ expect(countries.any? { |pair| !pair[0].html_safe? }).to eq(false)
375
377
  end
376
378
 
377
379
  it 'should return an alphabetized list of all country names translated to current locale with ISOCODE alpha2' do
378
- ISO3166.configuration.locales = [:es, :de, :en]
380
+ ISO3166.configuration.locales = %i[es de en]
379
381
 
380
382
  countries = ISO3166::Country.all_names_with_codes(:es)
381
383
  expect(countries).to be_an(Array)
@@ -395,7 +397,7 @@ describe ISO3166::Country do
395
397
  end
396
398
 
397
399
  it 'should return an alphabetized list of all country names translated to current locale with ISOCODE alpha2' do
398
- ISO3166.configuration.locales = [:es, :de, :en]
400
+ ISO3166.configuration.locales = %i[es de en]
399
401
 
400
402
  countries = ISO3166::Country.all_names_with_codes(:es)
401
403
  expect(countries).to be_an(Array)
@@ -407,7 +409,7 @@ describe ISO3166::Country do
407
409
 
408
410
  describe 'translation' do
409
411
  it 'should return the localized name for a country to the selected locale' do
410
- ISO3166.configuration.locales = [:es, :de, :en]
412
+ ISO3166.configuration.locales = %i[es de en]
411
413
  countries = ISO3166::Country.new(:de).translation('de')
412
414
  expect(countries).to be_an(String)
413
415
  expect(countries).to eq('Deutschland')
@@ -426,7 +428,7 @@ describe ISO3166::Country do
426
428
 
427
429
  context 'should return variant locales' do
428
430
  it 'should return different value for Chinese variants' do
429
- ISO3166.configuration.locales = [:'zh-cn', :'zh-hk', :'zh-tw']
431
+ ISO3166.configuration.locales = %i[zh-cn zh-hk zh-tw]
430
432
  name_cn = ISO3166::Country['TW'].translation('zh-cn')
431
433
  name_hk = ISO3166::Country['TW'].translation('zh-hk')
432
434
  name_tw = ISO3166::Country['TW'].translation('zh-tw')
@@ -434,7 +436,7 @@ describe ISO3166::Country do
434
436
  end
435
437
 
436
438
  it 'should return different value for Portuguese variants' do
437
- ISO3166.configuration.locales = [:pt, :'pt-br']
439
+ ISO3166.configuration.locales = %i[pt pt-br]
438
440
  name_pt = ISO3166::Country['SG'].translation('pt')
439
441
  name_br = ISO3166::Country['SG'].translation('pt-br')
440
442
  expect([name_pt, name_br].uniq.size).to eql 2
@@ -447,7 +449,7 @@ describe ISO3166::Country do
447
449
  countries = ISO3166::Country.translations('fr')
448
450
  expect(countries).to be_an(Hash)
449
451
  expect(countries.first[0]).to eq('AW')
450
- expect(countries.first).to eq(%w(AW Aruba))
452
+ expect(countries.first).to eq(%w[AW Aruba])
451
453
  # countries missing the desired locale will not be added to the list
452
454
  # so all 250 countries may not be returned, 'fr' returns 249, for example
453
455
  expect(countries.size).to eq(NUM_OF_COUNTRIES)
@@ -457,7 +459,7 @@ describe ISO3166::Country do
457
459
  countries = ISO3166::Country.translations
458
460
  expect(countries).to be_an(Hash)
459
461
  expect(countries.first[0]).to eq('AW')
460
- expect(countries.first).to eq(%w(AW Aruba))
462
+ expect(countries.first).to eq(%w[AW Aruba])
461
463
  expect(countries.size).to eq(NUM_OF_COUNTRIES)
462
464
  end
463
465
  end
@@ -648,7 +650,9 @@ describe ISO3166::Country do
648
650
 
649
651
  context 'when finding by invalid attribute' do
650
652
  it 'should raise an error' do
651
- expect { ISO3166::Country.find_by_invalid('invalid') }.to raise_error(RuntimeError, "Invalid attribute name 'invalid'")
653
+ expect { ISO3166::Country.find_by_invalid('invalid') }.to(
654
+ raise_error(RuntimeError, "Invalid attribute name 'invalid'")
655
+ )
652
656
  end
653
657
  end
654
658
 
@@ -713,6 +717,49 @@ describe ISO3166::Country do
713
717
  end
714
718
  end
715
719
 
720
+ describe '#find_country_by_any_name' do
721
+ context 'when search name found' do
722
+ let(:uk) { ISO3166::Country.find_country_by_any_name('United Kingdom') }
723
+
724
+ it 'should be a country instance' do
725
+ expect(uk).to be_a(ISO3166::Country)
726
+ expect(uk.alpha2).to eq('GB')
727
+ end
728
+ end
729
+
730
+ context 'when search lowercase name found' do
731
+ let(:uk) { ISO3166::Country.find_country_by_any_name('united kingdom') }
732
+
733
+ it 'should be a country instance' do
734
+ expect(uk).to be_a(ISO3166::Country)
735
+ expect(uk.alpha2).to eq('GB')
736
+ end
737
+ end
738
+
739
+ context 'when the search term contains comma' do
740
+ let(:korea) { ISO3166::Country.find_country_by_any_name('Korea, Republic of') }
741
+
742
+ it 'should be a country instance' do
743
+ expect(korea).to be_a(ISO3166::Country)
744
+ expect(korea.alpha2).to eq('KR')
745
+ end
746
+ end
747
+
748
+ context 'when search translation found' do
749
+ before do
750
+ ISO3166.configure do |config|
751
+ config.locales = [:bs]
752
+ end
753
+ end
754
+ let(:uk) { ISO3166::Country.find_country_by_any_name('Velika Britanija') }
755
+
756
+ it 'should be a country instance' do
757
+ expect(uk).to be_a(ISO3166::Country)
758
+ expect(uk.alpha2).to eq('GB')
759
+ end
760
+ end
761
+ end
762
+
716
763
  context 'regression test for #388' do
717
764
  let(:no_country) { ISO3166::Country.find_country_by_translated_names(nil) }
718
765
 
@@ -729,6 +776,7 @@ describe ISO3166::Country do
729
776
  expect { uk }.to raise_error(RuntimeError)
730
777
  end
731
778
  end
779
+
732
780
  context 'when search name not found' do
733
781
  let(:bogus) { ISO3166::Country.find_country_by_unofficial_names('Does not exist') }
734
782
 
@@ -785,8 +833,9 @@ describe ISO3166::Country do
785
833
 
786
834
  context 'when finding by invalid attribute' do
787
835
  it 'should raise an error' do
788
- expect { ISO3166::Country.find_country_by_invalid('invalid') }.to raise_error(RuntimeError,
789
- "Invalid attribute name 'invalid'")
836
+ expect { ISO3166::Country.find_country_by_invalid('invalid') }.to(
837
+ raise_error(RuntimeError, "Invalid attribute name 'invalid'")
838
+ )
790
839
  end
791
840
  end
792
841
 
@@ -848,7 +897,7 @@ describe ISO3166::Country do
848
897
  end
849
898
 
850
899
  describe 'names in Data' do
851
- it 'should be unique (to allow .find_by_name work properly)' do
900
+ it 'should be unique (to allow .find_by_any_name work properly)' do
852
901
  names = ISO3166::Data.cache.map do |_k, v|
853
902
  [v['iso_short_name'], v['unofficial_names']].flatten.uniq
854
903
  end.flatten
@@ -947,7 +996,7 @@ describe ISO3166::Country do
947
996
 
948
997
  it 'should contain all keys for vat_rates' do
949
998
  expect(belgium.vat_rates).to be_a(Hash)
950
- expect(belgium.vat_rates.keys).to eq(%w(standard reduced super_reduced parking))
999
+ expect(belgium.vat_rates.keys).to eq(%w[standard reduced super_reduced parking])
951
1000
  end
952
1001
 
953
1002
  it 'should return an array of reduced vat rates' do
@@ -1021,7 +1070,7 @@ describe ISO3166::Country do
1021
1070
 
1022
1071
  subject { ISO3166::Country.pluck(*args) }
1023
1072
 
1024
- it "returns empty arrays" do
1073
+ it 'returns empty arrays' do
1025
1074
  expect(subject.first).to be_empty
1026
1075
  expect(subject.last).to be_empty
1027
1076
  end
@@ -1029,9 +1078,9 @@ describe ISO3166::Country do
1029
1078
  context "when asking for alpha2, alpha3 and iso_short_name" do
1030
1079
  let(:args) { [:alpha2, :alpha3, :iso_short_name] }
1031
1080
 
1032
- it "returns the correct values" do
1033
- expect(subject.first).to eq(["AD", "AND", "Andorra"])
1034
- expect(subject.last).to eq(["ZW", "ZWE", "Zimbabwe"])
1081
+ it 'returns the correct values' do
1082
+ expect(subject.first).to eq(%w[AD AND Andorra])
1083
+ expect(subject.last).to eq(%w[ZW ZWE Zimbabwe])
1035
1084
  end
1036
1085
  end
1037
1086
  end
data/spec/data_spec.rb CHANGED
@@ -1,4 +1,5 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  require 'spec_helper'
3
4
  require 'benchmark'
4
5
 
@@ -10,13 +11,13 @@ describe ISO3166::Data do
10
11
 
11
12
  it 'can load selective locales' do
12
13
  # ISO3166::Data.update_cache
13
- ISO3166.configuration.locales = [:es, :de, :en]
14
+ ISO3166.configuration.locales = %i[es de en]
14
15
  data = ISO3166::Data.new('US').call
15
16
  expect(data['translated_names'].size).to eq 3
16
17
  end
17
18
 
18
19
  it 'can load selective locales and reload efficiently' do
19
- ISO3166.configuration.locales = [:es, :de, :en]
20
+ ISO3166.configuration.locales = %i[es de en]
20
21
  data = ISO3166::Data.new('US').call
21
22
  expect(data['translations']).to eq('de' => 'Vereinigte Staaten', 'es' => 'Estados Unidos', 'en' => 'United States')
22
23
  expect(data['translated_names'].sort).to eq ['Vereinigte Staaten', 'Estados Unidos', 'United States'].sort
@@ -35,7 +36,7 @@ describe ISO3166::Data do
35
36
 
36
37
  it 'locales will load prior to return results' do
37
38
  # require 'memory_profiler'
38
- ISO3166.configuration.locales = [:es, :de, :en]
39
+ ISO3166.configuration.locales = %i[es de en]
39
40
  # report = MemoryProfiler.report do
40
41
  ISO3166::Data.update_cache
41
42
  # end
@@ -44,7 +45,12 @@ describe ISO3166::Data do
44
45
  ISO3166::Data.update_cache
45
46
 
46
47
  ISO3166.configure do |config|
47
- config.locales = [:af, :am, :ar, :as, :az, :be, :bg, :bn, :br, :bs, :ca, :cs, :cy, :da, :de, :dz, :el, :en, :eo, :es, :et, :eu, :fa, :fi, :fo, :fr, :ga, :gl, :gu, :he, :hi, :hr, :hu, :hy, :ia, :id, :is, :it, :ja, :ka, :kk, :km, :kn, :ko, :ku, :lt, :lv, :mi, :mk, :ml, :mn, :mr, :ms, :mt, :nb, :ne, :nl, :nn, :oc, :or, :pa, :pl, :ps, :pt, :ro, :ru, :rw, :si, :sk, :sl, :so, :sq, :sr, :sv, :sw, :ta, :te, :th, :ti, :tk, :tl, :tr, :tt, :ug, :uk, :ve, :vi, :wa, :wo, :xh, :zh, :zu]
48
+ config.locales = %i[af am ar as az be bg bn br bs ca cs cy da de dz el en
49
+ eo es et eu fa fi fo fr ga gl gu he hi hr hu hy ia id
50
+ is it ja ka kk km kn ko ku lt lv mi mk ml mn mr ms mt
51
+ nb ne nl nn oc or pa pl ps pt ro ru rw si sk sl so sq
52
+ sr sv sw ta te th ti tk tl tr tt ug uk ve vi wa wo xh
53
+ zh zu]
48
54
  end
49
55
  # puts Benchmark.measure {ISO3166::Data.update_cache}
50
56
 
@@ -61,32 +67,32 @@ describe ISO3166::Data do
61
67
 
62
68
  it 'locales will load prior and be cached' do
63
69
  ISO3166.reset
64
- ISO3166.configuration.locales = [:es, :de, :en]
65
- expect(ISO3166::Data.send(:locales_to_load)).to eql(%w(es de en))
70
+ ISO3166.configuration.locales = %i[es de en]
71
+ expect(ISO3166::Data.send(:locales_to_load)).to eql(%w[es de en])
66
72
  ISO3166::Data.update_cache
67
- ISO3166.configuration.locales = [:es, :de, :en]
73
+ ISO3166.configuration.locales = %i[es de en]
68
74
  expect(ISO3166::Data.send(:locales_to_load)).to eql([])
69
75
  end
70
76
 
71
77
  it 'locales will load prior and be cached' do
72
78
  ISO3166.reset
73
- ISO3166.configuration.locales = [:es, :de, :en]
79
+ ISO3166.configuration.locales = %i[es de en]
74
80
  expect(ISO3166::Data.send(:locales_to_remove)).to eql([])
75
81
  expect(ISO3166::Country.new('DE').translation('de')).to eq 'Deutschland'
76
82
  ISO3166::Data.update_cache
77
- ISO3166.configuration.locales = [:es, :en]
83
+ ISO3166.configuration.locales = %i[es en]
78
84
  expect(ISO3166::Data.send(:locales_to_remove)).to eql(['de'])
79
85
  expect(ISO3166::Country.new('DE').translation('de')).to eq nil
80
86
  end
81
87
 
82
88
  describe '#load_cache' do
83
89
  it 'will return an empty hash for an unsupported locale' do
84
- file_array = %w(locales unsupported.json)
90
+ file_array = %w[locales unsupported.json]
85
91
  expect(ISO3166::Data.send(:load_cache, file_array)).to eql({})
86
92
  end
87
93
 
88
94
  it 'will return json for a supported locale' do
89
- file_array = %w(locales en.json)
95
+ file_array = %w[locales en.json]
90
96
  expect(ISO3166::Data.send(:load_cache, file_array)).not_to be_empty
91
97
  end
92
98
  end
@@ -97,8 +103,8 @@ describe ISO3166::Data do
97
103
  alpha2: 'TW',
98
104
  iso_short_name: 'NEW Taiwan',
99
105
  subdivisions: {
100
- CHA: {name: 'New Changhua'},
101
- CYI: {name: 'New Municipality'}
106
+ CHA: { name: 'New Changhua' },
107
+ CYI: { name: 'New Municipality' }
102
108
  },
103
109
  translations: {
104
110
  'en' => 'NEW Taiwan',
@@ -116,8 +122,8 @@ describe ISO3166::Data do
116
122
  expect(subject.iso_short_name).to eq 'NEW Taiwan'
117
123
  expect(subject.translations).to eq('en' => 'NEW Taiwan',
118
124
  'de' => 'NEW Taiwan')
119
- expect(subject.subdivisions).to eq('CHA' => ISO3166::Subdivision.new({name: 'New Changhua', code: 'CHA'}),
120
- 'CYI' => ISO3166::Subdivision.new({name: 'New Municipality', code: 'CYI'}))
125
+ expect(subject.subdivisions).to eq('CHA' => ISO3166::Subdivision.new({ name: 'New Changhua', code: 'CHA' }),
126
+ 'CYI' => ISO3166::Subdivision.new({ name: 'New Municipality', code: 'CYI' }))
121
127
  end
122
128
  end
123
129
 
@@ -127,8 +133,8 @@ describe ISO3166::Data do
127
133
  alpha2: 'LOL',
128
134
  iso_short_name: 'Happy Country',
129
135
  subdivisions: {
130
- LOL1: {name: 'Happy sub1'},
131
- LOL2: {name: 'Happy sub2'}
136
+ LOL1: { name: 'Happy sub1' },
137
+ LOL2: { name: 'Happy sub2' }
132
138
  },
133
139
  translations: {
134
140
  'en' => 'Happy Country',
data/spec/mongoid_spec.rb CHANGED
@@ -1,7 +1,7 @@
1
- # encoding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'spec_helper'
4
- require File.expand_path('../../lib/countries/mongoid', __FILE__)
4
+ require File.expand_path('../lib/countries/mongoid', __dir__)
5
5
 
6
6
  describe 'Mongoid support' do
7
7
  let(:britain) { ISO3166::Country.new('GB') }
data/spec/perf_spec.rb CHANGED
@@ -1,18 +1,18 @@
1
- # encoding: utf-8
2
- require 'spec_helper'
1
+ # frozen_string_literal: true
3
2
 
4
- describe ISO3166::Data, perf: true, order: :defined do
3
+ require 'spec_helper'
5
4
 
6
- ALL_LOCALES = [
7
- :af, :am, :ar, :as, :az, :be, :bg, :bn, :br, :bs, :ca, :cs, :cy, :da, :de,
8
- :dz, :el, :en, :eo, :es, :et, :eu, :fa, :fi, :fo, :fr, :ga, :gl, :gu, :he,
9
- :hi, :hr, :hu, :hy, :ia, :id, :is, :it, :ja, :ka, :kk, :km, :kn, :ko, :ku,
10
- :lt, :lv, :mi, :mk, :ml, :mn, :mr, :ms, :mt, :nb, :ne, :nl, :nn, :oc, :or,
11
- :pa, :pl, :ps, :pt, :ro, :ru, :rw, :si, :sk, :sl, :so, :sq, :sr, :sv, :sw,
12
- :ta, :te, :th, :ti, :tk, :tl, :tr, :tt, :ug, :uk, :ve, :vi, :wa, :wo, :xh,
13
- :zh, :zu
14
- ].freeze
5
+ ALL_LOCALES = %i[
6
+ af am ar as az be bg bn br bs ca cs cy da de
7
+ dz el en eo es et eu fa fi fo fr ga gl gu he
8
+ hi hr hu hy ia id is it ja ka kk km kn ko ku
9
+ lt lv mi mk ml mn mr ms mt nb ne nl nn oc or
10
+ pa pl ps pt ro ru rw si sk sl so sq sr sv sw
11
+ ta te th ti tk tl tr tt ug uk ve vi wa wo xh
12
+ zh zu
13
+ ].freeze
15
14
 
15
+ describe ISO3166::Data, perf: true, order: :defined do
16
16
  def perf_report(_name)
17
17
  require 'benchmark'
18
18
  require 'memory_profiler'
@@ -28,7 +28,7 @@ describe ISO3166::Data, perf: true, order: :defined do
28
28
 
29
29
  # print a flat profile to text
30
30
  printer = RubyProf::FlatPrinter.new(result)
31
- printer.print(STDOUT)
31
+ printer.print($stdout)
32
32
  end
33
33
 
34
34
  it 'responds to call' do
@@ -40,12 +40,12 @@ describe ISO3166::Data, perf: true, order: :defined do
40
40
  end
41
41
 
42
42
  it 'locales will load prior to return results' do
43
- ISO3166.configuration.locales = [:es, :de, :en]
43
+ ISO3166.configuration.locales = %i[es de en]
44
44
  report = MemoryProfiler.report do
45
45
  ISO3166::Data.update_cache
46
46
  end
47
47
 
48
- report.pretty_print(to_file: 'tmp/memory/3_locales')
48
+ report.pretty_print(to_file: 'tmp/3_locales.txt')
49
49
  puts Benchmark.measure { ISO3166::Data.update_cache }
50
50
 
51
51
  ISO3166.configure do |config|
@@ -57,7 +57,7 @@ describe ISO3166::Data, perf: true, order: :defined do
57
57
  ISO3166::Data.update_cache
58
58
  end
59
59
 
60
- report.pretty_print(to_file: 'tmp/memory/all_locales')
60
+ report.pretty_print(to_file: 'tmp/all_locales.txt')
61
61
  end
62
62
 
63
63
  it 'loades a specfic country quickly' do
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'countries'
2
4
  RSpec.configure do |config|
3
5
  config.filter_run :focus