hdo-storting-importer 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module Hdo
5
5
  class NotFoundError < StandardError
6
6
  end
7
7
 
8
- DEFAULT_SESSION = '2011-2012'
8
+ DEFAULT_SESSION = '2012-2013'
9
9
  DEFAULT_PERIOD = '2009-2013'
10
10
 
11
11
  private
@@ -0,0 +1,45 @@
1
+ module Hdo
2
+ module StortingImporter
3
+ class PartyMembership
4
+
5
+ include HasJsonSchema
6
+ include IvarEquality
7
+
8
+ schema_path StortingImporter.lib.join('hdo/storting_importer/schema/party_membership.json')
9
+
10
+ def self.example(overrides = nil)
11
+ obj = new 'A', Date.parse("2009-09-01"), Date.parse("2013-08-01")
12
+
13
+ if overrides
14
+ obj = from_hash(obj.to_hash.merge(overrides))
15
+ end
16
+
17
+ obj
18
+ end
19
+
20
+ def self.from_hash(hash)
21
+ new hash['externalId'],
22
+ (Date.parse(hash['startDate']) if hash['startDate']),
23
+ (Date.parse(hash['endDate']) if hash['endDate'])
24
+ end
25
+
26
+ attr_reader :external_id, :start_date, :end_date
27
+
28
+ def initialize(external_id, start_date, end_date)
29
+ @external_id = external_id
30
+ @start_date = start_date
31
+ @end_date = end_date
32
+ end
33
+
34
+ def to_hash
35
+ {
36
+ 'kind' => self.class.kind,
37
+ 'externalId' => external_id,
38
+ 'startDate' => (start_date.strftime("%Y-%m-%d") if start_date),
39
+ 'endDate' => (end_date.strftime("%Y-%m-%d") if end_date)
40
+ }
41
+ end
42
+
43
+ end # PartyMembership
44
+ end
45
+ end
@@ -35,11 +35,11 @@ module Hdo
35
35
  new(*arr)
36
36
  end
37
37
 
38
- def self.from_storting_doc(doc)
38
+ def self.from_storting_doc(doc, time)
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)
42
+ delivered_by = Representative.from_storting_node(rep_node, Util.session_for_date(time))
43
43
  else
44
44
  delivered_by = nil
45
45
  end
@@ -8,7 +8,7 @@ module Hdo
8
8
  include Inspectable
9
9
 
10
10
  attr_reader :external_id, :first_name, :last_name, :date_of_birth, :date_of_death,
11
- :district, :party, :committees, :period, :gender
11
+ :district, :parties, :committees, :period, :gender
12
12
 
13
13
  attr_accessor :vote_result
14
14
 
@@ -23,9 +23,8 @@ module Hdo
23
23
  '1975-07-07T00:00:00',
24
24
  '0001-01-01T00:00:00',
25
25
  'Akershus',
26
- 'Høyre',
26
+ [PartyMembership.from_hash("externalId" => 'H', 'startDate' => '2011-10-01', 'endDate' => nil)],
27
27
  ['Justiskomiteen'],
28
- '2011-2012'
29
28
  )
30
29
 
31
30
  if overrides
@@ -43,18 +42,27 @@ module Hdo
43
42
  nodes = doc.css("dagensrepresentant")
44
43
  nodes += doc.css("representant")
45
44
 
46
- nodes.map { |e| from_storting_node(e) }
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) }
47
49
  end
48
50
 
49
- def self.from_storting_node(node)
51
+ def self.from_storting_node(node, period = nil)
50
52
  district_node = node.css("fylke navn").first
51
53
  district = district_node ? district_node.text : ''
52
54
 
53
- party_node = node.css("parti navn").first
54
- party = party_node ? party_node.text : ''
55
+ party_node = node.css("parti id").first
56
+ if party_node
57
+ start_date = period ? period.begin : Util.current_session.begin
58
+ end_date = period ? period.end : nil
59
+
60
+ parties = [ PartyMembership.new(party_node.text, start_date, end_date) ]
61
+ else
62
+ parties = []
63
+ end
55
64
 
56
65
  committees = node.css("komite").map { |c| c.css("navn").text.strip }
57
- period = '2011-2012' # FIXME
58
66
 
59
67
  new(
60
68
  node.css("id").first.text,
@@ -64,9 +72,8 @@ module Hdo
64
72
  node.css("foedselsdato").first.text,
65
73
  node.css("doedsdato").first.text,
66
74
  district,
67
- party,
68
- committees,
69
- period
75
+ parties,
76
+ committees
70
77
  )
71
78
  end
72
79
 
@@ -78,16 +85,15 @@ module Hdo
78
85
  hash['dateOfBirth'],
79
86
  hash['dateOfDeath'],
80
87
  hash['district'],
81
- hash['party'],
82
- hash['committees'],
83
- hash['period']
88
+ Array(hash['parties']).map { |e| PartyMembership.from_hash(e) },
89
+ hash['committees']
84
90
 
85
91
  v.vote_result = hash['voteResult']
86
92
 
87
93
  v
88
94
  end
89
95
 
90
- def initialize(external_id, first_name, last_name, gender, date_of_birth, date_of_death, district, party, committees, period)
96
+ def initialize(external_id, first_name, last_name, gender, date_of_birth, date_of_death, district, parties, committees)
91
97
  @external_id = external_id
92
98
  @first_name = first_name
93
99
  @last_name = last_name
