widgeo 0.0.2
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +10 -0
- data/lib/data/continents.yml +21 -0
- data/lib/data/territories.yml +1017 -0
- data/lib/widgeo.rb +14 -0
- data/lib/widgeo/collection.rb +40 -0
- data/lib/widgeo/continent.rb +14 -0
- data/lib/widgeo/model.rb +21 -0
- data/lib/widgeo/territory.rb +21 -0
- data/lib/widgeo/version.rb +3 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/widgeo/continent_spec.rb +42 -0
- data/spec/widgeo/territory_spec.rb +59 -0
- data/widgeo.gemspec +27 -0
- metadata +107 -0
data/lib/widgeo.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "widgeo/version"
|
2
|
+
require "widgeo/collection"
|
3
|
+
require "widgeo/model"
|
4
|
+
require "widgeo/continent"
|
5
|
+
require "widgeo/territory"
|
6
|
+
|
7
|
+
module Widgeo
|
8
|
+
|
9
|
+
require "yaml"
|
10
|
+
|
11
|
+
class UndefinedDatasetError < StandardError; end
|
12
|
+
class UndefinedFieldError < StandardError; end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Widgeo
|
2
|
+
|
3
|
+
module Collection
|
4
|
+
|
5
|
+
# Set/Get the dataset file name
|
6
|
+
def dataset_file file_name = nil
|
7
|
+
|
8
|
+
@dataset_file ||= file_name
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
# Return the dataset
|
13
|
+
def dataset
|
14
|
+
|
15
|
+
# Raise an exception if the dataset file has not been set on the class
|
16
|
+
raise UndefinedDatasetError unless dataset_file
|
17
|
+
|
18
|
+
@dataset ||= YAML.load_file(
|
19
|
+
File.join(File.dirname(__FILE__), "../", "data", "#{dataset_file}.yml")
|
20
|
+
)
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
# Find an item by an attribute
|
25
|
+
def find_by attribute, value
|
26
|
+
|
27
|
+
all.detect { |continent| continent.send(attribute) == value }
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
# Parse full list and memoize it
|
32
|
+
def all
|
33
|
+
|
34
|
+
@all ||= dataset.map { |item| self.new item }
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/lib/widgeo/model.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Widgeo
|
2
|
+
|
3
|
+
module Model
|
4
|
+
|
5
|
+
# Set attributes on the instance
|
6
|
+
def initialize properties
|
7
|
+
|
8
|
+
properties.each { |name, value|
|
9
|
+
|
10
|
+
# Raise an exception if we don't have a getter for the property
|
11
|
+
raise UndefinedFieldError unless respond_to? name
|
12
|
+
|
13
|
+
instance_variable_set("@#{name}", value)
|
14
|
+
|
15
|
+
}
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Widgeo
|
2
|
+
|
3
|
+
class Territory
|
4
|
+
|
5
|
+
extend Widgeo::Collection
|
6
|
+
include Widgeo::Model
|
7
|
+
|
8
|
+
attr_reader :name, :long_name, :alpha_2, :continent_alpha_2
|
9
|
+
|
10
|
+
dataset_file :territories
|
11
|
+
|
12
|
+
# Load an instance of the continent
|
13
|
+
def continent
|
14
|
+
|
15
|
+
Continent.find_by :alpha_2, continent_alpha_2
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "widgeo"
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Widgeo::Continent do
|
4
|
+
|
5
|
+
CONTINENT_COUNT = 7
|
6
|
+
INVALID_CONTINENT = { bad_field: "BAD FIELD" }
|
7
|
+
CONTINENT_ALPHA = "EU"
|
8
|
+
VALID_CONTINENT = { name: "Beatland", alpha_2: "BE" }
|
9
|
+
CONTINENT_FIELDS = [:name, :alpha_2]
|
10
|
+
|
11
|
+
subject { Widgeo::Continent.new VALID_CONTINENT }
|
12
|
+
|
13
|
+
it { is_expected.to respond_to *CONTINENT_FIELDS }
|
14
|
+
|
15
|
+
context "given an invalid dataset" do
|
16
|
+
|
17
|
+
subject { -> { Widgeo::Continent.new INVALID_CONTINENT } }
|
18
|
+
|
19
|
+
it { is_expected.to raise_error Widgeo::UndefinedFieldError }
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".find_by" do
|
24
|
+
|
25
|
+
subject(:continent) { Widgeo::Continent.find_by :alpha_2, CONTINENT_ALPHA }
|
26
|
+
|
27
|
+
it { expect(continent).to be_kind_of Widgeo::Continent }
|
28
|
+
it { expect(continent.alpha_2).to eq CONTINENT_ALPHA }
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".all" do
|
33
|
+
|
34
|
+
subject(:continents) { Widgeo::Continent.all }
|
35
|
+
|
36
|
+
it { expect(continents).to be_kind_of Array }
|
37
|
+
it { expect(continents.size).to be CONTINENT_COUNT }
|
38
|
+
it { expect(continents.first).to be_kind_of Widgeo::Continent }
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Widgeo::Territory do
|
4
|
+
|
5
|
+
TERRITORY_COUNT = 254
|
6
|
+
INVALID_TERRITORY = { bad_field: "BAD FIELD" }
|
7
|
+
TERRITORY_ALPHA = "GB"
|
8
|
+
PARENT_CONTINENT_ALPHA = "EU"
|
9
|
+
|
10
|
+
VALID_TERRITORY = {
|
11
|
+
name: "Beatland", long_name: "Peoples republic of Beatland",
|
12
|
+
alpha_2: "BE", continent_alpha_2: "EU"
|
13
|
+
}
|
14
|
+
|
15
|
+
TERRITORY_FIELDS = [:name, :long_name, :alpha_2, :continent_alpha_2]
|
16
|
+
|
17
|
+
subject { Widgeo::Territory.new VALID_TERRITORY }
|
18
|
+
|
19
|
+
it { is_expected.to respond_to *TERRITORY_FIELDS }
|
20
|
+
|
21
|
+
context "given an invalid dataset" do
|
22
|
+
|
23
|
+
subject { -> { Widgeo::Territory.new INVALID_TERRITORY } }
|
24
|
+
|
25
|
+
it { is_expected.to raise_error Widgeo::UndefinedFieldError }
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#continent" do
|
30
|
+
|
31
|
+
subject (:continent) {
|
32
|
+
Widgeo::Territory.find_by(:alpha_2, TERRITORY_ALPHA).continent
|
33
|
+
}
|
34
|
+
|
35
|
+
it { expect(continent).to be_kind_of Widgeo::Continent }
|
36
|
+
it { expect(continent.alpha_2).to eq PARENT_CONTINENT_ALPHA }
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
describe ".find_by" do
|
41
|
+
|
42
|
+
subject(:territory) { Widgeo::Territory.find_by :alpha_2, TERRITORY_ALPHA }
|
43
|
+
|
44
|
+
it { expect(territory).to be_kind_of Widgeo::Territory }
|
45
|
+
it { expect(territory.alpha_2).to eq TERRITORY_ALPHA }
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".all" do
|
50
|
+
|
51
|
+
subject(:territories) { Widgeo::Territory.all }
|
52
|
+
|
53
|
+
it { expect(territories).to be_kind_of Array }
|
54
|
+
it { expect(territories.size).to be TERRITORY_COUNT }
|
55
|
+
it { expect(territories.first).to be_kind_of Widgeo::Territory }
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/widgeo.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'widgeo/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
|
8
|
+
spec.name = "widgeo"
|
9
|
+
spec.version = Widgeo::VERSION
|
10
|
+
spec.authors = ["Chris G"]
|
11
|
+
spec.email = ["chris@abstraktion.co.uk"]
|
12
|
+
spec.summary = "Provides easy access to the worlds territories and their continents."
|
13
|
+
spec.description = "Listing and filtering of continents and territories. Includes short names, long names and alpha codes, following the ISO 3166 standard."
|
14
|
+
spec.homepage = "https://github.com/BeatrootApp/Widgeo"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.1"
|
26
|
+
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: widgeo
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris G
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
description: Listing and filtering of continents and territories. Includes short names,
|
56
|
+
long names and alpha codes, following the ISO 3166 standard.
|
57
|
+
email:
|
58
|
+
- chris@abstraktion.co.uk
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/data/continents.yml
|
69
|
+
- lib/data/territories.yml
|
70
|
+
- lib/widgeo.rb
|
71
|
+
- lib/widgeo/collection.rb
|
72
|
+
- lib/widgeo/continent.rb
|
73
|
+
- lib/widgeo/model.rb
|
74
|
+
- lib/widgeo/territory.rb
|
75
|
+
- lib/widgeo/version.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
- spec/widgeo/continent_spec.rb
|
78
|
+
- spec/widgeo/territory_spec.rb
|
79
|
+
- widgeo.gemspec
|
80
|
+
homepage: https://github.com/BeatrootApp/Widgeo
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.2.2
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Provides easy access to the worlds territories and their continents.
|
104
|
+
test_files:
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/widgeo/continent_spec.rb
|
107
|
+
- spec/widgeo/territory_spec.rb
|