hdo-storting-importer 0.2.4 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/lib/hdo/storting_importer/api_data_source.rb +22 -4
  2. data/lib/hdo/storting_importer/caching_data_source.rb +4 -4
  3. data/lib/hdo/storting_importer/data_source.rb +0 -3
  4. data/lib/hdo/storting_importer/disk_data_source.rb +4 -4
  5. data/lib/hdo/storting_importer/parsing_data_source.rb +4 -4
  6. data/lib/hdo/storting_importer/promise.rb +1 -2
  7. data/lib/hdo/storting_importer/proposition.rb +2 -2
  8. data/lib/hdo/storting_importer/representative.rb +4 -7
  9. data/lib/hdo/storting_importer/version.rb +1 -1
  10. data/lib/hdo/storting_importer/vote.rb +6 -2
  11. data/lib/hdo/storting_importer.rb +0 -1
  12. data/spec/hdo/storting_importer/caching_data_source_spec.rb +5 -3
  13. data/spec/hdo/storting_importer/representative_spec.rb +1 -1
  14. data/spec/spec_helper.rb +0 -18
  15. metadata +2 -45
  16. data/lib/hdo/storting_importer/converter.rb +0 -40
  17. data/spec/fixtures/input/categories.xml +0 -1351
  18. data/spec/fixtures/input/committees.xml +0 -92
  19. data/spec/fixtures/input/districts.xml +0 -101
  20. data/spec/fixtures/input/parliament_issues.xml +0 -2852
  21. data/spec/fixtures/input/parties.xml +0 -42
  22. data/spec/fixtures/input/promises-a.csv +0 -341
  23. data/spec/fixtures/input/propositions_2175.xml +0 -64
  24. data/spec/fixtures/input/propositions_2176.xml +0 -64
  25. data/spec/fixtures/input/representatives.xml +0 -3218
  26. data/spec/fixtures/input/representatives_today.xml +0 -4872
  27. data/spec/fixtures/input/vote_results_2175.xml +0 -4400
  28. data/spec/fixtures/input/vote_results_2176.xml +0 -4400
  29. data/spec/fixtures/input/votes.xml +0 -85
  30. data/spec/fixtures/output/categories.json +0 -1
  31. data/spec/fixtures/output/committees.json +0 -1
  32. data/spec/fixtures/output/districts.json +0 -1
  33. data/spec/fixtures/output/parliament_issues.json +0 -1
  34. data/spec/fixtures/output/parties.json +0 -1
  35. data/spec/fixtures/output/representatives.json +0 -1
  36. data/spec/fixtures/output/votes.json +0 -1
  37. data/spec/hdo/storting_importer/converter_spec.rb +0 -72
@@ -20,7 +20,16 @@ module Hdo
20
20
  end
21
21
  end
22
22
 
23
- def representatives(period = DEFAULT_PERIOD)
23
+ #
24
+ # fetch representatives for the given period
25
+ #
26
+ # @param [String] period
27
+ #
28
+ # @example
29
+ # data_source.representatives('2009-2013')
30
+ #
31
+
32
+ def representatives(period)
24
33
  fetch "eksport/representanter/?StortingsPeriodeId=#{period}"
25
34
  end
26
35
 
@@ -28,11 +37,20 @@ module Hdo
28
37
  fetch 'eksport/dagensrepresentanter/'
29
38
  end
30
39
 
31
- def parties(session_id = DEFAULT_SESSION)
40
+ #
41
+ # fetch parties for the given session
42
+ #
43
+ # @param [String] session
44
+ #
45
+ # @example
46
+ # data_source.representatives('2011-2012')
47
+ #
48
+
49
+ def parties(session_id)
32
50
  fetch "eksport/partier/?sesjonid=#{session_id}"
33
51
  end
34
52
 
35
- def committees(session_id = DEFAULT_SESSION)
53
+ def committees(session_id)
36
54
  fetch "eksport/komiteer/?SesjonId=#{session_id}"