@@ -95,15 +101,14 @@ module Hdo
95
101
  @date_of_birth = date_of_birth
96
102
  @date_of_death = date_of_death
97
103
  @district = district
98
- @party = party
104
+ @parties = parties
99
105
  @committees = committees
100
- @period = period
101
106
 
102
107
  @vote_result = nil
103
108
  end
104
109
 
105
110
  def short_inspect
106
- short_inspect_string :include => [:external_id, :first_name, :last_name, :party, :vote_result]
111
+ short_inspect_string :include => [:external_id, :first_name, :last_name, :parties, :vote_result]
107
112
  end
108
113
 
109
114
  def external_id
@@ -120,9 +125,8 @@ module Hdo
120
125
  'dateOfBirth' => @date_of_birth,
121
126
  'dateOfDeath' => @date_of_death,
122
127
  'district' => @district,
123
- 'party' => @party,
124
- 'committees' => @committees,
125
- 'period' => @period
128
+ 'parties' => @parties.map { |e| e.to_hash },
129
+ 'committees' => @committees
126
130
  }
127
131
 
128
132
  h['voteResult'] = @vote_result if @vote_result
@@ -0,0 +1,29 @@
1
+ {
2
+ "id": "hdo#partyMembership",
3
+ "description": "a representative's party association for the given dates",
4
+ "type":"object",
5
+ "properties": {
6
+ "kind": {
7
+ "type": "string",
8
+ "description": "This is always 'hdo#partyMembership'",
9
+ "default": "hdo#partyMembership",
10
+ "required": true
11
+ },
12
+ "externalId": {
13
+ "type": "string",
14
+ "description": "An ID uniquely identifying the party (matching externalId of hdo#party)",
15
+ "required": true
16
+ },
17
+ "startDate": {
18
+ "type": "string",
19
+ "description": "The date the association started.",
20
+ "format": "date",
21
+ "required": true
22
+ },
23
+ "endDate": {
24
+ "type": "string",
25
+ "description": "The date the association ended. Can be null if the association is current.",
26
+ "format": "date",
27
+ "required": false
28
+ }
29
+ }
@@ -22,25 +22,21 @@
22
22
  "description": "The first name of the representative",
23
23
  "required": true
24
24
  },
25
- "period": {
26
- "type": "string",
27
- "description": "An identifier for the period the representative is elected for.",
28
- "required": true
29
- },
30
25
  "district": {
31
26
  "type": "string",
32
27
  "description": "The electoral district the representative belongs to. Must match the 'name' field of the district type.",
33
28
  "required": true
34
29
  },
35
- "party": {
36
- "type": "string",
37
- "description": "The name of the representative's party.",
30
+ "parties": {
31
+ "type": "array",
32
+ "description": "A list of party memberships for this representative. Note that the import code will assume the field is incomplete, and handle this as an append to the current data unless the start date matches.",
33
+ "items": { "$ref": "hdo#partyMembership"},
38
34
  "required": true
39
35
  },
40
36
  "committees": {
41
37
  "type": "array",
42
38
  "description": "A list of committees the representative is a member of. This should match the 'name' field of the committee type.",
43
- "items": { "type": "string"},
39
+ "items": { "type": "string" },
44
40
  "required": true
45
41
  },
46
42
  "dateOfBirth": {
@@ -29,6 +29,40 @@ module Hdo
29
29
  q
30
30
  end
31
31
 
32
+ def period_to_date_range(str)
33
+ start_year, end_year = str.split("-").map do |y|
34
+ case y.size
35
+ when 4
36
+ Integer(y)
37
+ when 2
38
+ Integer("19#{y}")
39
+ else
40
+ "couldn't parse year #{str.inspect}"
41
+ end
42
+ end
43
+
44
+ if start_year > end_year
45
+ raise "invalid period: #{str.inspect}"
46
+ end
47
+
48
+ # approximate dates
49
+ Date.new(start_year, 10, 1)..Date.new(end_year, 9, 30)
50
+ end
51
+
52
+ def current_session
53
+ session_for_date Date.today
54
+ end
55
+
56
+ def session_for_date(date)
57
+ new_session_start = Date.new(date.year, 10, 1)
58
+
59
+ if date >= new_session_start
60
+ new_session_start..(Date.new(date.year + 1, 9, 30))
61
+ else
62
+ Date.new(date.year - 1, 10, 1)..Date.new(date.year, 9, 30)
63
+ end
64
+ end
65
+
32
66
  def json_pretty(obj)
33
67
  case obj
34
68
  when Array
@@ -1,5 +1,5 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
- VERSION = "0.2.2"
3
+ VERSION = "0.2.3"
4
4
  end
5
5
  end
@@ -107,7 +107,7 @@ module Hdo
107
107
  end
108
108
 
109
109
  def add_storting_propositions(node)
110
- @propositions += Proposition.from_storting_doc(node)
110
+ @propositions += Proposition.from_storting_doc(node, Date.parse(time))
111
111
  end
112
112
 
113
113
  def add_storting_results(node)
@@ -125,6 +125,7 @@ require 'hdo/storting_importer/committee'
125
125
  require 'hdo/storting_importer/district'
126
126
  require 'hdo/storting_importer/parliament_issue'
127
127
  require 'hdo/storting_importer/party'
128
+ require 'hdo/storting_importer/party_membership'
128
129
  require 'hdo/storting_importer/promise'
129
130
  require 'hdo/storting_importer/representative'
130
131
  require 'hdo/storting_importer/proposition'