country-codes-rb 0.1.0-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d4bd566540d3cc08d6ef1efb46bd53ed4e90323b026a347d0516932f7b8d4b5c
4
+ data.tar.gz: 24781cceb630629ba7d0ed6d7f507c986d923a6d1d2a297f7e2fd064a2c89190
5
+ SHA512:
6
+ metadata.gz: 3f7850b7a39ae169b92b5b13497a43e15c344311603ec2df386409c854efe49d66840f4eb9fd3c624f2151f4ea3d324a224368df584fdd2de9af02ce38ebc659
7
+ data.tar.gz: 1784d56441f8e80f7a588303a93b2b6e12db1a311b73f9dff7581d122524d6ec74d58e0a143c968767d4dcfbd2d337113df9a7dc3eb150eeea2c5226a05b2945
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
6
+
7
+ gem 'rake', '~> 13.0'
8
+
9
+ gem 'rspec', '~> 3.0'
10
+
11
+ gem 'rubocop', '~> 1.21'
12
+
13
+ gem 'rubocop-rake', '~> 0.6.0'
14
+
15
+ gem 'pry', '~> 0.14.1'
16
+
17
+ gem 'rubocop-rspec', '~> 2.7'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Karl F. Meinkopf
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # CountryCodes.rb
2
+
3
+ A JRuby wrapper for the country-codes library.
4
+
5
+ ## Building
6
+ * Install Bundler
7
+ ```shell
8
+ gem install bundler
9
+ ```
10
+ * Install gem dependencies
11
+ ```shell
12
+ bundle install
13
+ ```
14
+ * Configure an access token for Github Packages Maven repo:
15
+ ```shell
16
+ GITHUB_USER=<username> GITHUB_TOKEN=<token> envsubst < settings.xml.template > settings.xml
17
+ ```
18
+ * Install jar dependencies
19
+ ```shell
20
+ bundle exec rake jars:install
21
+ ```
22
+ * Now you can just build the gem!
23
+ ```shell
24
+ gem build
25
+ ```
26
+
27
+ There's also an option to just run `bin/setup` shell file, which just executes commands listed above (except the last one).
28
+ Note that `$GITHUB_USER` and `$GITUHB_TOKEN` env variables should be set to valid GitHub credentials.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'jars/installer'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ require 'rubocop/rake_task'
10
+
11
+ RuboCop::RakeTask.new
12
+
13
+ namespace :jars do
14
+ desc 'Download jar dependencies and generate a jar-linker file'
15
+ task :install do
16
+ Jars::Installer.vendor_jars!
17
+ end
18
+ end
19
+
20
+ task default: %i[spec rubocop]
@@ -0,0 +1,30 @@
1
+ # this is a generated file, to avoid over-writing it just delete this comment
2
+ begin
3
+ require 'jar_dependencies'
4
+ rescue LoadError
5
+ require 'org/scala-lang/scala-library/2.12.12/scala-library-2.12.12.jar'
6
+ require 'com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.10.5/jackson-dataformat-yaml-2.10.5.jar'
7
+ require 'org/yaml/snakeyaml/1.26/snakeyaml-1.26.jar'
8
+ require 'com/fasterxml/jackson/module/jackson-module-scala_2.12/2.10.5/jackson-module-scala_2.12-2.10.5.jar'
9
+ require 'com/fasterxml/jackson/module/jackson-module-paranamer/2.10.5/jackson-module-paranamer-2.10.5.jar'
10
+ require 'com/fasterxml/jackson/core/jackson-core/2.10.5/jackson-core-2.10.5.jar'
11
+ require 'com/fasterxml/jackson/core/jackson-annotations/2.10.5/jackson-annotations-2.10.5.jar'
12
+ require 'org/scala-lang/scala-reflect/2.12.12/scala-reflect-2.12.12.jar'
13
+ require 'com/fasterxml/jackson/core/jackson-databind/2.10.5/jackson-databind-2.10.5.jar'
14
+ require 'io/wunderschild/country-codes_2.12/0.0.4/country-codes_2.12-0.0.4.jar'
15
+ require 'com/thoughtworks/paranamer/paranamer/2.8/paranamer-2.8.jar'
16
+ end
17
+
18
+ if defined? Jars
19
+ require_jar 'org.scala-lang', 'scala-library', '2.12.12'
20
+ require_jar 'com.fasterxml.jackson.dataformat', 'jackson-dataformat-yaml', '2.10.5'
21
+ require_jar 'org.yaml', 'snakeyaml', '1.26'
22
+ require_jar 'com.fasterxml.jackson.module', 'jackson-module-scala_2.12', '2.10.5'
23
+ require_jar 'com.fasterxml.jackson.module', 'jackson-module-paranamer', '2.10.5'
24
+ require_jar 'com.fasterxml.jackson.core', 'jackson-core', '2.10.5'
25
+ require_jar 'com.fasterxml.jackson.core', 'jackson-annotations', '2.10.5'
26
+ require_jar 'org.scala-lang', 'scala-reflect', '2.12.12'
27
+ require_jar 'com.fasterxml.jackson.core', 'jackson-databind', '2.10.5'
28
+ require_jar 'io.wunderschild', 'country-codes_2.12', '0.0.4'
29
+ require_jar 'com.thoughtworks.paranamer', 'paranamer', '2.8'
30
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'country_codes/internal/helpers'
4
+
5
+ module CountryCodes
6
+ # @!attribute [r] alpha2
7
+ # @return [String] Returns ISO-3166-1 alpha-2 country code
8
+ # @!attribute [r] alpha3
9
+ # @return [String] Returns ISO-3166-1 alpha-3 country code
10
+ # @!attribute [r] nationality
11
+ # @return [String] Returns a human-readable nationality name for this country
12
+ # @!attribute [r] sovereignty
13
+ # @return [String, nil] Return country code for a country that this country is affiliated to (if any).
14
+ # @!attribute [r] official_name
15
+ # @return [String] Returns an official name of this country
16
+ # @!attribute [r] other_names
17
+ # @return [Array<String>] Returns an array of alternative names for this country
18
+ class Country
19
+ include Internal::Helpers
20
+
21
+ # Wraps an instance of `io.wunderschild.country_codes.Country` to be used in Ruby.
22
+ #
23
+ # @note This code is not supposed to be used externally!
24
+ # @param [JavaLangObject] java_country An instance of `io.wunderschild.country_codes.Country`
25
+ def initialize(java_country)
26
+ @country = java_country
27
+ end
28
+
29
+ delegate :alpha2, :alpha3, :official_name, to: :country
30
+ delegate :nationality, :sovereignty, to: :country, interceptors: :unpack_some
31
+ delegate :other_names, to: :country, interceptors: %i[unpack_some unpack_seq array_no_nil]
32
+
33
+ private
34
+
35
+ attr_reader :country
36
+
37
+ def array_no_nil(array)
38
+ array.nil? ? [] : array
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+ require 'country-codes-rb_jars'
5
+
6
+ module CountryCodes
7
+ # @note This code is not supposed to be used externally!
8
+ module Internal
9
+ # @note This code is not supposed to be used externally!
10
+ module Helpers
11
+ java_import 'scala.collection.JavaConverters'
12
+ java_import 'java.util.ArrayList'
13
+
14
+ # rubocop:disable Metrics/CyclomaticComplexity
15
+ def self.included(obj)
16
+ obj.class_eval do
17
+ # Defines a method(s) that redirect calls to an underlying object (receiver).
18
+ # Allows passing a list of interceptors that will be applied sequentially
19
+ # with the result of redirected call as the first (and only) argument.
20
+ # An interceptor can be either a Proc (the proc itself will be `#call`-ed),
21
+ # or a Symbol as a name of an instance method to be used.
22
+ #
23
+ # @param [Array<Symbol>] methods Name of methods to be defined
24
+ # @param [Object] to Name of an instance method used to retrieve the receiver
25
+ # @param [Array<Proc, Symbol>] interceptors A list of interceptors.
26
+ def self.delegate(*methods, to:, interceptors: [])
27
+ unless methods.reject { |m| m.is_a? Symbol }.empty?
28
+ raise ArgumentError, 'expected method reference to be a Symbol!'
29
+ end
30
+
31
+ interceptors = [interceptors] unless interceptors.is_a?(Array)
32
+
33
+ methods.each do |method|
34
+ define_method(method) do |*args|
35
+ result = send(to).send(method, *args)
36
+
37
+ interceptors.reduce(result) do |memo, interceptor|
38
+ case interceptor
39
+ when Proc
40
+ interceptor.call(memo)
41
+ when Symbol
42
+ send(interceptor, memo)
43
+ else
44
+ memo
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ # rubocop:enable Metrics/CyclomaticComplexity
53
+
54
+ # Unpacks a scala.Some Java object to a value or `nil` (if an object is empty).
55
+ #
56
+ # @param [JavaLangObject] some A `scala.Some` object to unpack
57
+ # @return [Object] A value contained in the object, or nil, if the object is empty
58
+ def unpack_some(some)
59
+ if some.empty?
60
+ nil
61
+ else
62
+ some.get
63
+ end
64
+ end
65
+
66
+ # Converts a `scala.collection.Seq` Java object to a Ruby array
67
+ #
68
+ # @param [JavaLangObject] seq An object to convert
69
+ # @return [JavaLangObject] Extracted array
70
+ def unpack_seq(seq)
71
+ JavaConverters.seq_as_java_list(seq).to_a
72
+ end
73
+
74
+ # Converts a given enumerable to a `scala.collection.Seq` Java object
75
+ #
76
+ # @param [Enumerable] enumerable An enumerable to wrap
77
+ # @return [JavaLangObject] A wrapped Seq object
78
+ def pack_seq(enumerable)
79
+ list_iter = ArrayList.new(enumerable.to_a).iterator
80
+
81
+ JavaConverters.as_scala_iterator_converter(list_iter).as_scala.to_seq
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'java'
4
+ require 'country-codes-rb_jars'
5
+
6
+ require 'country_codes/internal/helpers'
7
+ require 'country_codes/country'
8
+
9
+ module CountryCodes
10
+ class LookupTable
11
+ include Internal::Helpers
12
+
13
+ java_import 'io.wunderschild.country_codes.ISOCountryCodes'
14
+
15
+ # @note This code is not supposed to be used externally!
16
+ def initialize(locale, indexed_fields)
17
+ @lookup_table = ISOCountryCodes.apply(locale, pack_seq(indexed_fields))
18
+ end
19
+
20
+ # Searches for a country that matches a given term.
21
+ # If nothing was found, returns `nil`.
22
+ #
23
+ # @param [String] term Search term
24
+ # @return [CountryCodes::Country, nil] Search result
25
+ def find(term)
26
+ packed = @lookup_table.lookup(term)
27
+
28
+ java_country = unpack_some(packed)
29
+
30
+ return nil if java_country.nil?
31
+
32
+ Country.new(java_country)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CountryCodes
4
+ VERSION = '0.1.0'
5
+ LIB_VERSION = '0.0.4'
6
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'country_codes/country'
4
+ require 'country_codes/lookup_table'
5
+ require 'country_codes/version'
6
+
7
+ module CountryCodes
8
+ # A list of allowed search fields (see {.instance}).
9
+ SEARCHABLE_FIELDS = %i[officialName otherNames nationality sovereignty alpha2 alpha3].freeze
10
+
11
+ # Creates an instance of {CountryCodes::LookupTable} to be used for querying.
12
+ #
13
+ # @param [String] locale A locale of expected search terms.
14
+ # @param [Array<Symbol>] search_fields Fields available for searching (see {SEARCHABLE_FIELDS}).
15
+ # @return [LookupTable]
16
+ def self.instance(locale: 'en', search_fields: %i[officialName otherNames nationality])
17
+ unless (search_fields - SEARCHABLE_FIELDS).empty?
18
+ raise ArgumentError, "Unknown search fields: #{search_fields - SEARCHABLE_FIELDS}"
19
+ end
20
+
21
+ CountryCodes::LookupTable.new(locale, search_fields.map(&:to_s))
22
+ end
23
+
24
+ # Searches for a country that matches a given term.
25
+ # If nothing was found, returns `nil`.
26
+ # Check {.instance} for the description of named parameters.
27
+ #
28
+ # @param [String] term Search term
29
+ # @param [String] locale
30
+ # @param [Array<Symbol>] search_fields
31
+ # @return [Country, nil] Search result
32
+ def self.find(term, locale: 'en', search_fields: %i[officialName otherNames nationality])
33
+ instance(locale: locale, search_fields: search_fields).find(term)
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module CountryCodes
2
+ SEARCHABLE_FIELDS: Array[Symbol]
3
+ VERSION: String
4
+ LIB_VERSION: String
5
+
6
+ def self.instance: (?locale: String, ?search_fields: ::Array[Symbol]) -> LookupTable
7
+
8
+ def self.find: (String term, ?locale: String, ?search_fields: ::Array[Symbol]) -> Country?
9
+
10
+ class Country
11
+ attr_reader alpha2: String
12
+
13
+ attr_reader alpha3: String
14
+
15
+ attr_reader nationality: String
16
+
17
+ attr_reader sovereignty: String?
18
+
19
+ attr_reader official_name: String
20
+
21
+ attr_reader other_names: ::Array[String]
22
+ end
23
+
24
+ class LookupTable
25
+ def find: (String term) -> CountryCodes::Country?
26
+ end
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: country-codes-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: java
6
+ authors:
7
+ - Wunderschild
8
+ - Karl F. Meinkopf
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2022-01-22 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ''
15
+ email:
16
+ - k.meinkopf@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - Rakefile
25
+ - lib/com/fasterxml/jackson/core/jackson-annotations/2.10.5/jackson-annotations-2.10.5.jar
26
+ - lib/com/fasterxml/jackson/core/jackson-core/2.10.5/jackson-core-2.10.5.jar
27
+ - lib/com/fasterxml/jackson/core/jackson-databind/2.10.5/jackson-databind-2.10.5.jar
28
+ - lib/com/fasterxml/jackson/dataformat/jackson-dataformat-yaml/2.10.5/jackson-dataformat-yaml-2.10.5.jar
29
+ - lib/com/fasterxml/jackson/module/jackson-module-paranamer/2.10.5/jackson-module-paranamer-2.10.5.jar
30
+ - lib/com/fasterxml/jackson/module/jackson-module-scala_2.12/2.10.5/jackson-module-scala_2.12-2.10.5.jar
31
+ - lib/com/thoughtworks/paranamer/paranamer/2.8/paranamer-2.8.jar
32
+ - lib/country-codes-rb_jars.rb
33
+ - lib/country_codes.rb
34
+ - lib/country_codes/country.rb
35
+ - lib/country_codes/internal/helpers.rb
36
+ - lib/country_codes/lookup_table.rb
37
+ - lib/country_codes/version.rb
38
+ - lib/io/wunderschild/country-codes_2.12/0.0.4/country-codes_2.12-0.0.4.jar
39
+ - lib/org/scala-lang/scala-library/2.12.12/scala-library-2.12.12.jar
40
+ - lib/org/scala-lang/scala-reflect/2.12.12/scala-reflect-2.12.12.jar
41
+ - lib/org/yaml/snakeyaml/1.26/snakeyaml-1.26.jar
42
+ - sig/country_codes.rbs
43
+ homepage: https://github.com/wunderschild/country-codes
44
+ licenses:
45
+ - MIT
46
+ metadata:
47
+ homepage_uri: https://github.com/wunderschild/country-codes
48
+ source_code_uri: https://github.com/wunderschild/country-codes
49
+ github_repo: https://github.com/thedeadferryman/country-codes
50
+ rubygems_mfa_required: 'true'
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: 2.5.0
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements:
66
+ - jar io.wunderschild:country-codes_2.12, 0.0.4
67
+ rubygems_version: 3.2.29
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: A JRuby wrapper for country-codes Scala library
71
+ test_files: []