37
55
  end
38
56
 
@@ -44,7 +62,7 @@ module Hdo
44
62
  fetch "eksport/emner"
45
63
  end
46
64
 
47
- def parliament_issues(session_id = DEFAULT_SESSION)
65
+ def parliament_issues(session_id)
48
66
  fetch "eksport/saker?sesjonid=#{session_id}"
49
67
  end
50
68
 
@@ -6,7 +6,7 @@ module Hdo
6
6
  @cache = cache
7
7
  end
8
8
 
9
- def representatives(period = DEFAULT_PERIOD)
9
+ def representatives(period)
10
10
  cache :representatives, period do
11
11
  @delegate.representatives(period)
12
12
  end
@@ -18,13 +18,13 @@ module Hdo
18
18
  end
19
19
  end
20
20
 
21
- def parties(session_id = DEFAULT_SESSION)
21
+ def parties(session_id)
22
22
  cache :parties, session_id do
23
23
  @delegate.parties(session_id)
24
24
  end
25
25
  end
26
26
 
27
- def committees(session_id = DEFAULT_SESSION)
27
+ def committees(session_id)
28
28
  cache :committees, session_id do
29
29
  @delegate.committees session_id
30
30
  end
@@ -42,7 +42,7 @@ module Hdo
42
42
  end
43
43
  end
44
44
 
45
- def parliament_issues(session_id = DEFAULT_SESSION)
45
+ def parliament_issues(session_id)
46
46
  cache :parliament_issues, session_id do
47
47
  @delegate.issues session_id
48
48
  end
@@ -5,9 +5,6 @@ module Hdo
5
5
  class NotFoundError < StandardError
6
6
  end
7
7
 
8
- DEFAULT_SESSION = '2012-2013'
9
- DEFAULT_PERIOD = '2009-2013'
10
-
11
8
  private
12
9
 
13
10
  def parse(str)
@@ -6,11 +6,11 @@ module Hdo
6
6
  @root = Pathname.new(root)
7
7
  end
8
8
 
9
- def parties(session_id = DEFAULT_SESSION)
9
+ def parties(session_id)
10
10
  fetch "eksport/partier/index.html?sesjonid=#{session_id}"
11
11
  end
12
12
 
13
- def committees(session_id = DEFAULT_SESSION)
13
+ def committees(session_id)
14
14
  fetch "eksport/komiteer/index.html?SesjonId=#{session_id}"
15
15
  end
16
16
 
@@ -22,11 +22,11 @@ module Hdo
22
22
  fetch "eksport/emner/index.html"
23
23
  end
24
24
 
25
- def parliament_issues(session_id = DEFAULT_SESSION)
25
+ def parliament_issues(session_id)
26
26
  fetch "eksport/saker/index.html?sesjonid=#{session_id}"
27
27
  end
28
28
 
29
- def representatives(period = DEFAULT_PERIOD)
29
+ def representatives(period)
30
30
  fetch "eksport/representanter/index.html?StortingsPeriodeId=#{period}"
31
31
  end
32
32
 
@@ -5,7 +5,7 @@ module Hdo
5
5
  @data_source = delegate_data_source
6
6
  end
7
7
 
8
- def representatives(period = DEFAULT_PERIOD)
8
+ def representatives(period)
9
9
  Representative.from_storting_doc @data_source.representatives(period)
10
10
  end
11
11
 
@@ -13,11 +13,11 @@ module Hdo
13
13
  Representative.from_storting_doc @data_source.representatives_today
14
14
  end
15
15
 
16
- def parties(session_id = DEFAULT_SESSION)
16
+ def parties(session_id)
17
17
  Party.from_storting_doc @data_source.parties(session_id)
18
18
  end
19
19
 
20
- def committees(session_id = DEFAULT_SESSION)
20
+ def committees(session_id)
21
21
  Committee.from_storting_doc @data_source.committees(session_id)
