territorial 0.4.0

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
+ SHA1:
3
+ metadata.gz: e573e03b8e959ce4df6d159fda7c10887774737c
4
+ data.tar.gz: dfadce601cf3df79f5f6657481b7cd600edc935e
5
+ SHA512:
6
+ metadata.gz: 32e958a58c5b4c3f841b3ab6ada2ce46fc32128a72e1c07095bcfbfb4be07ceba6190f61cd31681fcd32694abbdf2d5c5e59a65d769edbb35b7afaa012a39dd3
7
+ data.tar.gz: 15bebdfbf50eff44c8aa8920b48a7a19c87b4da46b5ab0b8a0ba1919dab4fd84e9b41dcb3b6e920445aa162f6dd9cab695417c24c8497718bd94be75059274a7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.2
5
+ - 2.1.6
6
+ - 2.0.0
7
+ - 1.9.3
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in territorial.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 tape.tv AG
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,84 @@
1
+ # Territorial
2
+
3
+ If you have to work with lists of ISO 3166-1 alpha-2 country codes that
4
+ sometimes include fake codes that should be expanded to a longer list, e.g.
5
+ 'EU', or if you have to deal with lists of territory metadata like 'EU -FR'
6
+ then territorial will help. It defines some commonly seen default expansion
7
+ codes, and allows you to correctly expand a string like 'EU -FR' into an
8
+ array of 2-letter country codes
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'territorial'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install territorial
25
+
26
+ ## Usage
27
+
28
+ Basic region expansion is very simple:
29
+
30
+ ```ruby
31
+ Territorial.expand(:GSA)
32
+ # => ['DE', 'CH', 'AT']
33
+
34
+ Territorial.expand(['GSA', EFTA'])
35
+ # => ['DE', 'CH', 'AT', 'NO', 'LI', 'IS']
36
+ ```
37
+
38
+ Available regions:
39
+
40
+ * `GSA` - Germany, Switzerland, Austria
41
+ * `EU` - European Union
42
+ * `EFTA` - European Free Trade Association
43
+ * `WW` - Worldwide
44
+
45
+ You can instantiate a Territorial instance directly with your own extra regions
46
+ defined, or override the defaults:
47
+
48
+ ```ruby
49
+ t = Territorial.new(Anglophone: ['GB', 'US', 'CA', 'AU', 'NZ', 'ZA'])
50
+ t.expand(:Anglophone)
51
+ # => ['GB', 'US', 'CA', 'AU', 'NZ', 'ZA']
52
+ ```
53
+
54
+ You can also parse strings listing territories and regions:
55
+
56
+ ```ruby
57
+ t = Territorial.new
58
+ t.parse('GSA -DE')
59
+ # => ['AT', 'CH']
60
+ ```
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
65
+ `bin/console` for an interactive prompt that will allow you to experiment.
66
+
67
+ To install this gem onto your local machine, run `bundle exec rake install`. To
68
+ release a new version, update the version number in `version.rb`, and then run
69
+ `bundle exec rake release` to create a git tag for the version, push git
70
+ commits and tags, and push the `.gem` file to
71
+ [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( https://github.com/[my-github-username]/territorial/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create a new Pull Request
80
+
81
+
82
+
83
+
84
+
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "territorial"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,144 @@
1
+ require 'territorial/version'
2
+ require 'set'
3
+
4
+ # Expand regional territory shorthand codes to ISO 3166-1 alpha-2 codes and
5
+ # parse lists of string codes like <tt>"EU -FR"</tt>
6
+ class Territorial
7
+ # The default region expansions. EL and UK are European Union variants of the
8
+ # ISO codes GR and GB respectively.
9
+ EXPANSIONS = {
10
+ 'GSA' => %w{DE CH AT},
11
+ 'EU' => %w{
12
+ BE BG CZ DK DE EE IE GR ES FR HR IT CY LV LT LU HU MT NL AT PL PT RO SI
13
+ SK FI SE GB
14
+ },
15
+ 'EFTA' => %w{CH NO LI IS},
16
+ 'WW' => %w{
17
+ AF AX AL DZ AS AD AO AI AQ AG AR AM AW AU AT AZ BS BH BD BB BY BE BZ BJ
18
+ BM BT BO BQ BA BW BV BR IO BN BG BF BI KH CM CA CV KY CF TD CL CN CX CC
19
+ CO KM CG CD CK CR CI HR CU CW CY CZ DK DJ DM DO EC EG SV GQ ER EE ET FK
20
+ FO FJ FI FR GF PF TF GA GM GE DE GH GI GR GL GD GP GU GT GG GN GW GY HT
21
+ HM VA HN HK HU IS IN ID IR IQ IE IM IL IT JM JP JE JO KZ KE KI KP KR KW
22
+ KG LA LV LB LS LR LY LI LT LU MO MK MG MW MY MV ML MT MH MQ MR MU YT MX
23
+ FM MD MC MN ME MS MA MZ MM NA NR NP NL NC NZ NI NE NG NU NF MP NO OM PK
24
+ PW PS PA PG PY PE PH PN PL PT PR QA RE RO RU RW BL SH KN LC MF PM VC WS
25
+ SM ST SA SN RS SC SL SG SX SK SI SB SO ZA GS SS ES LK SD SR SJ SZ SE CH
26
+ SY TW TJ TZ TH TL TG TK TO TT TN TR TM TC TV UG UA AE GB US UM UY UZ VU
27
+ VE VN VG VI WF EH YE ZM ZW
28
+ },
29
+ 'EL' => %w{GR},
30
+ 'UK' => %w{GB}
31
+ }
32
+
33
+ # expand regions from shorthand codes to arrays of ISO codes
34
+ # @return [<String>] the expanded ISO codes
35
+ def self.expand(*regions)
36
+ new.expand(*regions)
37
+ end
38
+
39
+ # @param extra_expansions [Hash] optional Hash of additional expansions
40
+ def initialize(extra_expansions = {}, bounds = [])
41
+ @extra_expansions = Hash[extra_expansions.map { |key, values|
42
+ [normalize_territory(key), normalize_territories(values)]
43
+ }]
44
+ if bounds.empty?
45
+ @bounds = nil
46
+ else
47
+ @bounds = expanded_set(normalize_territories(bounds))
48
+ end
49
+ end
50
+
51
+ # expand regions from shorthand codes to arrays of ISO codes
52
+ # @return [<String>] the expanded ISO codes
53
+ def expand(*regions)
54
+ regions = normalize_territories(regions.flatten)
55
+ expanded_set(regions).to_a
56
+ end
57
+
58
+ # Parse strings like 'EU -GB' or 'EU +RU' to fully expanded arrays of ISO
59
+ # codes. Expandable codes (i.e. EU) will be expanded, codes with a `-` prefix
60
+ # will be removed from the result and codes with a `+` prefix, or with no
61
+ # prefix will be added to the result.
62
+ # @param territory_list [String] the list of territories (and modifiers) as a string
63
+ # @return [<String>] the expanded ISO codes
64
+ def territories(territory_list)
65
+ parser = Parser.new(territory_list)
66
+ allowed = expanded_set(parser.accept)
67
+ rejected = expanded_set(parser.reject)
68
+ (allowed - rejected).to_a
69
+ end
70
+
71
+ private
72
+
73
+ def normalize_territory(territory_string)
74
+ territory_string.to_s.upcase
75
+ end
76
+
77
+ def normalize_territories(territory_strings)
78
+ territory_strings.map { |ts| normalize_territory(ts) }
79
+ end
80
+
81
+ def expansions
82
+ @expansions ||= EXPANSIONS.merge(@extra_expansions)
83
+ end
84
+
85
+ def expansion_regions
86
+ expansions.keys
87
+ end
88
+
89
+ def expandable?(region)
90
+ expansions.has_key?(region.to_s)
91
+ end
92
+
93
+ def constrained(territories)
94
+ return territories if @bounds.nil?
95
+ territories & @bounds
96
+ end
97
+
98
+ def expanded_set(regions)
99
+ territories = Set.new
100
+ already_expanded = Set.new
101
+
102
+ expand_set(territories, regions, already_expanded)
103
+ constrained(territories)
104
+ end
105
+
106
+ def expand_set(territories, regions, already_expanded)
107
+ regions.inject(territories) do |territories, region|
108
+ if expandable?(region) && !already_expanded.include?(region)
109
+ territories.merge(expand_set(territories, expansions[region], already_expanded << region))
110
+ else
111
+ territories << region
112
+ end
113
+ end
114
+ end
115
+
116
+ class Parser
117
+ TOKEN = /([+-]?)([a-z]+)(?:\s|$)/i
118
+ attr_reader :input
119
+
120
+ def initialize(input)
121
+ @input = input
122
+ end
123
+
124
+ def tokens
125
+ @tokens ||= input.scan(TOKEN)
126
+ end
127
+
128
+ def accept
129
+ @accept ||= tokens.reject { |prefix, code|
130
+ prefix == '-'
131
+ }.map { |prefix, code|
132
+ code
133
+ }
134
+ end
135
+
136
+ def reject
137
+ @reject ||= tokens.select { |prefix, code|
138
+ prefix == '-'
139
+ }.map { |prefix, code|
140
+ code
141
+ }
142
+ end
143
+ end
144
+ end
@@ -0,0 +1,3 @@
1
+ class Territorial
2
+ VERSION = "0.4.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'territorial/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "territorial"
8
+ spec.version = Territorial::VERSION
9
+ spec.authors = ["Christof Dorner", "Matt Patterson"]
10
+ spec.email = ["christof@chdorner.com", "matt@reprocessed.org"]
11
+
12
+ spec.summary = %q{Work with lists of country codes and shorthand regions ('EU')}
13
+ spec.description = <<-EOD
14
+ If you have to work with lists of ISO 3166-1 alpha-2 country codes that
15
+ sometimes include fake codes that should be expanded to a longer list, e.g.
16
+ 'EU', or if you have to deal with lists of territory metadata like 'EU -FR'
17
+ then territorial will help. It defines some commonly seen default expansion
18
+ codes, and allows you to correctly expand a string like 'EU -FR' into an
19
+ array of 2-letter country codes
20
+ EOD
21
+ spec.homepage = "https://github.com/tape-tv/territorial"
22
+ spec.license = "MIT"
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.7"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency 'rspec', '~> 3.3.0'
32
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: territorial
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Christof Dorner
8
+ - Matt Patterson
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-08-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.7'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.7'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: 3.3.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 3.3.0
56
+ description: |
57
+ If you have to work with lists of ISO 3166-1 alpha-2 country codes that
58
+ sometimes include fake codes that should be expanded to a longer list, e.g.
59
+ 'EU', or if you have to deal with lists of territory metadata like 'EU -FR'
60
+ then territorial will help. It defines some commonly seen default expansion
61
+ codes, and allows you to correctly expand a string like 'EU -FR' into an
62
+ array of 2-letter country codes
63
+ email:
64
+ - christof@chdorner.com
65
+ - matt@reprocessed.org
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - ".gitignore"
71
+ - ".rspec"
72
+ - ".travis.yml"
73
+ - CODE_OF_CONDUCT.md
74
+ - Gemfile
75
+ - LICENSE.txt
76
+ - README.md
77
+ - Rakefile
78
+ - bin/console
79
+ - bin/setup
80
+ - lib/territorial.rb
81
+ - lib/territorial/version.rb
82
+ - territorial.gemspec
83
+ homepage: https://github.com/tape-tv/territorial
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.5
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Work with lists of country codes and shorthand regions ('EU')
107
+ test_files: []
108
+ has_rdoc: