hdo-storting-importer 0.0.4 → 0.0.5

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.
@@ -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
- @log.info "parsing #{sub_resource}"
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.xml_example(builder = Util.builder)
26
+ def self.example
26
27
  cat = new("5", "Employment")
27
28
  cat.children << new("6", "Wages")
28
29
 
29
- cat.to_hdo_xml(builder)
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
  #
@@ -2,7 +2,6 @@ module Hdo
2
2
  module StortingImporter
3
3
  class CLI
4
4
  def initialize(args)
5
- @log = Logger.new(STDERR)
6
5
  @type, @files, @options = parse(args)
7
6
  end
8
7
 
@@ -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
- new("ARBSOS", "Arbeids- og sosialkomiteen").to_hdo_xml(builder)
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
- @log.info "parsing #{subpath}"
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
- new("Db", "Duckburg").to_hdo_xml(builder)
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
- new("DEM", "Democratic Party").to_hdo_xml(builder)
22
+ example.to_hdo_xml(builder)
18
23
  end
19
24
 
20
25
  def self.fields
@@ -7,6 +7,7 @@ module Hdo
7
7
  class Promise
8
8
  attr_reader :party, :body, :general, :categories, :source, :page
9
9
  alias_method :general?, :general
10
+ alias_method :short_inspect, :inspect
10
11
 
11
12
  def self.type_name
12
13
  'promise'
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  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 < Struct.new(:external_id, :description, :on_behalf_of, :body, :delivered_by)
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'
@@ -94,6 +94,9 @@ module Hdo
94
94
  Category.fields.should_not be_empty
95
95
  end
96
96
 
97
+ it 'has #short_inspect' do
98
+ Category.example.short_inspect.should be_kind_of(String)
99
+ end
97
100
 
98
101
  end
99
102
  end
@@ -61,6 +61,9 @@ module Hdo
61
61
  Committee.fields.should_not be_empty
62
62
  end
63
63
 
64
+ it 'has #short_inspect' do
65
+ Committee.example.short_inspect.should be_kind_of(String)
66
+ end
64
67
 
65
68
  end
66
69
  end
@@ -69,6 +69,11 @@ module Hdo
69
69
  District.fields.should_not be_empty
70
70
  end
71
71
 
72
+ it 'has #short_inspect' do
73
+ District.example.short_inspect.should be_kind_of(String)
74
+ end
75
+
76
+
72
77
 
73
78
  end
74
79
  end
@@ -116,6 +116,10 @@ XML
116
116
  Issue.fields.should_not be_empty
117
117
  end
118
118
 
119
+ it 'has #short_inspect' do
120
+ Issue.example.short_inspect.should be_kind_of(String)
121
+ end
122
+
119
123
  end
120
124
  end
121
125
  end
@@ -72,6 +72,10 @@ module Hdo
72
72
  party.external_id.should == "ÆØÅ"
73
73
  end
74
74
 
75
+ it 'has #short_inspect' do
76
+ Party.example.short_inspect.should be_kind_of(String)
77
+ end
78
+
75
79
  end
76
80
  end
77
81
  end
@@ -89,6 +89,10 @@ XML
89
89
  Promise.xml_example.should be_kind_of(String)
90
90
  end
91
91
 
92
+ it 'has #short_inspect' do
93
+ Promise.example.short_inspect.should be_kind_of(String)
94
+ end
95
+
92
96
  end
93
97
  end
94
98
  end
@@ -100,6 +100,10 @@ module Hdo
100
100
  rep.external_id.should == "ÆØÅ"
101
101
  end
102
102
 
103
+ it 'has #short_inspect' do
104
+ Representative.example.short_inspect.should be_kind_of(String)
105
+ end
106
+
103
107
  end
104
108
  end
105
109
  end
@@ -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
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