govkit-ca 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +3 -0
- data/README.md +5 -1
- data/govkit-ca.gemspec +1 -0
- data/lib/gov_kit-ca/postal_code/strategy/base.rb +20 -1
- data/lib/gov_kit-ca/postal_code/strategy/cbc_ca.rb +1 -3
- data/lib/gov_kit-ca/postal_code/strategy/conservative_ca.rb +9 -6
- data/lib/gov_kit-ca/postal_code/strategy/digital-copyright_ca.rb +0 -3
- data/lib/gov_kit-ca/postal_code/strategy/greenparty_ca.rb +3 -6
- data/lib/gov_kit-ca/postal_code/strategy/ndp_ca.rb +4 -6
- data/lib/gov_kit-ca/postal_code/strategy/parl_gc_ca.rb +0 -1
- data/lib/gov_kit-ca/postal_code.rb +7 -5
- data/lib/gov_kit-ca/version.rb +1 -1
- data/lib/gov_kit-ca.rb +0 -2
- data/spec/cbc_ca_spec.rb +2 -2
- data/spec/conservative_ca_spec.ca +36 -0
- data/spec/digital-copyright_ca_spec.rb +21 -22
- data/spec/elections_ca_spec.rb +23 -24
- data/spec/greenparty_ca_spec.rb +23 -24
- data/spec/liberal_ca_spec.rb +24 -22
- data/spec/ndp_ca_spec.rb +21 -24
- data/spec/postal_code_spec.rb +3 -2
- metadata +58 -109
- 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 -467
- data/spec/fixtures/conservative_ca/G0C2Y0.response +0 -467
- data/spec/fixtures/conservative_ca/H0H0H0.response +0 -467
- data/spec/fixtures/conservative_ca/K0A1K0.response +0 -467
- data/spec/fixtures/conservative_ca/T5S2B9.response +0 -467
- data/spec/fixtures/conservative_ca/X1B1B1.response +0 -467
- data/spec/fixtures/digital-copyright_ca/A1A1A1.response +0 -262
- data/spec/fixtures/digital-copyright_ca/G0C2Y0.response +0 -263
- data/spec/fixtures/digital-copyright_ca/H0H0H0.response +0 -278
- data/spec/fixtures/digital-copyright_ca/K0A1K0.response +0 -264
- data/spec/fixtures/digital-copyright_ca/T5S2B9.response +0 -267
- data/spec/fixtures/digital-copyright_ca/X1B1B1.response +0 -278
- data/spec/fixtures/elections_ca/A1A1A1.response +0 -12
- data/spec/fixtures/elections_ca/G0C2Y0.response +0 -12
- data/spec/fixtures/elections_ca/H0H0H0.response +0 -11
- data/spec/fixtures/elections_ca/K0A1K0.response +0 -11
- data/spec/fixtures/elections_ca/T5S2B9.response +0 -12
- data/spec/fixtures/elections_ca/X1B1B1.response +0 -11
- 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 -582
- data/spec/fixtures/liberal_ca/G0C2Y0.response +0 -582
- data/spec/fixtures/liberal_ca/H0H0H0.response +0 -663
- data/spec/fixtures/liberal_ca/K0A1K0.response +0 -663
- data/spec/fixtures/liberal_ca/T5S2B9.response +0 -663
- data/spec/fixtures/liberal_ca/X1B1B1.response +0 -663
- data/spec/fixtures/ndp_ca/A1A1A1.response +0 -12
- data/spec/fixtures/ndp_ca/G0C2Y0.response +0 -12
- data/spec/fixtures/ndp_ca/H0H0H0.response +0 -12
- data/spec/fixtures/ndp_ca/K0A1K0.response +0 -12
- data/spec/fixtures/ndp_ca/T5S2B9.response +0 -12
- data/spec/fixtures/ndp_ca/X1B1B1.response +0 -12
- data/spec/fixtures/parl_gc_ca/A1A1A1.response +0 -337
- data/spec/fixtures/parl_gc_ca/G0C2Y0.response +0 -337
- data/spec/fixtures/parl_gc_ca/H0H0H0.response +0 -226
- data/spec/fixtures/parl_gc_ca/K0A1K0.response +0 -784
- data/spec/fixtures/parl_gc_ca/T5S2B9.response +0 -511
- data/spec/fixtures/parl_gc_ca/X1B1B1.response +0 -226
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# GovKit-CA
|
2
2
|
|
3
|
+
[![Build Status](https://secure.travis-ci.org/opennorth/govkit-ca.png)](http://travis-ci.org/opennorth/govkit-ca)
|
4
|
+
[![Dependency Status](https://gemnasium.com/opennorth/govkit-ca.png)](https://gemnasium.com/opennorth/govkit-ca)
|
5
|
+
[![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/opennorth/govkit-ca)
|
6
|
+
|
3
7
|
GovKit-CA is a Ruby gem that provides easy access to Canadian civic data around the web. It currently provides an API for free postal code to electoral district lookups, using the following sources:
|
4
8
|
|
5
9
|
* [elections.ca](http://elections.ca/)
|
@@ -43,6 +47,6 @@ GovKit-CA will raise GovKit::CA::ResourceNotFound if the electoral districts wit
|
|
43
47
|
|
44
48
|
GovKit-CA interoperates with [Participatory Politics Foundation](http://www.participatorypolitics.org/)'s [GovKit](https://github.com/opengovernment/govkit). Please join the [GovKit Google Group](http://groups.google.com/group/govkit), especially if you'd like to talk about a new feature and get announcements.
|
45
49
|
|
46
|
-
|
50
|
+
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.
|
47
51
|
|
48
52
|
Copyright (c) 2011 Open North Inc., released under the MIT license
|
data/govkit-ca.gemspec
CHANGED
@@ -47,7 +47,13 @@ module GovKit
|
|
47
47
|
# Performs the request and returns the response.
|
48
48
|
# @return [HTTParty::Response] a HTTParty response object
|
49
49
|
def response
|
50
|
-
@response ||=
|
50
|
+
@response ||= begin
|
51
|
+
if self.class.http_method == :post
|
52
|
+
self.class.post path, :body => post_data
|
53
|
+
else
|
54
|
+
self.class.send self.class.http_method, path
|
55
|
+
end
|
56
|
+
end
|
51
57
|
end
|
52
58
|
|
53
59
|
# Allows setting an HTTP method to be used for each request.
|
@@ -70,6 +76,19 @@ module GovKit
|
|
70
76
|
def path
|
71
77
|
ERB.new(self.class.path).result binding
|
72
78
|
end
|
79
|
+
|
80
|
+
# Allows setting POST data to be sent in each request.
|
81
|
+
# @param [String] post_data an ERB template for the POST data
|
82
|
+
# @return [String] the POST data for the request
|
83
|
+
def self.post_data(post_data = nil)
|
84
|
+
return default_options[:post_data] unless post_data
|
85
|
+
default_options[:post_data] = post_data
|
86
|
+
end
|
87
|
+
|
88
|
+
# @return [String] the POST data for the request
|
89
|
+
def post_data
|
90
|
+
ERB.new(self.class.post_data).result binding
|
91
|
+
end
|
73
92
|
end
|
74
93
|
end
|
75
94
|
end
|
@@ -2,15 +2,13 @@ module GovKit
|
|
2
2
|
module CA
|
3
3
|
module PostalCode
|
4
4
|
module Strategy
|
5
|
-
# cbc.ca ought to be a reliable source. It is unknown if its database
|
6
|
-
# is kept up-to-date between elections, however.
|
7
|
-
# @see https://github.com/danielharan/pc_scraper
|
8
5
|
class CBCCa < Base
|
9
6
|
base_uri 'www.cbc.ca'
|
10
7
|
http_method :get
|
11
8
|
path '/news/politics/canadavotes2011/myelection/postalcodes/index.php?pc=<%= @postal_code %>'
|
12
9
|
|
13
10
|
private
|
11
|
+
|
14
12
|
def electoral_districts!
|
15
13
|
Yajl::Parser.parse(response.parsed_response).map{|x| self.class.rid_to_edid[x['rid'].to_i]}
|
16
14
|
end
|
@@ -2,21 +2,24 @@ module GovKit
|
|
2
2
|
module CA
|
3
3
|
module PostalCode
|
4
4
|
module Strategy
|
5
|
-
# conservative.ca seems unreliable. In the case of B0J2L0, for example,
|
6
|
-
# it does not return three ridings like other sources.
|
7
5
|
class ConservativeCa < Base
|
8
6
|
base_uri 'www.conservative.ca'
|
9
|
-
http_method :
|
10
|
-
path '/?
|
7
|
+
http_method :post
|
8
|
+
path '/?page_id=35'
|
9
|
+
post_data 'findmymp=35&pc=<%= @postal_code %>'
|
11
10
|
|
12
11
|
private
|
13
12
|
|
14
13
|
def electoral_districts!
|
15
|
-
|
14
|
+
images.map{|a| a[:src][/\d+\.jpg\z/]}
|
16
15
|
end
|
17
16
|
|
18
17
|
def valid?
|
19
|
-
|
18
|
+
!images.empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
def images
|
22
|
+
@document ||= Nokogiri::HTML(response.parsed_response, nil, 'utf-8').css('#type img')
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
@@ -2,9 +2,6 @@ module GovKit
|
|
2
2
|
module CA
|
3
3
|
module PostalCode
|
4
4
|
module Strategy
|
5
|
-
# digital-copyright.ca often returns more or fewer electoral districts
|
6
|
-
# than others. It says it uses makethechange.ca, but makethechange.ca
|
7
|
-
# returns different results for, e.g., K0A1K0.
|
8
5
|
class DigitalCopyrightCa < Base
|
9
6
|
base_uri 'www.digital-copyright.ca'
|
10
7
|
http_method :get
|
@@ -2,13 +2,10 @@ module GovKit
|
|
2
2
|
module CA
|
3
3
|
module PostalCode
|
4
4
|
module Strategy
|
5
|
-
# greenparty.ca does not return electoral districts for postal codes
|
6
|
-
# that contain multiple electoral districts. It has no lookup service
|
7
|
-
# between elections.
|
8
5
|
class GreenPartyCa < Base
|
9
|
-
base_uri 'greenparty.ca'
|
6
|
+
base_uri 'www.greenparty.ca'
|
10
7
|
http_method :head
|
11
|
-
path '/search/
|
8
|
+
path '/search/green_geo/<%= @postal_code %>'
|
12
9
|
|
13
10
|
private
|
14
11
|
|
@@ -17,7 +14,7 @@ module GovKit
|
|
17
14
|
end
|
18
15
|
|
19
16
|
def valid?
|
20
|
-
response.headers['location'] != 'http://greenparty.ca/find_your_riding'
|
17
|
+
response.headers['location'] != 'http://www.greenparty.ca/find_your_riding'
|
21
18
|
end
|
22
19
|
end
|
23
20
|
|
@@ -2,21 +2,19 @@ module GovKit
|
|
2
2
|
module CA
|
3
3
|
module PostalCode
|
4
4
|
module Strategy
|
5
|
-
# ndp.ca does not return electoral districts for postal codes that
|
6
|
-
# contain multiple electoral districts.
|
7
5
|
class NDPCa < Base
|
8
6
|
base_uri 'www.ndp.ca'
|
9
|
-
http_method :
|
10
|
-
path '/riding
|
7
|
+
http_method :get
|
8
|
+
path '/your-riding/?search=<%= @postal_code %>'
|
11
9
|
|
12
10
|
private
|
13
11
|
|
14
12
|
def electoral_districts!
|
15
|
-
|
13
|
+
Nokogiri::HTML(response.parsed_response, nil, 'utf-8').css('a.mp-riding-link,a.no-mp').map{|a| a[:href][/\d+\z/]}
|
16
14
|
end
|
17
15
|
|
18
16
|
def valid?
|
19
|
-
response.
|
17
|
+
!response.parsed_response['No ridings or MPs match this search.']
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
@@ -80,13 +80,15 @@ module GovKit
|
|
80
80
|
end
|
81
81
|
|
82
82
|
require 'gov_kit-ca/postal_code/strategy/base'
|
83
|
-
|
83
|
+
# Returns only riding name.
|
84
84
|
#require 'gov_kit-ca/postal_code/strategy/parl_gc_ca'
|
85
|
-
require 'gov_kit-ca/postal_code/strategy/
|
85
|
+
require 'gov_kit-ca/postal_code/strategy/elections_ca' # only one-to-one
|
86
86
|
require 'gov_kit-ca/postal_code/strategy/ndp_ca'
|
87
|
+
require 'gov_kit-ca/postal_code/strategy/liberal_ca' # incomplete database
|
88
|
+
require 'gov_kit-ca/postal_code/strategy/greenparty_ca' # only one-to-one
|
89
|
+
require 'gov_kit-ca/postal_code/strategy/cbc_ca' # disagrees with elections.ca
|
87
90
|
|
88
|
-
#
|
91
|
+
# If not held by Conservative MP, returns only riding name.
|
89
92
|
# require 'gov_kit-ca/postal_code/strategy/conservative_ca'
|
93
|
+
# Returns too many or too few ridings.
|
90
94
|
# require 'gov_kit-ca/postal_code/strategy/digital-copyright_ca'
|
91
|
-
# require 'gov_kit-ca/postal_code/strategy/greenparty_ca'
|
92
|
-
# require 'gov_kit-ca/postal_code/strategy/liberal_ca'
|
data/lib/gov_kit-ca/version.rb
CHANGED
data/lib/gov_kit-ca.rb
CHANGED
data/spec/cbc_ca_spec.rb
CHANGED
@@ -14,8 +14,8 @@ class GovKit::CA::PostalCode::Strategy::CBCCa
|
|
14
14
|
|
15
15
|
it 'should return the electoral districts within a postal code' do
|
16
16
|
{ 'G0C2Y0' => [24019],
|
17
|
-
'T5S2B9' => [48015, 48017],
|
18
|
-
'K0A1K0' => [35025, 35052, 35063, 35064],
|
17
|
+
'T5S2B9' => [48015, 48017],
|
18
|
+
'K0A1K0' => [35025, 35052, 35063, 35064],
|
19
19
|
}.each do |postal_code,electoral_districts|
|
20
20
|
GovKit::CA::PostalCode::Strategy::CBCCa.new(postal_code).electoral_districts.should == electoral_districts
|
21
21
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'gov_kit-ca/postal_code/strategy/conservative_ca'
|
3
|
+
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::ConservativeCa 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::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
|
+
it 'should return the electoral districts within a postal code' do
|
16
|
+
{ 'T5S2B9' => [48015],
|
17
|
+
'K0A1K0' => [35025, 35052, 35063],
|
18
|
+
'B0J2L0' => [12002, 12006, 12009, 12011],
|
19
|
+
}.each do |postal_code,electoral_districts|
|
20
|
+
GovKit::CA::PostalCode::Strategy::ConservativeCa.new(postal_code).electoral_districts.should == electoral_districts
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should (unfortunately) return false if a postal code is held by another party' do
|
25
|
+
GovKit::CA::PostalCode::Strategy::ConservativeCa.new('G0C2Y0').electoral_districts.should be_false
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should return false if a postal code contains no electoral districts' do
|
29
|
+
GovKit::CA::PostalCode::Strategy::ConservativeCa.new('H0H0H0').electoral_districts.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return false if a postal code does not exist' do
|
33
|
+
GovKit::CA::PostalCode::Strategy::ConservativeCa.new('X1B1B1').electoral_districts.should be_false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -1,34 +1,33 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
require 'gov_kit-ca/postal_code/strategy/digital-copyright_ca'
|
3
3
|
|
4
|
-
|
5
|
-
describe
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('digital-copyright_ca', "#{postal_code}.response")
|
12
|
-
end
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa 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::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")
|
13
11
|
end
|
14
12
|
end
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
it 'should return the electoral districts within a postal code' do
|
16
|
+
{ 'G0C2Y0' => [24019, 24039], # differs from all
|
17
|
+
'T5S2B9' => [48012, 48013, 48014, 48015, 48017, 48018], # differs from all
|
18
|
+
'K0A1K0' => [35025, 35052, 35063],
|
19
|
+
'B0J2L0' => [12002, 12007, 12008],
|
20
|
+
}.each do |postal_code,electoral_districts|
|
21
|
+
GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new(postal_code).electoral_districts.should == electoral_districts
|
23
22
|
end
|
23
|
+
end
|
24
24
|
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
it 'should return false if a postal code contains no electoral districts' do
|
26
|
+
GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new('H0H0H0').electoral_districts.should be_false
|
27
|
+
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
end
|
29
|
+
it 'should return false if a postal code does not exist' do
|
30
|
+
GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new('X1B1B1').electoral_districts.should be_false
|
32
31
|
end
|
33
32
|
end
|
34
33
|
end
|
data/spec/elections_ca_spec.rb
CHANGED
@@ -1,36 +1,35 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
class GovKit::CA::PostalCode::Strategy::ElectionsCa
|
4
|
-
describe GovKit::CA::PostalCode::Strategy::ElectionsCa 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::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
3
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::ElectionsCa 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::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")
|
20
11
|
end
|
21
12
|
end
|
13
|
+
end
|
22
14
|
|
23
|
-
|
24
|
-
|
15
|
+
it 'should return the electoral districts within a postal code' do
|
16
|
+
{ 'G0C2Y0' => [24019],
|
17
|
+
'T5S2B9' => [48015],
|
18
|
+
}.each do |postal_code,electoral_districts|
|
19
|
+
GovKit::CA::PostalCode::Strategy::ElectionsCa.new(postal_code).electoral_districts.should == electoral_districts
|
25
20
|
end
|
21
|
+
end
|
26
22
|
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
it 'should (unfortunately) return false if a postal code contains multiple electoral districts' do
|
24
|
+
GovKit::CA::PostalCode::Strategy::ElectionsCa.new('K0A1K0').electoral_districts.should be_false # B0J2L0
|
25
|
+
end
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
it 'should return false if a postal code contains no electoral districts' do
|
28
|
+
GovKit::CA::PostalCode::Strategy::ElectionsCa.new('H0H0H0').electoral_districts.should be_false
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should return false if a postal code does not exist' do
|
32
|
+
GovKit::CA::PostalCode::Strategy::ElectionsCa.new('X1B1B1').electoral_districts.should be_false
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
data/spec/greenparty_ca_spec.rb
CHANGED
@@ -1,36 +1,35 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
require 'gov_kit-ca/postal_code/strategy/greenparty_ca'
|
2
3
|
|
3
|
-
|
4
|
-
describe
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::GreenPartyCa 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::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")
|
12
11
|
end
|
13
12
|
end
|
13
|
+
end
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
15
|
+
it 'should return the electoral districts within a postal code' do
|
16
|
+
{ 'G0C2Y0' => [24019],
|
17
|
+
'T5S2B9' => [48015],
|
18
|
+
}.each do |postal_code,electoral_districts|
|
19
|
+
GovKit::CA::PostalCode::Strategy::GreenPartyCa.new(postal_code).electoral_districts.should == electoral_districts
|
21
20
|
end
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
it 'should (unfortunately) return false if a postal code contains multiple electoral districts' do
|
24
|
+
GovKit::CA::PostalCode::Strategy::GreenPartyCa.new('K0A1K0').electoral_districts.should be_false # B0J2L0
|
25
|
+
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
it 'should return false if a postal code contains no electoral districts' do
|
28
|
+
GovKit::CA::PostalCode::Strategy::GreenPartyCa.new('H0H0H0').electoral_districts.should be_false
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
31
|
+
it 'should return false if a postal code does not exist' do
|
32
|
+
GovKit::CA::PostalCode::Strategy::GreenPartyCa.new('X1B1B1').electoral_districts.should be_false
|
34
33
|
end
|
35
34
|
end
|
36
35
|
end
|
data/spec/liberal_ca_spec.rb
CHANGED
@@ -1,34 +1,36 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
require 'gov_kit-ca/postal_code/strategy/liberal_ca'
|
3
3
|
|
4
|
-
|
5
|
-
describe
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('liberal_ca', "#{postal_code}.response")
|
12
|
-
end
|
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")
|
13
11
|
end
|
14
12
|
end
|
13
|
+
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
15
|
+
it 'should return the electoral districts within a postal code' do
|
16
|
+
{ 'G0C2Y0' => [24019],
|
17
|
+
'T5S2B9' => [48015],
|
18
|
+
'K0A1K0' => [35025, 35052, 35063],
|
19
|
+
}.each do |postal_code,electoral_districts|
|
20
|
+
GovKit::CA::PostalCode::Strategy::LiberalCa.new(postal_code).electoral_districts.should == electoral_districts
|
23
21
|
end
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
it 'should (unfortunately) return false if a postal code is rare or ambiguous' do
|
25
|
+
GovKit::CA::PostalCode::Strategy::LiberalCa.new('B0J2L0').electoral_districts.should be_false
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
it 'should return false if a postal code contains no electoral districts' do
|
29
|
+
GovKit::CA::PostalCode::Strategy::LiberalCa.new('H0H0H0').electoral_districts.should be_false
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should return false if a postal code does not exist' do
|
33
|
+
GovKit::CA::PostalCode::Strategy::LiberalCa.new('X1B1B1').electoral_districts.should be_false
|
32
34
|
end
|
33
35
|
end
|
34
36
|
end
|
data/spec/ndp_ca_spec.rb
CHANGED
@@ -1,36 +1,33 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
class GovKit::CA::PostalCode::Strategy::NDPCa
|
4
|
-
describe GovKit::CA::PostalCode::Strategy::NDPCa 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::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
3
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::NDPCa 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::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")
|
20
11
|
end
|
21
12
|
end
|
13
|
+
end
|
22
14
|
|
23
|
-
|
24
|
-
|
15
|
+
it 'should return the electoral districts within a postal code' do
|
16
|
+
{ 'G0C2Y0' => [24019],
|
17
|
+
'T5S2B9' => [48015],
|
18
|
+
'K0A1K0' => [35012, 35025, 35040, 35052],
|
19
|
+
'B0J2L0' => [12002, 12006, 12009, 12011],
|
20
|
+
}.each do |postal_code,electoral_districts|
|
21
|
+
GovKit::CA::PostalCode::Strategy::NDPCa.new(postal_code).electoral_districts.should == electoral_districts
|
25
22
|
end
|
23
|
+
end
|
26
24
|
|
27
|
-
|
28
|
-
|
29
|
-
|
25
|
+
it 'should return false if a postal code contains no electoral districts' do
|
26
|
+
GovKit::CA::PostalCode::Strategy::NDPCa.new('H0H0H0').electoral_districts.should be_false
|
27
|
+
end
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
29
|
+
it 'should return false if a postal code does not exist' do
|
30
|
+
GovKit::CA::PostalCode::Strategy::NDPCa.new('X1B1B1').electoral_districts.should be_false
|
34
31
|
end
|
35
32
|
end
|
36
33
|
end
|
data/spec/postal_code_spec.rb
CHANGED
@@ -33,7 +33,8 @@ module GovKit::CA::PostalCode
|
|
33
33
|
{ 'ElectionsCa' => 'elections_ca',
|
34
34
|
'CBCCa' => 'cbc_ca',
|
35
35
|
'NDPCa' => 'ndp_ca',
|
36
|
-
|
36
|
+
# GreenPartyCa is broken.
|
37
|
+
#'GreenPartyCa' => 'greenparty_ca',
|
37
38
|
}.each do |const,path|
|
38
39
|
%w(A1A1A1 K0A1K0 H0H0H0).each do |postal_code|
|
39
40
|
strategy = GovKit::CA::PostalCode::Strategy.const_get(const).new(postal_code)
|
@@ -46,7 +47,7 @@ module GovKit::CA::PostalCode
|
|
46
47
|
|
47
48
|
it 'should return the electoral districts within a postal code' do
|
48
49
|
{ 'A1A1A1' => [10007],
|
49
|
-
'K0A1K0' => [
|
50
|
+
'K0A1K0' => [35012, 35025, 35040, 35052],
|
50
51
|
}.each do |postal_code,electoral_districts|
|
51
52
|
subject.find_electoral_districts_by_postal_code(postal_code).should == electoral_districts
|
52
53
|
end
|