22
22
  end
23
23
 
@@ -29,7 +29,7 @@ module Hdo
29
29
  Category.from_storting_doc @data_source.categories
30
30
  end
31
31
 
32
- def parliament_issues(session_id = DEFAULT_SESSION)
32
+ def parliament_issues(session_id)
33
33
  ParliamentIssue.from_storting_doc @data_source.parliament_issues(session_id)
34
34
  end
35
35
 
@@ -101,8 +101,7 @@ module Hdo
101
101
  }.compact
102
102
 
103
103
  if errors.any?
104
- puts errors
105
- # raise "found errors:\n#{errors.join("\n")}"
104
+ raise "found errors:\n#{errors.join("\n")}"
106
105
  end
107
106
 
108
107
  promises
@@ -35,11 +35,11 @@ module Hdo
35
35
  new(*arr)
36
36
  end
37
37
 
38
- def self.from_storting_doc(doc, time)
38
+ def self.from_storting_doc(doc, date)
39
39
  doc.css("voteringsforslag").map do |n|
40
40
  rep_node = n.css("forslag_levert_av_representant").first
41
41
  if rep_node && rep_node['nil'] != 'true'
42
- delivered_by = Representative.from_storting_node(rep_node, Util.session_for_date(time))
42
+ delivered_by = Representative.from_storting_node(rep_node, date)
43
43
  else
44
44
  delivered_by = nil
45
45
  end
@@ -42,18 +42,15 @@ module Hdo
42
42
  nodes = doc.css("dagensrepresentant")
43
43
  nodes += doc.css("representant")
44
44
 
45
- period_node = doc.css("stortingsperiode_id").first
46
- period = Util.period_to_date_range(period_node.text) if period_node
47
-
48
- nodes.map { |e| from_storting_node(e, period) }
45
+ nodes.map { |e| from_storting_node(e) }
49
46
  end
50
47
 
51
- def self.from_storting_node(node, period = nil)
48
+ def self.from_storting_node(node, date = nil)
52
49
  district_node = node.css("fylke navn").first
53
50
  district = district_node ? district_node.text : ''
54
51
 
55
- start_date = period ? period.begin : Util.current_session.begin
56
- end_date = period ? period.end : nil
52
+ start_date = date || Date.today
53
+ end_date = nil
57
54
 
58
55
  party_node = node.css("parti id").first
59
56
  if party_node
@@ -1,5 +1,5 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
- VERSION = "0.2.4"
3
+ VERSION = "0.2.6"
4
4
  end
5
5
  end
@@ -106,13 +106,17 @@ module Hdo
106
106
  short_inspect_string :include => [:external_id, :subject, :time, :counts]
107
107
  end
108
108
 
109
+ def date
110
+ @date ||= Date.parse(time)
111
+ end
112
+
109
113
  def add_storting_propositions(node)
110
- @propositions += Proposition.from_storting_doc(node, Date.parse(time))
114
+ @propositions += Proposition.from_storting_doc(node, date)
111
115
  end
112
116
 
113
117
  def add_storting_results(node)
114
118
  @representatives += node.css("representant_voteringsresultat").map do |n|
115
- rep = Representative.from_storting_node(n.css("representant").first)
119
+ rep = Representative.from_storting_node(n.css("representant").first, date)
116
120
  rep.vote_result = case n.css("votering").text
117
121
  when 'for'
118
122
  'for'
@@ -132,7 +132,6 @@ require 'hdo/storting_importer/proposition'
132
132
  require 'hdo/storting_importer/vote'
133
133
  require 'hdo/storting_importer/governing_period'
134
134
 
135
- require 'hdo/storting_importer/converter'
136
135
  require 'hdo/storting_importer/cli'
137
136
 
138
137
 
@@ -10,10 +10,12 @@ module Hdo
10
10
 
