hdo-storting-importer 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hdo/storting_importer/api_data_source.rb +1 -2
- data/lib/hdo/storting_importer/category.rb +11 -2
- data/lib/hdo/storting_importer/cli.rb +0 -1
- data/lib/hdo/storting_importer/committee.rb +6 -1
- data/lib/hdo/storting_importer/disk_data_source.rb +1 -2
- data/lib/hdo/storting_importer/district.rb +6 -1
- data/lib/hdo/storting_importer/inspectable.rb +27 -0
- data/lib/hdo/storting_importer/issue.rb +5 -0
- data/lib/hdo/storting_importer/party.rb +6 -1
- data/lib/hdo/storting_importer/promise.rb +1 -0
- data/lib/hdo/storting_importer/representative.rb +5 -0
- data/lib/hdo/storting_importer/version.rb +1 -1
- data/lib/hdo/storting_importer/vote.rb +23 -1
- data/lib/hdo/storting_importer.rb +28 -8
- data/spec/hdo/storting_importer/category_spec.rb +3 -0
- data/spec/hdo/storting_importer/committee_spec.rb +3 -0
- data/spec/hdo/storting_importer/district_spec.rb +5 -0
- data/spec/hdo/storting_importer/issue_spec.rb +4 -0
- data/spec/hdo/storting_importer/party_spec.rb +4 -0
- data/spec/hdo/storting_importer/promise_spec.rb +4 -0
- data/spec/hdo/storting_importer/representative_spec.rb +4 -0
- data/spec/hdo/storting_importer/vote_spec.rb +8 -0
- metadata +2 -1
@@ -3,7 +3,6 @@ module Hdo
|
|
3
3
|
class ApiDataSource < DataSource
|
4
4
|
|
5
5
|
def initialize(url)
|
6
|
-
@log = Logger.new(STDERR)
|
7
6
|
@resource = RestClient::Resource.new(URI.parse(url))
|
8
7
|
end
|
9
8
|
|
@@ -51,7 +50,7 @@ module Hdo
|
|
51
50
|
|
52
51
|
def fetch(path)
|
53
52
|
sub_resource = @resource[path]
|
54
|
-
|
53
|
+
Hdo::StortingImporter.logger.info "parsing #{sub_resource}"
|
55
54
|
|
56
55
|
parse sub_resource.get
|
57
56
|
end
|
@@ -2,6 +2,7 @@ module Hdo
|
|
2
2
|
module StortingImporter
|
3
3
|
class Category
|
4
4
|
include IvarEquality
|
5
|
+
include Inspectable
|
5
6
|
|
6
7
|
attr_reader :external_id, :name
|
7
8
|
attr_accessor :children
|
@@ -22,11 +23,15 @@ module Hdo
|
|
22
23
|
]
|
23
24
|
end
|
24
25
|
|
25
|
-
def self.
|
26
|
+
def self.example
|
26
27
|
cat = new("5", "Employment")
|
27
28
|
cat.children << new("6", "Wages")
|
28
29
|
|
29
|
-
cat
|
30
|
+
cat
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.xml_example(builder = Util.builder)
|
34
|
+
example.to_hdo_xml(builder)
|
30
35
|
end
|
31
36
|
|
32
37
|
#
|
@@ -90,6 +95,10 @@ module Hdo
|
|
90
95
|
@children = []
|
91
96
|
end
|
92
97
|
|
98
|
+
def short_inspect
|
99
|
+
short_inspect_string :include => [:external_id, :name]
|
100
|
+
end
|
101
|
+
|
93
102
|
#
|
94
103
|
# Serialize as HDO XML
|
95
104
|
#
|
@@ -4,6 +4,7 @@ module Hdo
|
|
4
4
|
include IvarEquality
|
5
5
|
|
6
6
|
attr_reader :external_id, :name
|
7
|
+
alias_method :short_inspect, :inspect
|
7
8
|
|
8
9
|
def self.type_name
|
9
10
|
'committee'
|
@@ -13,8 +14,12 @@ module Hdo
|
|
13
14
|
'a parliamentary committe'
|
14
15
|
end
|
15
16
|
|
17
|
+
def self.example
|
18
|
+
new "ARBSOS", "Arbeids- og sosialkomiteen"
|
19
|
+
end
|
20
|
+
|
16
21
|
def self.xml_example(builder = Util.builder)
|
17
|
-
|
22
|
+
example.to_hdo_xml(builder)
|
18
23
|
end
|
19
24
|
|
20
25
|
def self.fields
|
@@ -4,7 +4,6 @@ module Hdo
|
|
4
4
|
|
5
5
|
def initialize(root)
|
6
6
|
@root = Pathname.new(root)
|
7
|
-
@log = Logger.new(STDERR)
|
8
7
|
end
|
9
8
|
|
10
9
|
def parties(session_id = DEFAULT_SESSION)
|
@@ -51,7 +50,7 @@ module Hdo
|
|
51
50
|
|
52
51
|
def fetch(path)
|
53
52
|
subpath = @root.join(path)
|
54
|
-
|
53
|
+
Hdo::StortingImporter.logger.info "parsing #{subpath}"
|
55
54
|
|
56
55
|
parse subpath.read
|
57
56
|
end
|
@@ -4,6 +4,7 @@ module Hdo
|
|
4
4
|
include IvarEquality
|
5
5
|
|
6
6
|
attr_reader :external_id, :name
|
7
|
+
alias_method :short_inspect, :inspect
|
7
8
|
|
8
9
|
def self.type_name
|
9
10
|
'district'
|
@@ -13,8 +14,12 @@ module Hdo
|
|
13
14
|
'an electoral district'
|
14
15
|
end
|
15
16
|
|
17
|
+
def self.example
|
18
|
+
new("Db", "Duckburg")
|
19
|
+
end
|
20
|
+
|
16
21
|
def self.xml_example(builder = Util.builder)
|
17
|
-
|
22
|
+
example.to_hdo_xml(builder)
|
18
23
|
end
|
19
24
|
|
20
25
|
def self.fields
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Hdo
|
2
|
+
module StortingImporter
|
3
|
+
module Inspectable
|
4
|
+
def short_inspect_string(opts)
|
5
|
+
if excluded_ivars = opts[:exclude]
|
6
|
+
excluded_ivars.map! { |ivar| ivar.to_sym }
|
7
|
+
all_ivars = instance_variables.map { |e| e.to_sym }
|
8
|
+
ivars = all_ivars - excluded_ivars
|
9
|
+
elsif included_ivars = opts[:include]
|
10
|
+
included_ivars.map! { |ivar| "@#{ivar}".to_sym }
|
11
|
+
ivars = included_ivars
|
12
|
+
else
|
13
|
+
raise ArgumentError, "unknown arg: #{opts.inspect}"
|
14
|
+
end
|
15
|
+
|
16
|
+
ivars.map! do |ivar|
|
17
|
+
val = instance_variable_get(ivar)
|
18
|
+
str = val.respond_to?(:short_inspect) ? val.short_inspect : val.inspect
|
19
|
+
|
20
|
+
"#{ivar}=#{str}"
|
21
|
+
end
|
22
|
+
|
23
|
+
'#<%s:0x%s %s>' % [self.class.name, self.hash.to_s(16), ivars.join(" ").strip]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -4,6 +4,7 @@ module Hdo
|
|
4
4
|
module StortingImporter
|
5
5
|
class Issue
|
6
6
|
include IvarEquality
|
7
|
+
include Inspectable
|
7
8
|
|
8
9
|
attr_reader :external_id, :summary, :description, :type, :status, :last_update,
|
9
10
|
:reference, :document_group, :committee, :categories
|
@@ -115,6 +116,10 @@ module Hdo
|
|
115
116
|
@categories = categories || []
|
116
117
|
end
|
117
118
|
|
119
|
+
def short_inspect
|
120
|
+
short_inspect_string :include => [:external_id, :summary]
|
121
|
+
end
|
122
|
+
|
118
123
|
def to_hdo_xml(builder = Util.builder)
|
119
124
|
builder.issue do |i|
|
120
125
|
i.externalId external_id
|
@@ -4,6 +4,7 @@ module Hdo
|
|
4
4
|
include IvarEquality
|
5
5
|
|
6
6
|
attr_reader :external_id, :name
|
7
|
+
alias_method :short_inspect, :inspect
|
7
8
|
|
8
9
|
def self.type_name
|
9
10
|
'party'
|
@@ -13,8 +14,12 @@ module Hdo
|
|
13
14
|
'a political party'
|
14
15
|
end
|
15
16
|
|
17
|
+
def self.example
|
18
|
+
new("DEM", "Democratic Party")
|
19
|
+
end
|
20
|
+
|
16
21
|
def self.xml_example(builder = Util.builder)
|
17
|
-
|
22
|
+
example.to_hdo_xml(builder)
|
18
23
|
end
|
19
24
|
|
20
25
|
def self.fields
|
@@ -4,6 +4,7 @@ module Hdo
|
|
4
4
|
module StortingImporter
|
5
5
|
class Representative
|
6
6
|
include IvarEquality
|
7
|
+
include Inspectable
|
7
8
|
|
8
9
|
attr_reader :external_id, :first_name, :last_name, :date_of_birth, :date_of_death,
|
9
10
|
:district, :party, :committees, :period, :gender
|
@@ -125,6 +126,10 @@ module Hdo
|
|
125
126
|
@vote_result = nil
|
126
127
|
end
|
127
128
|
|
129
|
+
def short_inspect
|
130
|
+
short_inspect_string :include => [:external_id, :first_name, :last_name, :party, :vote_result]
|
131
|
+
end
|
132
|
+
|
128
133
|
def external_id
|
129
134
|
Util.unescape_param @external_id
|
130
135
|
end
|
@@ -4,6 +4,7 @@ module Hdo
|
|
4
4
|
module StortingImporter
|
5
5
|
class Vote
|
6
6
|
include IvarEquality
|
7
|
+
include Inspectable
|
7
8
|
|
8
9
|
attr_reader :external_id, :external_issue_id, :personal, :enacted, :subject,
|
9
10
|
:method, :result_type, :time, :counts
|
@@ -117,6 +118,10 @@ module Hdo
|
|
117
118
|
@representatives = []
|
118
119
|
end
|
119
120
|
|
121
|
+
def short_inspect
|
122
|
+
short_inspect_string :include => [:external_id, :subject, :time, :counts]
|
123
|
+
end
|
124
|
+
|
120
125
|
def add_storting_propositions(node)
|
121
126
|
@propositions += node.css("voteringsforslag").map do |n|
|
122
127
|
rep_node = n.css("forslag_levert_av_representant").first
|
@@ -186,7 +191,12 @@ module Hdo
|
|
186
191
|
class Counts < Struct.new(:for, :against, :absent)
|
187
192
|
end
|
188
193
|
|
189
|
-
class Proposition
|
194
|
+
class Proposition
|
195
|
+
include IvarEquality
|
196
|
+
include Inspectable
|
197
|
+
|
198
|
+
attr_reader :external_id, :description, :on_behalf_of, :body, :delivered_by
|
199
|
+
|
190
200
|
def self.type_name
|
191
201
|
'proposition'
|
192
202
|
end
|
@@ -225,6 +235,18 @@ module Hdo
|
|
225
235
|
new external_id, description, on_behalf_of, body, delivered_by
|
226
236
|
end
|
227
237
|
|
238
|
+
def initialize(external_id, description, on_behalf_of, body, delivered_by)
|
239
|
+
@external_id = external_id
|
240
|
+
@description = description
|
241
|
+
@on_behalf_of = on_behalf_of
|
242
|
+
@body = body
|
243
|
+
@delivered_by = delivered_by
|
244
|
+
end
|
245
|
+
|
246
|
+
def short_inspect
|
247
|
+
short_inspect_string :include => [:external_id, :description, :on_behalf_of]
|
248
|
+
end
|
249
|
+
|
228
250
|
def to_hdo_xml(builder)
|
229
251
|
builder.proposition do |pr|
|
230
252
|
pr.externalId external_id
|
@@ -1,24 +1,44 @@
|
|
1
|
+
require 'nokogiri'
|
2
|
+
require 'restclient'
|
3
|
+
require 'tempfile'
|
4
|
+
require 'pathname'
|
5
|
+
require 'open-uri'
|
6
|
+
require 'erb'
|
7
|
+
require 'logger'
|
8
|
+
|
1
9
|
module Hdo
|
2
10
|
module StortingImporter
|
3
11
|
def self.root
|
4
12
|
@root ||= File.expand_path("../../..", __FILE__)
|
5
13
|
end
|
6
14
|
|
15
|
+
def self.logger
|
16
|
+
@logger ||= (
|
17
|
+
out = $stderr # should be able to pipe output to a file
|
18
|
+
|
19
|
+
if defined?(Rails)
|
20
|
+
klass = Class.new(Logger) do
|
21
|
+
def format_message(severity, timestamp, progname, msg)
|
22
|
+
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
klass.new(out)
|
27
|
+
else
|
28
|
+
Logger.new(out)
|
29
|
+
end
|
30
|
+
)
|
31
|
+
|
32
|
+
end
|
33
|
+
|
7
34
|
Field = Struct.new(:name, :required, :type, :description)
|
8
35
|
EXTERNAL_ID_FIELD = Field.new(:externalId, false, :string, 'An optional external id, matching potential id fields in the input data. This is useful if you want to reimport previous data without creating duplicates.')
|
9
36
|
end
|
10
37
|
end
|
11
38
|
|
12
|
-
require 'nokogiri'
|
13
|
-
require 'restclient'
|
14
|
-
require 'tempfile'
|
15
|
-
require 'pathname'
|
16
|
-
require 'open-uri'
|
17
|
-
require 'erb'
|
18
|
-
require 'logger'
|
19
|
-
|
20
39
|
require 'hdo/storting_importer/core_ext/enumerable'
|
21
40
|
require 'hdo/storting_importer/ivar_equality'
|
41
|
+
require 'hdo/storting_importer/inspectable'
|
22
42
|
require 'hdo/storting_importer/util'
|
23
43
|
|
24
44
|
require 'hdo/storting_importer/data_source'
|
@@ -162,6 +162,14 @@ module Hdo
|
|
162
162
|
Vote.fields.should_not be_empty
|
163
163
|
end
|
164
164
|
|
165
|
+
it 'has #short_inspect' do
|
166
|
+
Vote.example.short_inspect.should be_kind_of(String)
|
167
|
+
|
168
|
+
str = Vote::Proposition.example.short_inspect
|
169
|
+
str.should be_kind_of(String)
|
170
|
+
str.should_not include("nil")
|
171
|
+
end
|
172
|
+
|
165
173
|
end
|
166
174
|
end
|
167
175
|
end
|
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.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/hdo/storting_importer/data_source.rb
|
121
121
|
- lib/hdo/storting_importer/disk_data_source.rb
|
122
122
|
- lib/hdo/storting_importer/district.rb
|
123
|
+
- lib/hdo/storting_importer/inspectable.rb
|
123
124
|
- lib/hdo/storting_importer/issue.rb
|
124
125
|
- lib/hdo/storting_importer/ivar_equality.rb
|
125
126
|
- lib/hdo/storting_importer/parsing_data_source.rb
|