govkit-ca 0.0.6 → 0.0.7
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/.travis.yml +4 -0
- data/README.md +1 -1
- data/govkit-ca.gemspec +4 -2
- data/lib/gov_kit-ca/postal_code/strategy/cbc_ca.rb +1 -1
- data/lib/gov_kit-ca/postal_code/strategy/conservative_ca.rb +3 -4
- data/lib/gov_kit-ca/postal_code/strategy/elections_ca.rb +4 -6
- data/lib/gov_kit-ca/postal_code/strategy/liberal_ca.rb +0 -1
- data/lib/gov_kit-ca/postal_code/strategy/parl_gc_ca.rb +5 -1
- data/lib/gov_kit-ca/postal_code.rb +19 -7
- data/lib/gov_kit-ca/represent.rb +108 -0
- data/lib/gov_kit-ca/version.rb +1 -1
- data/lib/gov_kit-ca.rb +2 -1
- data/spec/cbc_ca_spec.rb +14 -25
- data/spec/conservative_ca_spec.rb +1 -10
- data/spec/digital-copyright_ca_spec.rb +0 -9
- data/spec/elections_ca_spec.rb +0 -9
- data/spec/greenparty_ca_spec.rb +0 -9
- data/spec/liberal_ca_spec.rb +1 -10
- data/spec/ndp_ca_spec.rb +0 -9
- data/spec/parl_gc_ca_spec.rb +8 -0
- data/spec/postal_code_spec.rb +69 -87
- data/spec/represent_spec.rb +279 -0
- data/spec/spec_helper.rb +1 -4
- data/spec/strategy_set_spec.rb +55 -2
- data/tasks/tasks.rb +0 -28
- metadata +47 -133
- data/spec/fixtures/cbc_ca/A1A1A1.response +0 -11
- data/spec/fixtures/cbc_ca/G0C2Y0.response +0 -11
- data/spec/fixtures/cbc_ca/H0H0H0.response +0 -11
- data/spec/fixtures/cbc_ca/K0A1K0.response +0 -11
- data/spec/fixtures/cbc_ca/T5S2B9.response +0 -11
- data/spec/fixtures/cbc_ca/X1B1B1.response +0 -11
- data/spec/fixtures/conservative_ca/A1A1A1.response +0 -452
- data/spec/fixtures/conservative_ca/G0C2Y0.response +0 -452
- data/spec/fixtures/conservative_ca/H0H0H0.response +0 -452
- data/spec/fixtures/conservative_ca/K0A1K0.response +0 -455
- data/spec/fixtures/conservative_ca/T5S2B9.response +0 -453
- data/spec/fixtures/conservative_ca/X1B1B1.response +0 -452
- data/spec/fixtures/digital-copyright_ca/A1A1A1.response +0 -227
- data/spec/fixtures/digital-copyright_ca/G0C2Y0.response +0 -229
- data/spec/fixtures/digital-copyright_ca/H0H0H0.response +0 -243
- data/spec/fixtures/digital-copyright_ca/K0A1K0.response +0 -230
- data/spec/fixtures/digital-copyright_ca/T5S2B9.response +0 -233
- data/spec/fixtures/digital-copyright_ca/X1B1B1.response +0 -243
- data/spec/fixtures/elections_ca/A1A1A1.response +0 -10
- data/spec/fixtures/elections_ca/G0C2Y0.response +0 -11
- data/spec/fixtures/elections_ca/H0H0H0.response +0 -10
- data/spec/fixtures/elections_ca/K0A1K0.response +0 -10
- data/spec/fixtures/elections_ca/T5S2B9.response +0 -10
- data/spec/fixtures/elections_ca/X1B1B1.response +0 -10
- data/spec/fixtures/greenparty_ca/A1A1A1.response +0 -13
- data/spec/fixtures/greenparty_ca/G0C2Y0.response +0 -13
- data/spec/fixtures/greenparty_ca/H0H0H0.response +0 -13
- data/spec/fixtures/greenparty_ca/K0A1K0.response +0 -13
- data/spec/fixtures/greenparty_ca/T5S2B9.response +0 -13
- data/spec/fixtures/greenparty_ca/X1B1B1.response +0 -13
- data/spec/fixtures/liberal_ca/A1A1A1.response +0 -702
- data/spec/fixtures/liberal_ca/G0C2Y0.response +0 -570
- data/spec/fixtures/liberal_ca/H0H0H0.response +0 -649
- data/spec/fixtures/liberal_ca/K0A1K0.response +0 -650
- data/spec/fixtures/liberal_ca/T5S2B9.response +0 -570
- data/spec/fixtures/liberal_ca/X1B1B1.response +0 -649
- data/spec/fixtures/ndp_ca/A1A1A1.response +0 -610
- data/spec/fixtures/ndp_ca/G0C2Y0.response +0 -610
- data/spec/fixtures/ndp_ca/H0H0H0.response +0 -593
- data/spec/fixtures/ndp_ca/K0A1K0.response +0 -641
- data/spec/fixtures/ndp_ca/T5S2B9.response +0 -605
- data/spec/fixtures/ndp_ca/X1B1B1.response +0 -593
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e88d9a36dfcf9ce1825f8a204c801d2c3c902ee
|
4
|
+
data.tar.gz: 527b1088c8bffbd00458b6e7855c3e365994fbb6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3cb1d44087529a014496717a00a38435f66aa1db7e02dddc7ecfb3a78d958e2f0f754a449a33329d73123690ed7178d99faf9ea19e4d630dc46c95e08a4f0596
|
7
|
+
data.tar.gz: 9a46ef38c43306f7c733dd8eb105d4e6b03c39a3db644c4b205e26820a3f0e6a60928e87feeb4abd7f6dbc93f4a69402bb1b04fe89e28f19af7e21ce89b3c859
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -50,4 +50,4 @@ GovKit-CA interoperates with [Participatory Politics Foundation](http://www.part
|
|
50
50
|
|
51
51
|
This gem's main repository is on GitHub: [http://github.com/opennorth/govkit-ca](http://github.com/opennorth/govkit-ca), where your contributions, forks, bug reports, feature requests, and feedback are greatly welcomed.
|
52
52
|
|
53
|
-
Copyright (c) 2011 Open North Inc., released under the MIT license
|
53
|
+
Copyright (c) 2011-2013 Open North Inc., released under the MIT license
|
data/govkit-ca.gemspec
CHANGED
@@ -12,17 +12,19 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.description = %q{GovKit-CA lets you quickly get encapsulated Ruby objects for Canadian civic data.}
|
13
13
|
|
14
14
|
s.rubyforge_project = "govkit-ca"
|
15
|
+
s.license = 'MIT'
|
15
16
|
|
16
17
|
s.files = `git ls-files`.split("\n")
|
17
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
20
|
s.require_paths = ["lib"]
|
20
21
|
|
22
|
+
s.add_runtime_dependency('faraday')
|
21
23
|
s.add_runtime_dependency('httparty', '~> 0.10.0')
|
22
24
|
s.add_runtime_dependency('nokogiri', '~> 1.5.0')
|
23
|
-
s.
|
25
|
+
s.add_development_dependency('json')
|
24
26
|
s.add_development_dependency('rspec', '~> 2.6.0')
|
25
|
-
s.add_development_dependency('fakeweb', '~> 1.3.0')
|
26
27
|
s.add_development_dependency('rake')
|
27
28
|
s.add_development_dependency('coveralls')
|
29
|
+
s.add_development_dependency('mime-types', '~> 1.16')
|
28
30
|
end
|
@@ -10,7 +10,7 @@ module GovKit
|
|
10
10
|
private
|
11
11
|
|
12
12
|
def electoral_districts!
|
13
|
-
|
13
|
+
JSON.load(response.parsed_response).map{|x| self.class.rid_to_edid[x['rid'].to_i]}
|
14
14
|
end
|
15
15
|
|
16
16
|
def valid?
|
@@ -4,9 +4,8 @@ module GovKit
|
|
4
4
|
module Strategy
|
5
5
|
class ConservativeCa < Base
|
6
6
|
base_uri 'www.conservative.ca'
|
7
|
-
http_method :
|
8
|
-
path '
|
9
|
-
post_data 'findmymp=35&pc=<%= @postal_code %>'
|
7
|
+
http_method :get
|
8
|
+
path '/wp-content/themes/conservative/functions-process.php?x=vldpc&fpc=<%= @postal_code %>'
|
10
9
|
|
11
10
|
private
|
12
11
|
|
@@ -19,7 +18,7 @@ module GovKit
|
|
19
18
|
end
|
20
19
|
|
21
20
|
def images
|
22
|
-
@
|
21
|
+
@images ||= Nokogiri::HTML(response.parsed_response, nil, 'utf-8').css('img')
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
@@ -2,13 +2,11 @@ module GovKit
|
|
2
2
|
module CA
|
3
3
|
module PostalCode
|
4
4
|
module Strategy
|
5
|
-
# elections.ca is a reliable source, but it does not return electoral
|
6
|
-
# districts for postal codes that contain multiple electoral districts.
|
7
|
-
# @see https://github.com/danielharan/postal_code_to_edid_webservice
|
8
5
|
class ElectionsCa < Base
|
9
6
|
base_uri 'elections.ca'
|
10
|
-
http_method :
|
11
|
-
path '/
|
7
|
+
http_method :post
|
8
|
+
path '/Scripts/vis/FindED'
|
9
|
+
post_data 'CommonSearchTxt=<%= @postal_code %>'
|
12
10
|
|
13
11
|
private
|
14
12
|
|
@@ -17,7 +15,7 @@ module GovKit
|
|
17
15
|
end
|
18
16
|
|
19
17
|
def valid?
|
20
|
-
|
18
|
+
!response.headers['location'][/EDNotFound|MultipleEDs/]
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
@@ -79,14 +79,26 @@ end
|
|
79
79
|
|
80
80
|
require 'gov_kit-ca/postal_code/strategy_set'
|
81
81
|
require 'gov_kit-ca/postal_code/strategy/base'
|
82
|
-
|
83
|
-
|
82
|
+
|
83
|
+
# Only riding name and not implemented.
|
84
|
+
# require 'gov_kit-ca/postal_code/strategy/parl_gc_ca'
|
85
|
+
|
86
|
+
# Only one-to-one.
|
87
|
+
require 'gov_kit-ca/postal_code/strategy/elections_ca'
|
88
|
+
|
84
89
|
require 'gov_kit-ca/postal_code/strategy/ndp_ca'
|
85
|
-
require 'gov_kit-ca/postal_code/strategy/liberal_ca' # incomplete database
|
86
|
-
require 'gov_kit-ca/postal_code/strategy/greenparty_ca' # only one-to-one
|
87
|
-
require 'gov_kit-ca/postal_code/strategy/cbc_ca' # disagrees with elections.ca
|
88
90
|
|
89
|
-
#
|
91
|
+
# Only one-to-one.
|
92
|
+
require 'gov_kit-ca/postal_code/strategy/greenparty_ca'
|
93
|
+
|
94
|
+
# Only riding name if not held by a Conservative MP.
|
90
95
|
# require 'gov_kit-ca/postal_code/strategy/conservative_ca'
|
91
|
-
|
96
|
+
|
97
|
+
# 2014-02-12: Occasionally renders HTML instead of redirect.
|
98
|
+
# require 'gov_kit-ca/postal_code/strategy/liberal_ca'
|
99
|
+
|
100
|
+
# 2014-02-12: Broken until next election.
|
101
|
+
# require 'gov_kit-ca/postal_code/strategy/cbc_ca'
|
102
|
+
|
103
|
+
# Too many or too few ridings.
|
92
104
|
# require 'gov_kit-ca/postal_code/strategy/digital-copyright_ca'
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module GovKit
|
2
|
+
module CA
|
3
|
+
# A Ruby wrapper for the Represent API.
|
4
|
+
# @see http://represent.opennorth.ca/api/
|
5
|
+
class Represent
|
6
|
+
# Get boundary sets.
|
7
|
+
#
|
8
|
+
# @param [Hash] opts optional arguments
|
9
|
+
# @option opts [String] :boundary_set a boundary set
|
10
|
+
# @option opts [Integer] :limit
|
11
|
+
# @option opts [Integer] :offset
|
12
|
+
# @see http://represent.opennorth.ca/api/#boundaryset
|
13
|
+
def boundary_sets(opts = {})
|
14
|
+
request(['boundary-sets', opts.delete(:boundary_set)], opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get boundaries.
|
18
|
+
#
|
19
|
+
# @param [Hash] opts optional arguments
|
20
|
+
# @option opts [String] :boundary_set a boundary set
|
21
|
+
# @option opts [String] :boundary a boundary
|
22
|
+
# @option opts [Boolean] :representatives
|
23
|
+
# @option opts [Array<String>,String] :sets comma-separated list of boundary sets
|
24
|
+
# @option opts [Array<Float>,String] :contains comma-separated latitude and longitude
|
25
|
+
# @option opts [String] :touches a boundary
|
26
|
+
# @option opts [String] :intersects a boundary
|
27
|
+
# @option opts [Integer] :limit
|
28
|
+
# @option opts [Integer] :offset
|
29
|
+
# @see http://represent.opennorth.ca/api/#boundary
|
30
|
+
def boundaries(opts = {})
|
31
|
+
if Array === opts[:sets]
|
32
|
+
opts[:sets] = opts[:sets].join(',')
|
33
|
+
end
|
34
|
+
if Array === opts[:contains]
|
35
|
+
opts[:contains] = opts[:contains].join(',')
|
36
|
+
end
|
37
|
+
if opts.has_key?(:boundary) && !opts.has_key?(:boundary_set)
|
38
|
+
raise ArgumentError, ':boundary_set must be set if :boundary is set'
|
39
|
+
end
|
40
|
+
if opts[:representatives] && !(opts.has_key?(:boundary) && opts.has_key?(:boundary_set))
|
41
|
+
raise ArgumentError, ':boundary_set and :boundary must be set if :representatives is true'
|
42
|
+
end
|
43
|
+
request(['boundaries', opts.delete(:boundary_set), opts.delete(:boundary), opts.delete(:representatives) && 'representatives'], opts)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Get boundaries and representatives by postal code.
|
47
|
+
#
|
48
|
+
# @param [Hash] opts optional arguments
|
49
|
+
# @option opts [Array<String>,String] :sets comma-separated list of boundary sets
|
50
|
+
# @option opts [Integer] :limit
|
51
|
+
# @option opts [Integer] :offset
|
52
|
+
# @see http://represent.opennorth.ca/api/#postcode
|
53
|
+
def postcodes(postcode, opts = {})
|
54
|
+
if Array === opts[:sets]
|
55
|
+
opts[:sets] = opts[:sets].join(',')
|
56
|
+
end
|
57
|
+
request(['postcodes', postcode], opts)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Get representative sets.
|
61
|
+
#
|
62
|
+
# @param [Hash] opts optional arguments
|
63
|
+
# @option opts [String] :representative_set a representative set
|
64
|
+
# @option opts [Integer] :limit
|
65
|
+
# @option opts [Integer] :offset
|
66
|
+
# @see http://represent.opennorth.ca/api/#representativeset
|
67
|
+
def representative_sets(opts = {})
|
68
|
+
request ['representative-sets', opts.delete(:representative_set)], opts
|
69
|
+
end
|
70
|
+
|
71
|
+
# Get representatives.
|
72
|
+
#
|
73
|
+
# @param [Hash] opts optional arguments
|
74
|
+
# @option opts [String] :representative_set a representative set
|
75
|
+
# @option opts [Array<Float>,String] :point a comma-separated latitude and longitude
|
76
|
+
# @option opts [Array<Strong>,String] :districts a comma-separated list of boundaries
|
77
|
+
# @option opts [Integer] :limit
|
78
|
+
# @option opts [Integer] :offset
|
79
|
+
# @see http://represent.opennorth.ca/api/#representative
|
80
|
+
def representatives(opts = {})
|
81
|
+
if Array === opts[:point]
|
82
|
+
opts[:point] = opts[:point].join(',')
|
83
|
+
end
|
84
|
+
if Array === opts[:districts]
|
85
|
+
opts[:districts] = opts[:districts].join(',')
|
86
|
+
end
|
87
|
+
request(['representatives', opts.delete(:representative_set)], opts)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def request(parts, opts)
|
93
|
+
begin
|
94
|
+
url = "http://represent.opennorth.ca/#{parts.compact.join('/')}/"
|
95
|
+
response = Faraday.get(url, opts)
|
96
|
+
case response.status
|
97
|
+
when 200
|
98
|
+
JSON.parse(response.body)
|
99
|
+
when 404
|
100
|
+
raise ResourceNotFound, "#{response.status} #{url}?#{opts.map{|k,v| "#{k}=#{v}"}.join('&')}"
|
101
|
+
else
|
102
|
+
raise InvalidRequest, "#{response.status} #{url}?#{opts.map{|k,v| "#{k}=#{v}"}.join('&')} #{response.body}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/lib/gov_kit-ca/version.rb
CHANGED
data/lib/gov_kit-ca.rb
CHANGED
@@ -2,9 +2,9 @@ require 'erb'
|
|
2
2
|
require 'json'
|
3
3
|
require 'yaml'
|
4
4
|
|
5
|
+
require 'faraday'
|
5
6
|
require 'httparty'
|
6
7
|
require 'nokogiri'
|
7
|
-
require 'yajl'
|
8
8
|
|
9
9
|
module GovKit
|
10
10
|
module CA
|
@@ -15,3 +15,4 @@ module GovKit
|
|
15
15
|
end
|
16
16
|
|
17
17
|
require 'gov_kit-ca/postal_code'
|
18
|
+
require 'gov_kit-ca/represent'
|
data/spec/cbc_ca_spec.rb
CHANGED
@@ -1,34 +1,23 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
require 'gov_kit-ca/postal_code/strategy/cbc_ca'
|
3
3
|
|
4
|
-
|
5
|
-
describe
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'should return the electoral districts within a postal code' do
|
17
|
-
{ 'G0C2Y0' => [24019],
|
18
|
-
'T5S2B9' => [48015, 48017],
|
19
|
-
'K0A1K0' => [35025, 35052, 35063, 35064],
|
20
|
-
}.each do |postal_code,electoral_districts|
|
21
|
-
GovKit::CA::PostalCode::Strategy::CBCCa.new(postal_code).electoral_districts.should == electoral_districts
|
22
|
-
end
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::CBCCa do
|
5
|
+
describe '#electoral_districts', :broken => true do
|
6
|
+
it 'should return the electoral districts within a postal code' do
|
7
|
+
{ 'G0C2Y0' => [24019],
|
8
|
+
'T5S2B9' => [48015, 48017],
|
9
|
+
'K0A1K0' => [35025, 35052, 35063, 35064],
|
10
|
+
}.each do |postal_code,electoral_districts|
|
11
|
+
GovKit::CA::PostalCode::Strategy::CBCCa.new(postal_code).electoral_districts.should == electoral_districts
|
23
12
|
end
|
13
|
+
end
|
24
14
|
|
25
|
-
|
26
|
-
|
27
|
-
|
15
|
+
it 'should return false if a postal code contains no electoral districts' do
|
16
|
+
GovKit::CA::PostalCode::Strategy::CBCCa.new('H0H0H0').electoral_districts.should be_false
|
17
|
+
end
|
28
18
|
|
29
|
-
|
30
|
-
|
31
|
-
end
|
19
|
+
it 'should return false if a postal code does not exist' do
|
20
|
+
GovKit::CA::PostalCode::Strategy::CBCCa.new('X1B1B1').electoral_districts.should be_false
|
32
21
|
end
|
33
22
|
end
|
34
23
|
end
|
@@ -3,17 +3,8 @@ require 'gov_kit-ca/postal_code/strategy/conservative_ca'
|
|
3
3
|
|
4
4
|
describe GovKit::CA::PostalCode::Strategy::ConservativeCa do
|
5
5
|
describe '#electoral_districts' do
|
6
|
-
before :all do
|
7
|
-
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
8
|
-
strategy = GovKit::CA::PostalCode::Strategy::ConservativeCa.new(postal_code)
|
9
|
-
unless FakeWeb.allow_net_connect?
|
10
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('digital-copyright_ca', "#{postal_code}.response")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
6
|
it 'should return the electoral districts within a postal code' do
|
16
|
-
{ '
|
7
|
+
{ 'T1P1K1' => [48010],
|
17
8
|
'K0A1K0' => [35025, 35052, 35063],
|
18
9
|
# returns nothing for B0J2L0
|
19
10
|
}.each do |postal_code,electoral_districts|
|
@@ -3,15 +3,6 @@ require 'gov_kit-ca/postal_code/strategy/digital-copyright_ca'
|
|
3
3
|
|
4
4
|
describe GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa do
|
5
5
|
describe '#electoral_districts' do
|
6
|
-
before :all do
|
7
|
-
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
8
|
-
strategy = GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new(postal_code)
|
9
|
-
unless FakeWeb.allow_net_connect?
|
10
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('digital-copyright_ca', "#{postal_code}.response")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
6
|
it 'should return the electoral districts within a postal code' do
|
16
7
|
{ 'G0C2Y0' => [24019, 24039], # differs from all
|
17
8
|
'T5S2B9' => [48012, 48013, 48014, 48015, 48017, 48018], # differs from all
|
data/spec/elections_ca_spec.rb
CHANGED
@@ -3,15 +3,6 @@ require 'gov_kit-ca/postal_code/strategy/elections_ca'
|
|
3
3
|
|
4
4
|
describe GovKit::CA::PostalCode::Strategy::ElectionsCa do
|
5
5
|
describe '#electoral_districts' do
|
6
|
-
before :all do
|
7
|
-
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
8
|
-
strategy = GovKit::CA::PostalCode::Strategy::ElectionsCa.new(postal_code)
|
9
|
-
unless FakeWeb.allow_net_connect?
|
10
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('elections_ca', "#{postal_code}.response")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
6
|
it 'should return the electoral districts within a postal code' do
|
16
7
|
{ 'G0C2Y0' => [24019],
|
17
8
|
# returns nothing for T5S2B9
|
data/spec/greenparty_ca_spec.rb
CHANGED
@@ -3,15 +3,6 @@ require 'gov_kit-ca/postal_code/strategy/greenparty_ca'
|
|
3
3
|
|
4
4
|
describe GovKit::CA::PostalCode::Strategy::GreenPartyCa do
|
5
5
|
describe '#electoral_districts' do
|
6
|
-
before :all do
|
7
|
-
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
8
|
-
strategy = GovKit::CA::PostalCode::Strategy::GreenPartyCa.new(postal_code)
|
9
|
-
unless FakeWeb.allow_net_connect?
|
10
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('greenparty_ca', "#{postal_code}.response")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
6
|
it 'should return the electoral districts within a postal code' do
|
16
7
|
{ 'G0C2Y0' => [24019],
|
17
8
|
'T5S2B9' => [48015],
|
data/spec/liberal_ca_spec.rb
CHANGED
@@ -2,16 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
require 'gov_kit-ca/postal_code/strategy/liberal_ca'
|
3
3
|
|
4
4
|
describe GovKit::CA::PostalCode::Strategy::LiberalCa do
|
5
|
-
describe '#electoral_districts' do
|
6
|
-
before :all do
|
7
|
-
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
8
|
-
strategy = GovKit::CA::PostalCode::Strategy::LiberalCa.new(postal_code)
|
9
|
-
unless FakeWeb.allow_net_connect?
|
10
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('liberal_ca', "#{postal_code}.response")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
5
|
+
describe '#electoral_districts', :broken => true do
|
15
6
|
it 'should return the electoral districts within a postal code' do
|
16
7
|
{ 'G0C2Y0' => [24019],
|
17
8
|
'T5S2B9' => [48015],
|
data/spec/ndp_ca_spec.rb
CHANGED
@@ -3,15 +3,6 @@ require 'gov_kit-ca/postal_code/strategy/ndp_ca'
|
|
3
3
|
|
4
4
|
describe GovKit::CA::PostalCode::Strategy::NDPCa do
|
5
5
|
describe '#electoral_districts' do
|
6
|
-
before :all do
|
7
|
-
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
8
|
-
strategy = GovKit::CA::PostalCode::Strategy::NDPCa.new(postal_code)
|
9
|
-
unless FakeWeb.allow_net_connect?
|
10
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('ndp_ca', "#{postal_code}.response")
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
6
|
it 'should return the electoral districts within a postal code' do
|
16
7
|
{ 'G0C2Y0' => [24019],
|
17
8
|
'T5S2B9' => [48015],
|
data/spec/postal_code_spec.rb
CHANGED
@@ -1,106 +1,88 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
|
4
|
-
describe
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
subject.valid?(postal_code).should be_false
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'should return true if the postal code is properly formatted' do
|
22
|
-
[ 'A1Z1Z1', # Z not as first letter
|
23
|
-
'H0H0H0', # doesn't exist
|
24
|
-
'A1A1A1', # does exist
|
25
|
-
].each do |postal_code|
|
26
|
-
subject.valid?(postal_code).should be_true
|
27
|
-
end
|
3
|
+
describe GovKit::CA::PostalCode do
|
4
|
+
describe '#valid?' do
|
5
|
+
it 'should return false if the postal code is not properly formatted' do
|
6
|
+
[ 'A1A1A', # too short
|
7
|
+
'A1A1A1A', # too long
|
8
|
+
"A1A1A1\nA1A1A1", # multiline
|
9
|
+
'AAAAAA', # no digits
|
10
|
+
'111111', # no letters
|
11
|
+
'1A1A1A', # wrong order
|
12
|
+
'Z1Z1Z1', # Z as first letter
|
13
|
+
'Q1Q1Q1', # Q as letter
|
14
|
+
'a1a1a1', # lowercase
|
15
|
+
].each do |postal_code|
|
16
|
+
subject.valid?(postal_code).should be_false
|
28
17
|
end
|
29
18
|
end
|
30
19
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
#'GreenPartyCa' => 'greenparty_ca',
|
38
|
-
}.each do |const,path|
|
39
|
-
%w(A1A1A1 K0A1K0 H0H0H0).each do |postal_code|
|
40
|
-
strategy = GovKit::CA::PostalCode::Strategy.const_get(const).new(postal_code)
|
41
|
-
unless FakeWeb.allow_net_connect?
|
42
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path(path, "#{postal_code}.response")
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
20
|
+
it 'should return true if the postal code is properly formatted' do
|
21
|
+
[ 'A1Z1Z1', # Z not as first letter
|
22
|
+
'H0H0H0', # doesn't exist
|
23
|
+
'A1A1A1', # does exist
|
24
|
+
].each do |postal_code|
|
25
|
+
subject.valid?(postal_code).should be_true
|
46
26
|
end
|
27
|
+
end
|
28
|
+
end
|
47
29
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
30
|
+
describe '#find_electoral_districts_by_postal_code' do
|
31
|
+
it 'should return the electoral districts within a postal code' do
|
32
|
+
{ 'A1A1A1' => [10007],
|
33
|
+
'K0A1K0' => [35012, 35025, 35040, 35052],
|
34
|
+
}.each do |postal_code,electoral_districts|
|
35
|
+
subject.find_electoral_districts_by_postal_code(postal_code).should == electoral_districts
|
54
36
|
end
|
37
|
+
end
|
55
38
|
|
56
|
-
|
57
|
-
|
58
|
-
|
39
|
+
it 'should raise an error if the postal code cannot be determined' do
|
40
|
+
lambda{subject.find_electoral_districts_by_postal_code('H0H0H0')}.should raise_error(GovKit::CA::ResourceNotFound)
|
41
|
+
end
|
59
42
|
|
60
|
-
|
61
|
-
|
62
|
-
end
|
43
|
+
it 'should raise an error if the postal code is invalid' do
|
44
|
+
lambda{subject.find_electoral_districts_by_postal_code('AAAAAA')}.should raise_error(GovKit::CA::InvalidRequest)
|
63
45
|
end
|
46
|
+
end
|
64
47
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
end
|
48
|
+
describe '#find_province_by_postal_code' do
|
49
|
+
it 'should return the province that a postal code belongs to' do
|
50
|
+
{ 'A' => 'Newfoundland and Labrador',
|
51
|
+
'B' => 'Nova Scotia',
|
52
|
+
'C' => 'Prince Edward Island',
|
53
|
+
'E' => 'New Brunswick',
|
54
|
+
'G' => 'Quebec',
|
55
|
+
'H' => 'Quebec',
|
56
|
+
'J' => 'Quebec',
|
57
|
+
'K' => 'Ontario',
|
58
|
+
'L' => 'Ontario',
|
59
|
+
'M' => 'Ontario',
|
60
|
+
'N' => 'Ontario',
|
61
|
+
'P' => 'Ontario',
|
62
|
+
'R' => 'Manitoba',
|
63
|
+
'S' => 'Saskatchewan',
|
64
|
+
'T' => 'Alberta',
|
65
|
+
'V' => 'British Columbia',
|
66
|
+
'X0A' => 'Nunavut',
|
67
|
+
'X0B' => 'Nunavut',
|
68
|
+
'X0C' => 'Nunavut',
|
69
|
+
'X0E' => 'Northwest Territories',
|
70
|
+
'X0G' => 'Northwest Territories',
|
71
|
+
'X1A' => 'Northwest Territories',
|
72
|
+
'Y' => 'Yukon',
|
73
|
+
}.each do |postal_code, province|
|
74
|
+
subject.find_province_by_postal_code(postal_code).should == province
|
93
75
|
end
|
76
|
+
end
|
94
77
|
|
95
|
-
|
96
|
-
|
97
|
-
end
|
78
|
+
it 'should raise an error if the province cannot be determined' do
|
79
|
+
lambda{subject.find_province_by_postal_code('X1B1B1')}.should raise_error(GovKit::CA::ResourceNotFound)
|
98
80
|
end
|
81
|
+
end
|
99
82
|
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
83
|
+
describe '#format_postal_code' do
|
84
|
+
it 'should format a postal code' do
|
85
|
+
subject.format_postal_code("+a1a 1a1\n").should == 'A1A1A1'
|
104
86
|
end
|
105
87
|
end
|
106
88
|
end
|