11
11
  ads = CachingDataSource.new(delegate)
12
12
 
13
- delegate.should_receive(:representatives).once.and_return "data"
13
+ delegate.should_receive(:representatives).twice.and_return "data"
14
14
 
15
- ads.representatives # not cached
16
- ads.representatives # cached
15
+ ads.representatives('2011-2012') # not cached
16
+ ads.representatives('2011-2012') # cached
17
+ ads.representatives('2012-2013') # not cached
18
+ ads.representatives('2012-2013') # cached
17
19
  end
18
20
 
19
21
  end
@@ -46,7 +46,7 @@ module Hdo
46
46
  rep.last_name.should == 'Dahl'
47
47
  rep.gender.should == 'M'
48
48
  rep.district.should == 'Akershus'
49
- rep.parties.should == [PartyMembership.from_hash('externalId' => 'H', 'startDate' => '2009-10-01', 'endDate' => '2013-09-30')]
49
+ rep.parties.should == [PartyMembership.from_hash('externalId' => 'H', 'startDate' => Date.today.strftime("%Y-%m-%d"), 'endDate' => nil)]
50
50
  rep.external_id.should == 'ADA'
51
51
  rep.date_of_birth.should == '1975-07-07T00:00:00'
52
52
  rep.date_of_death.should == '0001-01-01T00:00:00'
data/spec/spec_helper.rb CHANGED
@@ -14,24 +14,6 @@ end
14
14
  module Hdo
15
15
  module StortingImporter
16
16
  module SpecHelper
17
- FIXTURES = Pathname.new(File.expand_path("../fixtures", __FILE__))
18
-
19
- def input_fixture(name)
20
- FIXTURES.join(input_path("#{name}.xml")).read
21
- end
22
-
23
- def output_fixture(name)
24
- FIXTURES.join(output_path("#{name}.json")).read
25
- end
26
-
27
- def input_path(filename)
28
- FIXTURES.join("input/#{filename}")
29
- end
30
-
31
- def output_path(filename)
32
- FIXTURES.join("output/#{filename}")
33
- end
34
-
35
17
  def parse(str)
36
18
  doc = Nokogiri::XML.parse(str)
37
19
  doc.remove_namespaces!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hdo-storting-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-03 00:00:00.000000000 Z
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
@@ -277,7 +277,6 @@ files:
277
277
  - lib/hdo/storting_importer/cli.rb
278
278
  - lib/hdo/storting_importer/committee.rb
279
279
  - lib/hdo/storting_importer/committee_membership.rb
280
- - lib/hdo/storting_importer/converter.rb
281
280
  - lib/hdo/storting_importer/core_ext/enumerable.rb
282
281
  - lib/hdo/storting_importer/data_source.rb
283
282
  - lib/hdo/storting_importer/disk_data_source.rb
@@ -309,31 +308,10 @@ files:
309
308
  - lib/hdo/storting_importer/util.rb
310
309
  - lib/hdo/storting_importer/version.rb
311
310
  - lib/hdo/storting_importer/vote.rb
312
- - spec/fixtures/input/categories.xml
313
- - spec/fixtures/input/committees.xml
314
- - spec/fixtures/input/districts.xml
315
- - spec/fixtures/input/parliament_issues.xml
316
- - spec/fixtures/input/parties.xml
317
- - spec/fixtures/input/promises-a.csv
318
- - spec/fixtures/input/propositions_2175.xml
319
- - spec/fixtures/input/propositions_2176.xml
320
- - spec/fixtures/input/representatives.xml
321
- - spec/fixtures/input/representatives_today.xml
322
- - spec/fixtures/input/vote_results_2175.xml
323
- - spec/fixtures/input/vote_results_2176.xml
324
- - spec/fixtures/input/votes.xml
325
- - spec/fixtures/output/categories.json
326
- - spec/fixtures/output/committees.json
327
- - spec/fixtures/output/districts.json
328
- - spec/fixtures/output/parliament_issues.json
329
- - spec/fixtures/output/parties.json
330
- - spec/fixtures/output/representatives.json
331
- - spec/fixtures/output/votes.json
332
311
  - spec/hdo/storting_importer/caching_data_source_spec.rb
