govkit-ca 0.0.2 → 0.0.3
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.
- data/LICENSE +1 -1
- data/README.md +2 -2
- data/Rakefile +1 -1
- data/govkit-ca.gemspec +8 -8
- data/lib/gov_kit-ca/postal_code/strategy/base.rb +33 -1
- data/lib/gov_kit-ca/postal_code/strategy/{cbc-ca.rb → cbc_ca.rb} +6 -13
- data/lib/gov_kit-ca/postal_code/strategy/{conservative-ca.rb → conservative_ca.rb} +2 -4
- data/lib/gov_kit-ca/postal_code/strategy/digital-copyright_ca.rb +27 -0
- data/lib/gov_kit-ca/postal_code/strategy/{elections-ca.rb → elections_ca.rb} +2 -4
- data/lib/gov_kit-ca/postal_code/strategy/{greenparty-ca.rb → greenparty_ca.rb} +3 -5
- data/lib/gov_kit-ca/postal_code/strategy/{liberal-ca.rb → liberal_ca.rb} +3 -5
- data/lib/gov_kit-ca/postal_code/strategy/{ndp-ca.rb → ndp_ca.rb} +3 -5
- data/lib/gov_kit-ca/postal_code/strategy/{parl-gc-ca.rb → parl_gc_ca.rb} +3 -5
- data/lib/gov_kit-ca/postal_code/{strategy.rb → strategy_set.rb} +1 -7
- data/lib/gov_kit-ca/postal_code.rb +9 -3
- data/lib/gov_kit-ca/version.rb +1 -1
- data/lib/gov_kit-ca.rb +2 -1
- data/spec/cbc_ca_spec.rb +13 -6
- data/spec/digital-copyright_ca_spec.rb +32 -0
- data/spec/elections_ca_spec.rb +7 -0
- data/spec/fixtures/cbc_ca/A1A1A1.response +11 -0
- data/spec/fixtures/cbc_ca/G0C2Y0.response +11 -0
- data/spec/fixtures/cbc_ca/H0H0H0.response +309 -0
- data/spec/fixtures/cbc_ca/K0A1K0.response +11 -0
- data/spec/fixtures/cbc_ca/T5S2B9.response +11 -0
- data/spec/fixtures/cbc_ca/X1B1B1.response +309 -0
- data/spec/fixtures/conservative_ca/A1A1A1.response +467 -0
- data/spec/fixtures/conservative_ca/G0C2Y0.response +467 -0
- data/spec/fixtures/conservative_ca/H0H0H0.response +467 -0
- data/spec/fixtures/conservative_ca/K0A1K0.response +467 -0
- data/spec/fixtures/conservative_ca/T5S2B9.response +467 -0
- data/spec/fixtures/conservative_ca/X1B1B1.response +467 -0
- data/spec/fixtures/digital-copyright_ca/A1A1A1.response +262 -0
- data/spec/fixtures/digital-copyright_ca/G0C2Y0.response +263 -0
- data/spec/fixtures/digital-copyright_ca/H0H0H0.response +278 -0
- data/spec/fixtures/digital-copyright_ca/K0A1K0.response +264 -0
- data/spec/fixtures/digital-copyright_ca/T5S2B9.response +267 -0
- data/spec/fixtures/digital-copyright_ca/X1B1B1.response +278 -0
- data/spec/fixtures/elections_ca/A1A1A1.response +12 -0
- data/spec/fixtures/elections_ca/G0C2Y0.response +12 -0
- data/spec/fixtures/elections_ca/H0H0H0.response +11 -0
- data/spec/fixtures/elections_ca/K0A1K0.response +11 -0
- data/spec/fixtures/elections_ca/T5S2B9.response +12 -0
- data/spec/fixtures/elections_ca/X1B1B1.response +11 -0
- data/spec/fixtures/greenparty_ca/A1A1A1.response +13 -0
- data/spec/fixtures/greenparty_ca/G0C2Y0.response +13 -0
- data/spec/fixtures/greenparty_ca/H0H0H0.response +13 -0
- data/spec/fixtures/greenparty_ca/K0A1K0.response +13 -0
- data/spec/fixtures/greenparty_ca/T5S2B9.response +13 -0
- data/spec/fixtures/greenparty_ca/X1B1B1.response +13 -0
- data/spec/fixtures/liberal_ca/A1A1A1.response +582 -0
- data/spec/fixtures/liberal_ca/G0C2Y0.response +582 -0
- data/spec/fixtures/liberal_ca/H0H0H0.response +663 -0
- data/spec/fixtures/liberal_ca/K0A1K0.response +663 -0
- data/spec/fixtures/liberal_ca/T5S2B9.response +663 -0
- data/spec/fixtures/liberal_ca/X1B1B1.response +663 -0
- data/spec/fixtures/ndp_ca/A1A1A1.response +12 -0
- data/spec/fixtures/ndp_ca/G0C2Y0.response +12 -0
- data/spec/fixtures/ndp_ca/H0H0H0.response +12 -0
- data/spec/fixtures/ndp_ca/K0A1K0.response +12 -0
- data/spec/fixtures/ndp_ca/T5S2B9.response +12 -0
- data/spec/fixtures/ndp_ca/X1B1B1.response +12 -0
- data/spec/fixtures/parl_gc_ca/A1A1A1.response +337 -0
- data/spec/fixtures/parl_gc_ca/G0C2Y0.response +337 -0
- data/spec/fixtures/parl_gc_ca/H0H0H0.response +226 -0
- data/spec/fixtures/parl_gc_ca/K0A1K0.response +784 -0
- data/spec/fixtures/parl_gc_ca/T5S2B9.response +511 -0
- data/spec/fixtures/parl_gc_ca/X1B1B1.response +226 -0
- data/spec/greenparty_ca_spec.rb +7 -0
- data/spec/liberal_ca_spec.rb +10 -2
- data/spec/ndp_ca_spec.rb +7 -0
- data/spec/postal_code_spec.rb +13 -0
- data/spec/spec_helper.rb +13 -0
- data/spec/strategy_set_spec.rb +0 -0
- data/tasks/postal-code-for-districts.csv +212 -0
- data/tasks/tasks.rb +73 -0
- metadata +180 -98
- data/lib/gov_kit-ca/postal_code/strategy/digital_copyright-ca.rb +0 -28
- data/tasks/rid_to_edid.rb +0 -136
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -43,6 +43,6 @@ GovKit-CA will raise GovKit::CA::ResourceNotFound if the electoral districts wit
|
|
|
43
43
|
|
|
44
44
|
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
45
|
|
|
46
|
-
Govkit-CA's main repository is on GitHub: [http://github.com/
|
|
46
|
+
Govkit-CA'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
47
|
|
|
48
|
-
Copyright (c) 2011
|
|
48
|
+
Copyright (c) 2011 Open North Inc., released under the MIT license
|
data/Rakefile
CHANGED
data/govkit-ca.gemspec
CHANGED
|
@@ -6,9 +6,9 @@ Gem::Specification.new do |s|
|
|
|
6
6
|
s.name = "govkit-ca"
|
|
7
7
|
s.version = GovKit::CA::VERSION
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
|
9
|
-
s.authors = ["
|
|
10
|
-
s.email = ["
|
|
11
|
-
s.homepage = "http://github.com/
|
|
9
|
+
s.authors = ["Open North"]
|
|
10
|
+
s.email = ["info@opennorth.ca"]
|
|
11
|
+
s.homepage = "http://github.com/opennorth/govkit-ca"
|
|
12
12
|
s.summary = %q{Easy access to Canadian civic data around the web}
|
|
13
13
|
s.description = %q{GovKit-CA lets you quickly get encapsulated Ruby objects for Canadian civic data.}
|
|
14
14
|
|
|
@@ -19,9 +19,9 @@ Gem::Specification.new do |s|
|
|
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
|
20
20
|
s.require_paths = ["lib"]
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
s.add_runtime_dependency('
|
|
24
|
-
s.add_runtime_dependency('
|
|
25
|
-
s.
|
|
26
|
-
s.add_development_dependency('
|
|
22
|
+
s.add_runtime_dependency('httparty', '~> 0.7.8')
|
|
23
|
+
s.add_runtime_dependency('nokogiri', '~> 1.5.0')
|
|
24
|
+
s.add_runtime_dependency('yajl-ruby', '~> 0.8.2')
|
|
25
|
+
s.add_development_dependency('rspec', '~> 2.6.0')
|
|
26
|
+
s.add_development_dependency('fakeweb', '~> 1.3.0')
|
|
27
27
|
end
|
|
@@ -5,9 +5,14 @@ module GovKit
|
|
|
5
5
|
# Abstract class for implementing postal code to electoral district
|
|
6
6
|
# strategies.
|
|
7
7
|
#
|
|
8
|
-
# The following methods must be implemented in sub-classes:
|
|
8
|
+
# The following instance methods must be implemented in sub-classes:
|
|
9
9
|
#
|
|
10
10
|
# * `electoral_districts!`
|
|
11
|
+
#
|
|
12
|
+
# The following class methods must be called in sub-classes:
|
|
13
|
+
#
|
|
14
|
+
# * `http_method`
|
|
15
|
+
# * `path`
|
|
11
16
|
class Base
|
|
12
17
|
include HTTParty
|
|
13
18
|
follow_redirects false
|
|
@@ -38,6 +43,33 @@ module GovKit
|
|
|
38
43
|
def valid?
|
|
39
44
|
true
|
|
40
45
|
end
|
|
46
|
+
|
|
47
|
+
# Performs the request and returns the response.
|
|
48
|
+
# @return [HTTParty::Response] a HTTParty response object
|
|
49
|
+
def response
|
|
50
|
+
@response ||= self.class.send self.class.http_method, path
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Allows setting an HTTP method to be used for each request.
|
|
54
|
+
# @param [Symbol] http_method an HTTP method
|
|
55
|
+
# @return [Symbol] the HTTP method to use to send the request
|
|
56
|
+
def self.http_method(http_method = nil)
|
|
57
|
+
return default_options[:http_method] unless http_method
|
|
58
|
+
default_options[:http_method] = http_method
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Allows setting an absolute path to be used in each request.
|
|
62
|
+
# @param [String] path an ERB template for the absolute path
|
|
63
|
+
# @return [String] the absolute path to request
|
|
64
|
+
def self.path(path = nil)
|
|
65
|
+
return default_options[:path] unless path
|
|
66
|
+
default_options[:path] = path
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [String] the absolute path to be used in the request
|
|
70
|
+
def path
|
|
71
|
+
ERB.new(self.class.path).result binding
|
|
72
|
+
end
|
|
41
73
|
end
|
|
42
74
|
end
|
|
43
75
|
end
|
|
@@ -5,32 +5,25 @@ module GovKit
|
|
|
5
5
|
# cbc.ca ought to be a reliable source. It is unknown if its database
|
|
6
6
|
# is kept up-to-date between elections, however.
|
|
7
7
|
# @see https://github.com/danielharan/pc_scraper
|
|
8
|
-
class
|
|
8
|
+
class CBCCa < Base
|
|
9
9
|
base_uri 'www.cbc.ca'
|
|
10
|
+
http_method :get
|
|
11
|
+
path '/news/canadavotes/myriding/postalcodes/<%= @letter %>/<%= @fsa %>/<%= @ldu %>.html'
|
|
10
12
|
|
|
11
13
|
def initialize(postal_code)
|
|
12
|
-
@fsa, @letter, @ldu = postal_code.downcase.match(/\A((.).{2})(.{3})\
|
|
14
|
+
@fsa, @letter, @ldu = postal_code.downcase.match(/\A((.).{2})(.{3})\z/)[1..3]
|
|
13
15
|
super
|
|
14
16
|
end
|
|
15
17
|
|
|
16
|
-
def json_response # Yajl barfs on bad encoding
|
|
17
|
-
Yajl::Parser.parse(response.parsed_response) rescue JSON.parse(response.parsed_response)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
18
|
private
|
|
21
|
-
|
|
22
19
|
def electoral_districts!
|
|
23
|
-
|
|
20
|
+
Yajl::Parser.parse(Iconv.new('UTF-8', 'ISO-8859-1').iconv(response.parsed_response)).map{|x| self.class.rid_to_edid[x['rid']]}
|
|
24
21
|
end
|
|
25
22
|
|
|
26
23
|
def valid?
|
|
27
24
|
!!response.headers['expires']
|
|
28
25
|
end
|
|
29
26
|
|
|
30
|
-
def response
|
|
31
|
-
@response ||= self.class.get "/news/canadavotes/myriding/postalcodes/#{@letter}/#{@fsa}/#{@ldu}.html"
|
|
32
|
-
end
|
|
33
|
-
|
|
34
27
|
# cbc.ca uses an internal riding ID, which must be matched to a
|
|
35
28
|
# canonical electoral district ID.
|
|
36
29
|
# @return [Hash] a map of cbc.ca riding ID to electoral district ID
|
|
@@ -39,7 +32,7 @@ module GovKit
|
|
|
39
32
|
end
|
|
40
33
|
end
|
|
41
34
|
|
|
42
|
-
StrategySet.register
|
|
35
|
+
StrategySet.register CBCCa
|
|
43
36
|
end
|
|
44
37
|
end
|
|
45
38
|
end
|
|
@@ -6,6 +6,8 @@ module GovKit
|
|
|
6
6
|
# it does not return three ridings like other sources.
|
|
7
7
|
class ConservativeCa < Base
|
|
8
8
|
base_uri 'www.conservative.ca'
|
|
9
|
+
http_method :get
|
|
10
|
+
path '/?section_id=1051&postal_code=<%= @postal_code %>'
|
|
9
11
|
|
|
10
12
|
private
|
|
11
13
|
|
|
@@ -16,10 +18,6 @@ module GovKit
|
|
|
16
18
|
def valid?
|
|
17
19
|
# TODO
|
|
18
20
|
end
|
|
19
|
-
|
|
20
|
-
def response
|
|
21
|
-
@response ||= self.class.get "/?section_id=1051&postal_code=#{@postal_code}"
|
|
22
|
-
end
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
StrategySet.register ConservativeCa
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module GovKit
|
|
2
|
+
module CA
|
|
3
|
+
module PostalCode
|
|
4
|
+
module Strategy
|
|
5
|
+
# digital-copyright.ca often returns more or fewer electoral districts
|
|
6
|
+
# than others. Occasionally suffers from timeout errors.
|
|
7
|
+
class DigitalCopyrightCa < Base
|
|
8
|
+
base_uri 'www.digital-copyright.ca'
|
|
9
|
+
http_method :get
|
|
10
|
+
path '/edid/postal?postalcode=<%= @postal_code %>'
|
|
11
|
+
|
|
12
|
+
private
|
|
13
|
+
|
|
14
|
+
def electoral_districts!
|
|
15
|
+
Nokogiri::HTML(response.parsed_response, nil, 'utf-8').css('.node .content a').map{|a| a[:href][/\d+\z/]}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def valid?
|
|
19
|
+
!response.parsed_response.match /\b(invalid postal code|not found)\b/
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
StrategySet.register DigitalCopyrightCa
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -7,6 +7,8 @@ module GovKit
|
|
|
7
7
|
# @see https://github.com/danielharan/postal_code_to_edid_webservice
|
|
8
8
|
class ElectionsCa < Base
|
|
9
9
|
base_uri 'elections.ca'
|
|
10
|
+
http_method :head
|
|
11
|
+
path '/scripts/pss/FindED.aspx?PC=<%= @postal_code %>'
|
|
10
12
|
|
|
11
13
|
private
|
|
12
14
|
|
|
@@ -17,10 +19,6 @@ module GovKit
|
|
|
17
19
|
def valid?
|
|
18
20
|
!!response.headers['location']
|
|
19
21
|
end
|
|
20
|
-
|
|
21
|
-
def response
|
|
22
|
-
@response ||= self.class.head "/scripts/pss/FindED.aspx?PC=#{@postal_code}"
|
|
23
|
-
end
|
|
24
22
|
end
|
|
25
23
|
|
|
26
24
|
StrategySet.register ElectionsCa
|
|
@@ -6,20 +6,18 @@ module GovKit
|
|
|
6
6
|
# that contain multiple electoral districts.
|
|
7
7
|
class GreenPartyCa < Base
|
|
8
8
|
base_uri 'greenparty.ca'
|
|
9
|
+
http_method :head
|
|
10
|
+
path '/search/civicrm_lookup/<%= @postal_code %>'
|
|
9
11
|
|
|
10
12
|
private
|
|
11
13
|
|
|
12
14
|
def electoral_districts!
|
|
13
|
-
[ response.headers['location'][/\d+\
|
|
15
|
+
[ response.headers['location'][/\d+\z/] ]
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
def valid?
|
|
17
19
|
response.headers['location'] != 'http://greenparty.ca/find_your_riding'
|
|
18
20
|
end
|
|
19
|
-
|
|
20
|
-
def response
|
|
21
|
-
@response ||= self.class.head "/search/civicrm_lookup/#{@postal_code}"
|
|
22
|
-
end
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
StrategySet.register GreenPartyCa
|
|
@@ -5,20 +5,18 @@ module GovKit
|
|
|
5
5
|
# liberal.ca is unreliable during elections.
|
|
6
6
|
class LiberalCa < Base
|
|
7
7
|
base_uri 'www.liberal.ca'
|
|
8
|
+
http_method :get
|
|
9
|
+
path '/riding/postal/<%= @postal_code %>/'
|
|
8
10
|
|
|
9
11
|
private
|
|
10
12
|
|
|
11
13
|
def electoral_districts!
|
|
12
|
-
Nokogiri::HTML(response.parsed_response).css('img.RidingListImage').map{|img| img[:src][/\d{5}/]}
|
|
14
|
+
Nokogiri::HTML(response.parsed_response, nil, 'utf-8').css('img.RidingListImage').map{|img| img[:src][/\d{5}/]}
|
|
13
15
|
end
|
|
14
16
|
|
|
15
17
|
def valid?
|
|
16
18
|
!response.parsed_response.match /\bOopsies!/
|
|
17
19
|
end
|
|
18
|
-
|
|
19
|
-
def response
|
|
20
|
-
@response ||= self.class.get "/riding/postal/#{@postal_code}/"
|
|
21
|
-
end
|
|
22
20
|
end
|
|
23
21
|
|
|
24
22
|
StrategySet.register LiberalCa
|
|
@@ -6,20 +6,18 @@ module GovKit
|
|
|
6
6
|
# contain multiple electoral districts.
|
|
7
7
|
class NDPCa < Base
|
|
8
8
|
base_uri 'www.ndp.ca'
|
|
9
|
+
http_method :head
|
|
10
|
+
path '/riding/<%= @postal_code %>'
|
|
9
11
|
|
|
10
12
|
private
|
|
11
13
|
|
|
12
14
|
def electoral_districts!
|
|
13
|
-
[ response.headers['location'][/\d+\
|
|
15
|
+
[ response.headers['location'][/\d+\z/] ]
|
|
14
16
|
end
|
|
15
17
|
|
|
16
18
|
def valid?
|
|
17
19
|
response.headers['location'] != 'http://www.ndp.ca/'
|
|
18
20
|
end
|
|
19
|
-
|
|
20
|
-
def response
|
|
21
|
-
@response ||= self.class.head "/riding/#{@postal_code}"
|
|
22
|
-
end
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
StrategySet.register NDPCa
|
|
@@ -5,7 +5,9 @@ module GovKit
|
|
|
5
5
|
# parl.gc.ca seems unreliable. In the case of K0A1K0, for example, it
|
|
6
6
|
# does not return seven ridings like other sources.
|
|
7
7
|
class ParlGcCa < Base
|
|
8
|
-
base_uri '
|
|
8
|
+
base_uri 'www.parl.gc.ca'
|
|
9
|
+
http_method :get
|
|
10
|
+
path '/ParlInfo/Compilations/HouseOfCommons/MemberByPostalCode.aspx?PostalCode=<%= @postal_code %>'
|
|
9
11
|
|
|
10
12
|
private
|
|
11
13
|
|
|
@@ -16,10 +18,6 @@ module GovKit
|
|
|
16
18
|
def valid?
|
|
17
19
|
# TODO
|
|
18
20
|
end
|
|
19
|
-
|
|
20
|
-
def response
|
|
21
|
-
@response ||= self.class.get "/parlinfo/Compilations/HouseOfCommons/MemberByPostalCode.aspx?PostalCode=#{@postal_code}"
|
|
22
|
-
end
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
StrategySet.register ParlGcCa
|
|
@@ -2,7 +2,7 @@ module GovKit
|
|
|
2
2
|
module CA
|
|
3
3
|
module PostalCode
|
|
4
4
|
# The set of postal code to electoral district strategies.
|
|
5
|
-
|
|
5
|
+
module StrategySet
|
|
6
6
|
# Stores the strategy set.
|
|
7
7
|
# @return [Array<Strategy::Base>] the strategy set
|
|
8
8
|
def self.strategies
|
|
@@ -40,9 +40,3 @@ module GovKit
|
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
|
-
|
|
44
|
-
require 'gov_kit-ca/postal_code/strategy/base'
|
|
45
|
-
require 'gov_kit-ca/postal_code/strategy/elections-ca'
|
|
46
|
-
require 'gov_kit-ca/postal_code/strategy/cbc-ca'
|
|
47
|
-
require 'gov_kit-ca/postal_code/strategy/ndp-ca'
|
|
48
|
-
require 'gov_kit-ca/postal_code/strategy/greenparty-ca'
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
require 'gov_kit-ca/postal_code/strategy'
|
|
2
|
-
|
|
3
1
|
module GovKit
|
|
4
2
|
module CA
|
|
5
3
|
# A collection of postal code helpers.
|
|
6
4
|
# @see http://en.wikipedia.org/wiki/Postal_codes_in_Canada Postal codes in Canada
|
|
7
5
|
module PostalCode
|
|
6
|
+
autoload :StrategySet, 'gov_kit-ca/postal_code/strategy_set'
|
|
7
|
+
|
|
8
8
|
# @param [String] postal_code a postal code
|
|
9
9
|
# @return [Boolean] whether the postal code is properly formatted
|
|
10
10
|
# @see http://en.wikipedia.org/wiki/Postal_codes_in_Canada#Number_of_possible_postal_codes Possible postal codes
|
|
11
11
|
def self.valid?(postal_code)
|
|
12
|
-
!!postal_code.match(/\A[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]\
|
|
12
|
+
!!postal_code.match(/\A[ABCEGHJKLMNPRSTVXY][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9][ABCEGHJKLMNPRSTVWXYZ][0-9]\z/)
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
# Returns the electoral districts within a postal code.
|
|
@@ -78,3 +78,9 @@ module GovKit
|
|
|
78
78
|
end
|
|
79
79
|
end
|
|
80
80
|
end
|
|
81
|
+
|
|
82
|
+
require 'gov_kit-ca/postal_code/strategy/base'
|
|
83
|
+
require 'gov_kit-ca/postal_code/strategy/elections_ca'
|
|
84
|
+
require 'gov_kit-ca/postal_code/strategy/cbc_ca'
|
|
85
|
+
require 'gov_kit-ca/postal_code/strategy/ndp_ca'
|
|
86
|
+
require 'gov_kit-ca/postal_code/strategy/greenparty_ca'
|
data/lib/gov_kit-ca/version.rb
CHANGED
data/lib/gov_kit-ca.rb
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
|
|
2
2
|
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'iconv'
|
|
3
5
|
require 'json'
|
|
4
6
|
require 'yaml'
|
|
5
7
|
|
|
6
|
-
#require 'graticule'
|
|
7
8
|
require 'httparty'
|
|
8
9
|
require 'nokogiri'
|
|
9
10
|
require 'yajl'
|
data/spec/cbc_ca_spec.rb
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
2
|
|
|
3
|
-
class GovKit::CA::PostalCode::Strategy::
|
|
4
|
-
describe GovKit::CA::PostalCode::Strategy::
|
|
3
|
+
class GovKit::CA::PostalCode::Strategy::CBCCa
|
|
4
|
+
describe GovKit::CA::PostalCode::Strategy::CBCCa 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::CBCCa.new(postal_code)
|
|
9
|
+
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('cbc_ca', "#{postal_code}.response")
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
6
13
|
it 'should return the electoral districts within a postal code' do
|
|
7
14
|
{ 'G0C2Y0' => [24019],
|
|
8
|
-
'T5S2B9' => [48015, 48017], # differs from elections.ca, ndp.ca
|
|
15
|
+
'T5S2B9' => [48015, 48017], # differs from elections.ca, ndp.ca, greenparty.ca
|
|
9
16
|
'K0A1K0' => [35012, 35025, 35040, 35052, 35063, 35064, 35087],
|
|
10
17
|
}.each do |postal_code,electoral_districts|
|
|
11
|
-
GovKit::CA::PostalCode::Strategy::
|
|
18
|
+
GovKit::CA::PostalCode::Strategy::CBCCa.new(postal_code).electoral_districts.should == electoral_districts
|
|
12
19
|
end
|
|
13
20
|
end
|
|
14
21
|
|
|
15
22
|
it 'should return false if a postal code contains no electoral districts' do
|
|
16
|
-
GovKit::CA::PostalCode::Strategy::
|
|
23
|
+
GovKit::CA::PostalCode::Strategy::CBCCa.new('H0H0H0').electoral_districts.should be_false
|
|
17
24
|
end
|
|
18
25
|
|
|
19
26
|
it 'should return false if a postal code does not exist' do
|
|
20
|
-
GovKit::CA::PostalCode::Strategy::
|
|
27
|
+
GovKit::CA::PostalCode::Strategy::CBCCa.new('X1B1B1').electoral_districts.should be_false
|
|
21
28
|
end
|
|
22
29
|
end
|
|
23
30
|
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
2
|
+
require 'gov_kit-ca/postal_code/strategy/digital-copyright_ca'
|
|
3
|
+
|
|
4
|
+
class GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa
|
|
5
|
+
describe GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa do
|
|
6
|
+
describe '#electoral_districts' do
|
|
7
|
+
before :all do
|
|
8
|
+
%w(G0C2Y0 T5S2B9 K0A1K0 H0H0H0 X1B1B1).each do |postal_code|
|
|
9
|
+
strategy = GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new(postal_code)
|
|
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
|
+
|
|
14
|
+
it 'should return the electoral districts within a postal code' do
|
|
15
|
+
{ 'G0C2Y0' => [24019, 24039], # differs from all
|
|
16
|
+
'T5S2B9' => [48012, 48013, 48014, 48015, 48017, 48018], # differs from all
|
|
17
|
+
'K0A1K0' => [35025, 35052, 35063], # differs from all
|
|
18
|
+
}.each do |postal_code,electoral_districts|
|
|
19
|
+
GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new(postal_code).electoral_districts.should == electoral_districts
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'should return false if a postal code contains no electoral districts' do
|
|
24
|
+
GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new('H0H0H0').electoral_districts.should be_false
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'should return false if a postal code does not exist' do
|
|
28
|
+
GovKit::CA::PostalCode::Strategy::DigitalCopyrightCa.new('X1B1B1').electoral_districts.should be_false
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
data/spec/elections_ca_spec.rb
CHANGED
|
@@ -3,6 +3,13 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
|
|
3
3
|
class GovKit::CA::PostalCode::Strategy::ElectionsCa
|
|
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
|
+
FakeWeb.register_uri strategy.class.http_method, "#{strategy.class.base_uri}#{strategy.send(:path)}", :response => fixture_path('elections_ca', "#{postal_code}.response")
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
6
13
|
it 'should return the electoral districts within a postal code' do
|
|
7
14
|
{ 'G0C2Y0' => [24019],
|
|
8
15
|
'T5S2B9' => [48015], # differs from cbc.ca
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
HTTP/1.1 200 OK
|
|
2
|
+
Server: Apache/2.2.3 (Red Hat)
|
|
3
|
+
Content-Length: 52
|
|
4
|
+
Content-Type: text/html
|
|
5
|
+
X-Origin-Server: web15:sb10
|
|
6
|
+
Cache-Control: max-age=10
|
|
7
|
+
Expires: Sun, 05 Jun 2011 03:16:49 GMT
|
|
8
|
+
Date: Sun, 05 Jun 2011 03:16:39 GMT
|
|
9
|
+
Connection: close
|
|
10
|
+
|
|
11
|
+
[{"rid":7,"name":"St. John's South - Mount Pearl"}]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
HTTP/1.1 200 OK
|
|
2
|
+
Server: Apache/2.2.3 (Red Hat)
|
|
3
|
+
Content-Length: 54
|
|
4
|
+
Content-Type: text/html
|
|
5
|
+
X-Origin-Server: web20:sb10
|
|
6
|
+
Cache-Control: max-age=10
|
|
7
|
+
Expires: Sun, 05 Jun 2011 03:16:49 GMT
|
|
8
|
+
Date: Sun, 05 Jun 2011 03:16:39 GMT
|
|
9
|
+
Connection: close
|
|
10
|
+
|
|
11
|
+
[{"rid":52,"name":"Gasp�sie - �les-de-la-Madeleine"}]
|