333
312
  - spec/hdo/storting_importer/category_spec.rb
334
313
  - spec/hdo/storting_importer/committee_membership_spec.rb
335
314
  - spec/hdo/storting_importer/committee_spec.rb
336
- - spec/hdo/storting_importer/converter_spec.rb
337
315
  - spec/hdo/storting_importer/district_spec.rb
338
316
  - spec/hdo/storting_importer/parliament_issue_spec.rb
339
317
  - spec/hdo/storting_importer/party_membership_spec.rb
@@ -372,31 +350,10 @@ summary: Gem to process data from data.stortinget.no
372
350
  test_files:
373
351
  - features/convert.feature
374
352
  - features/support/env.rb
375
- - spec/fixtures/input/categories.xml
376
- - spec/fixtures/input/committees.xml
377
- - spec/fixtures/input/districts.xml
378
- - spec/fixtures/input/parliament_issues.xml
379
- - spec/fixtures/input/parties.xml
380
- - spec/fixtures/input/promises-a.csv
381
- - spec/fixtures/input/propositions_2175.xml
382
- - spec/fixtures/input/propositions_2176.xml
383
- - spec/fixtures/input/representatives.xml
384
- - spec/fixtures/input/representatives_today.xml
385
- - spec/fixtures/input/vote_results_2175.xml
386
- - spec/fixtures/input/vote_results_2176.xml
387
- - spec/fixtures/input/votes.xml
388
- - spec/fixtures/output/categories.json
389
- - spec/fixtures/output/committees.json
390
- - spec/fixtures/output/districts.json
391
- - spec/fixtures/output/parliament_issues.json
392
- - spec/fixtures/output/parties.json
393
- - spec/fixtures/output/representatives.json
394
- - spec/fixtures/output/votes.json
395
353
  - spec/hdo/storting_importer/caching_data_source_spec.rb
396
354
  - spec/hdo/storting_importer/category_spec.rb
397
355
  - spec/hdo/storting_importer/committee_membership_spec.rb
398
356
  - spec/hdo/storting_importer/committee_spec.rb
399
- - spec/hdo/storting_importer/converter_spec.rb
400
357
  - spec/hdo/storting_importer/district_spec.rb
401
358
  - spec/hdo/storting_importer/parliament_issue_spec.rb
402
359
  - spec/hdo/storting_importer/party_membership_spec.rb
@@ -1,40 +0,0 @@
1
- module Hdo
2
- module StortingImporter
3
- class Converter
4
-
5
- def initialize(data_source)
6
- @data_source = data_source
7
- @cache = {}
8
- end
9
-
10
- def json_for(name, opts = nil)
11
- obj = data_for(name)
12
-
13
- Yajl::Encoder.encode(obj, opts && opts[:pretty])
14
- end
15
-
16
- private
17
-
18
- def data_for(name)
19
- case name
20
- when :votes
21
- # not ideal
22
- issue_ids = fetch(:parliament_issues).map { |e| e.external_id }
23
- issue_ids.map { |id| @data_source.votes_for(id) }.flatten.uniq_by { |e| e.external_id }
24
- when :districts
25
- fetch(name).sort_by { |e| e.name }
26
- when :representatives
27
- data = fetch(:representatives_today) + fetch(:representatives)
28
- data.uniq_by { |e| e.external_id }.sort_by { |e| e.external_id }
29
- else
30
- fetch(name)
31
- end
32
- end
33
-
34
- def fetch(name)
35
- @cache[name] ||= @data_source.__send__(name)
36
- end
37
-
38
- end
39
- end
40
- end