hdo-storting-importer 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Guardfile ADDED
@@ -0,0 +1,7 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+ guard 'rspec', :version => 2 do
4
+ watch(%r{^spec/.+_spec\.rb$})
5
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
6
+ watch('spec/spec_helper.rb') { "spec" }
7
+ end
@@ -26,5 +26,7 @@ Gem::Specification.new do |gem|
26
26
  gem.add_runtime_dependency "jschematic", ">= 0.1.0"
27
27
 
28
28
  gem.add_development_dependency 'pry'
29
+ gem.add_development_dependency 'guard'
30
+ gem.add_development_dependency 'guard-rspec'
29
31
  gem.add_development_dependency 'rspec'
30
32
  end
@@ -17,10 +17,6 @@ module Hdo
17
17
  cat
18
18
  end
19
19
 
20
- def self.json_example
21
- Util.json_pretty example
22
- end
23
-
24
20
  #
25
21
  # Deserialize from a Storting XML document (<emne_liste><emne>...</emne></emne_liste>)
26
22
  #
@@ -67,12 +63,12 @@ module Hdo
67
63
 
68
64
  def to_hash
69
65
  h = {
70
- :kind => self.class.kind,
71
- :externalId => @external_id,
72
- :name => @name
66
+ 'kind' => self.class.kind,
67
+ 'externalId' => @external_id,
68
+ 'name' => @name
73
69
  }
74
70
 
75
- h[:subCategories] = @children.map { |e| e.to_hash } if @children.any?
71
+ h['subCategories'] = @children.map { |e| e.to_hash } if @children.any?
76
72
 
77
73
  h
78
74
  end
@@ -13,10 +13,6 @@ module Hdo
13
13
  new "ARBSOS", "Arbeids- og sosialkomiteen"
14
14
  end
15
15
 
16
- def self.json_example
17
- Util.json_pretty example
18
- end
19
-
20
16
  def self.from_storting_doc(doc)
21
17
  doc.css("komiteer_liste komite").map do |node|
22
18
  from_storting_node(node)
@@ -24,7 +20,8 @@ module Hdo
24
20
  end
25
21
 
26
22
  def self.from_storting_node(node)
27
- new node.css("id").first.text, node.css("navn").first.text
23
+ new node.css("id").first.text,
24
+ node.css("navn").first.text
28
25
  end
29
26
 
30
27
  def self.from_hash(hash)
@@ -33,14 +30,14 @@ module Hdo
33
30
 
34
31
  def initialize(external_id, name)
35
32
  @external_id = external_id
36
- @name = name
33
+ @name = name
37
34
  end
38
35
 
39
36
  def to_hash
40
37
  {
41
- :kind => self.class.kind,
42
- :externalId => @external_id,
43
- :name => @name
38
+ 'kind' => self.class.kind,
39
+ 'externalId' => @external_id,
40
+ 'name' => @name
44
41
  }
45
42
  end
46
43
 
@@ -13,10 +13,6 @@ module Hdo
13
13
  new("Db", "Duckburg")
14
14
  end
15
15
 
16
- def self.json_example
17
- Util.json_pretty example
18
- end
19
-
20
16
  def self.from_storting_doc(doc)
21
17
  doc.css("fylker_liste fylke").map do |node|
22
18
  from_storting_node(node)
@@ -24,23 +20,24 @@ module Hdo
24
20
  end
25
21
 
26
22
  def self.from_storting_node(node)
27
- new node.css("id").first.text, node.css("navn").first.text
23
+ new node.css("id").first.text,
24
+ node.css("navn").first.text
28
25
  end
29
26
 
30
27
  def self.from_hash(hash)
31
- new hash.fetch('externalId'), hash.fetch('name')
28
+ new hash['externalId'], hash['name']
32
29
  end
33
30
 
34
31
  def initialize(external_id, name)
35
32
  @external_id = external_id
36
- @name = name
33
+ @name = name
37
34
  end
38
35
 
39
36
  def to_hash
40
37
  {
41
- :kind => self.class.kind,
42
- :externalId => @external_id,
43
- :name => @name
38
+ 'kind' => self.class.kind,
39
+ 'externalId' => @external_id,
40
+ 'name' => @name
44
41
  }
45
42
  end
46
43
 
@@ -7,6 +7,7 @@ module Hdo
7
7
  # Includer must define these methods:
8
8
  #
9
9
  # .schema_path
10
+ # .example
10
11
  # #from_hash(hash)
11
12
  # #to_hash
12
13
  #
@@ -28,6 +29,14 @@ module Hdo
28
29
  to_hash
29
30
  end
30
31
 
32
+ def valid?
33
+ self.class.valid? to_hash
34
+ end
35
+
36
+ def validate!
37
+ self.class.validate! to_hash
38
+ end
39
+
31
40
  module ClassMethods
32
41
  attr_reader :schema
33
42
 
@@ -40,6 +49,10 @@ module Hdo
40
49
  @schema or raise "schema must be set with #{self}.schema_path"
41
50
  end
42
51
 
52
+ def json_example
53
+ @json_example ||= Util.json_pretty example
54
+ end
55
+
43
56
  def kind
44
57
  @kind ||= schema['properties']['kind']['default']
45
58
  end
@@ -72,6 +85,10 @@ module Hdo
72
85
  end
73
86
  end
74
87
 
88
+ def valid?(e)
89
+ Jschematic.validate(e, schema, :context => HasJsonSchema.schemas, :debug => true)
90
+ end
91
+
75
92
  def validate!(e)
76
93
  Jschematic.validate!(e, schema, :context => HasJsonSchema.schemas, :debug => true)
77
94
  e
@@ -58,22 +58,19 @@ module Hdo
58
58
  end
59
59
 
60
60
  new(external_id, summary, description, type, status, last_update, reference, document_group, committee, categories)
61
- rescue
62
- puts lnode
63
- raise
64
61
  end
65
62
 
66
63
  def self.from_hash(hash)
67
- new hash.fetch('externalId'),
68
- hash.fetch('summary'),
69
- hash.fetch('description'),
70
- hash.fetch('type'),
71
- hash.fetch('status'),
72
- hash.fetch('lastUpdate'),
73
- hash.fetch('reference'),
74
- hash.fetch('documentGroup'),
75
- hash.fetch('committee'),
76
- hash.fetch('categories')
64
+ new hash['externalId'],
65
+ hash['summary'],
66
+ hash['description'],
67
+ hash['type'],
68
+ hash['status'],
69
+ hash['lastUpdate'],
70
+ hash['reference'],
71
+ hash['documentGroup'],
72
+ hash['committee'],
73
+ hash['categories']
77
74
  end
78
75
 
79
76
  def initialize(external_id, summary, description, type, status, last_update,
@@ -96,17 +93,17 @@ module Hdo
96
93
 
97
94
  def to_hash
98
95
  {
99
- :kind => self.class.kind,
100
- :externalId => @external_id,
101
- :summary => @summary,
102
- :description => @description,
103
- :type => @type,
104
- :status => @status,
105
- :lastUpdate => @last_update,
106
- :reference => @reference,
107
- :documentGroup => @document_group,
108
- :committee => @committee,
109
- :categories => @categories
96
+ 'kind' => self.class.kind,
97
+ 'externalId' => @external_id,
98
+ 'summary' => @summary,
99
+ 'description' => @description,
100
+ 'type' => @type,
101
+ 'status' => @status,
102
+ 'lastUpdate' => @last_update,
103
+ 'reference' => @reference,
104
+ 'documentGroup' => @document_group,
105
+ 'committee' => @committee,
106
+ 'categories' => @categories
110
107
  }
111
108
  end
112
109
 
@@ -13,18 +13,16 @@ module Hdo
13
13
  new("DEM", "Democratic Party")
14
14
  end
15
15
 
16
- def self.json_example
17
- Util.json_pretty example
18
- end
19
-
20
16
  def self.from_storting_doc(doc)
21
17
  doc.css("partier_liste parti").map do |node|
22
- new node.css("id").first.text, node.css("navn").first.text
18
+ new node.css("id").first.text,
19
+ node.css("navn").first.text
23
20
  end
24
21
  end
25
22
 
26
23
  def self.from_hash(hash)
27
- new hash.fetch('externalId'), hash.fetch('name')
24
+ new hash['externalId'],
25
+ hash['name']
28
26
  end
29
27
 
30
28
  def initialize(external_id, name)
@@ -38,9 +36,9 @@ module Hdo
38
36
 
39
37
  def to_hash
40
38
  {
41
- :kind => self.class.kind,
42
- :externalId => @external_id,
43
- :name => @name
39
+ 'kind' => self.class.kind,
40
+ 'externalId' => @external_id,
41
+ 'name' => @name
44
42
  }
45
43
  end
46
44
 
@@ -75,12 +75,12 @@ module Hdo
75
75
  end
76
76
 
77
77
  def self.from_hash(hash)
78
- pr = new hash.fetch('party'),
79
- hash.fetch('body'),
80
- hash.fetch('general'),
81
- hash.fetch('categories'),
82
- hash.fetch('source'),
83
- hash.fetch('page')
78
+ pr = new hash['party'],
79
+ hash['body'],
80
+ hash['general'],
81
+ hash['categories'],
82
+ hash['source'],
83
+ hash['page']
84
84
 
85
85
  pr.external_id = hash['externalId']
86
86
 
@@ -88,11 +88,11 @@ module Hdo
88
88
  end
89
89
 
90
90
  def initialize(party, body, general, categories, source, page)
91
- @party = party.strip
92
- @body = body.strip
91
+ @party = strip_if_string(party)
92
+ @body = strip_if_string(body)
93
93
  @general = general
94
94
  @categories = clean_categories(categories)
95
- @source = source.strip
95
+ @source = strip_if_string(source)
96
96
  @page = page
97
97
 
98
98
  @external_id = nil
@@ -100,16 +100,16 @@ module Hdo
100
100
 
101
101
  def to_hash
102
102
  h = {
103
- :kind => self.class.kind,
104
- :party => @party,
105
- :general => @general,
106
- :categories => @categories,
107
- :source => @source,
108
- :page => @page,
109
- :body => @body
103
+ 'kind' => self.class.kind,
104
+ 'party' => @party,
105
+ 'general' => @general,
106
+ 'categories' => @categories,
107
+ 'source' => @source,
108
+ 'page' => @page,
109
+ 'body' => @body
110
110
  }
111
111
 
112
- h[:externalId] = @external_id if @external_id
112
+ h["externalId"] = @external_id if @external_id
113
113
 
114
114
  h
115
115
  end
@@ -119,9 +119,17 @@ module Hdo
119
119
  def clean_categories(categories)
120
120
  categories = categories.split(",") if categories.kind_of?(String)
121
121
 
122
- categories.map(&:strip).
123
- reject(&:empty?).
124
- map { |e| UnicodeUtils.upcase(e) }
122
+ Array(categories).map(&:strip).
123
+ reject(&:empty?).
124
+ map { |e| UnicodeUtils.upcase(e) }
125
+ end
126
+
127
+ def strip_if_string(str)
128
+ if str.respond_to? :strip
129
+ str.strip
130
+ else
131
+ str
132
+ end
125
133
  end
126
134
 
127
135
  end
@@ -19,15 +19,13 @@ module Hdo
19
19
 
20
20
  def self.from_hash(hash)
21
21
  arr = [
22
- hash.fetch('externalId'),
23
- hash.fetch('description'),
24
- hash.fetch('onBehalfOf'),
25
- hash.fetch('body')
22
+ hash['externalId'],
23
+ hash['description'],
24
+ hash['onBehalfOf'],
25
+ hash['body'],
26
+ Representative.from_hash(hash['deliveredBy'] || {})
26
27
  ]
27
28
 
28
- delivered_by = hash['deliveredBy']
29
- arr << Representative.from_hash(delivered_by) if delivered_by
30
-
31
29
  new(*arr)
32
30
  end
33
31
 
@@ -45,14 +43,14 @@ module Hdo
45
43
 
46
44
  def to_hash
47
45
  h = {
48
- :kind => self.class.kind,
49
- :externalId => @external_id,
50
- :description => @description,
51
- :onBehalfOf => @on_behalf_of,
52
- :body => @body
46
+ 'kind' => self.class.kind,
47
+ 'externalId' => @external_id,
48
+ 'description' => @description,
49
+ 'onBehalfOf' => @on_behalf_of,
50
+ 'body' => @body
53
51
  }
54
52
 
55
- h[:deliveredBy] = @delivered_by.to_hash if @delivered_by
53
+ h['deliveredBy'] = @delivered_by.to_hash if @delivered_by
56
54
 
57
55
  h
58
56
  end
@@ -65,16 +65,16 @@ module Hdo
65
65
  end
66
66
 
67
67
  def self.from_hash(hash)
68
- v = new hash.fetch('externalId'),
69
- hash.fetch('firstName'),
70
- hash.fetch('lastName'),
71
- hash.fetch('gender'),
72
- hash.fetch('dateOfBirth'),
73
- hash.fetch('dateOfDeath'),
74
- hash.fetch('district'),
75
- hash.fetch('party'),
76
- hash.fetch('committees'),
77
- hash.fetch('period')
68
+ v = new hash['externalId'],
69
+ hash['firstName'],
70
+ hash['lastName'],
71
+ hash['gender'],
72
+ hash['dateOfBirth'],
73
+ hash['dateOfDeath'],
74
+ hash['district'],
75
+ hash['party'],
76
+ hash['committees'],
77
+ hash['period']
78
78
 
79
79
  v.vote_result = hash['voteResult']
80
80
 
@@ -106,20 +106,20 @@ module Hdo
106
106
 
107
107
  def to_hash
108
108
  h = {
109
- :kind => self.class.kind,
110
- :externalId => @external_id,
111
- :firstName => @first_name,
112
- :lastName => @last_name,
113
- :gender => @gender,
114
- :dateOfBirth => @date_of_birth,
115
- :dateOfDeath => @date_of_death,
116
- :district => @district,
117
- :party => @party,
118
- :committees => @committees,
119
- :period => @period
109
+ 'kind' => self.class.kind,
110
+ 'externalId' => @external_id,
111
+ 'firstName' => @first_name,
112
+ 'lastName' => @last_name,
113
+ 'gender' => @gender,
114
+ 'dateOfBirth' => @date_of_birth,
115
+ 'dateOfDeath' => @date_of_death,
116
+ 'district' => @district,
117
+ 'party' => @party,
118
+ 'committees' => @committees,
119
+ 'period' => @period
120
120
  }
121
121
 
122
- h[:voteResult] = @vote_result if @vote_result
122
+ h['voteResult'] = @vote_result if @vote_result
123
123
 
124
124
  h
125
125
  end
@@ -20,6 +20,8 @@ module Hdo
20
20
  }
21
21
 
22
22
  def unescape_param(query_param)
23
+ return unless query_param.kind_of?(String)
24
+
23
25
  q = query_param.dup
24
26
  ID_CONVERSIONS.each { |k, v| q.gsub!(k, v) }
25
27
 
@@ -1,5 +1,5 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
- VERSION = "0.0.9"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -60,25 +60,25 @@ module Hdo
60
60
  end
61
61
 
62
62
  def self.from_hash(hash)
63
- counts = hash.fetch("counts")
63
+ counts = hash['counts']
64
64
 
65
65
  args = [
66
- hash.fetch("externalId"),
67
- hash.fetch("externalIssueId"),
68
- hash.fetch("personal"),
69
- hash.fetch("enacted"),
70
- hash.fetch("subject"),
71
- hash.fetch("method"),
72
- hash.fetch("resultType"),
73
- hash.fetch("time"),
74
- counts.fetch("for"),
75
- counts.fetch("against"),
76
- counts.fetch("absent")
66
+ hash["externalId"],
67
+ hash["externalIssueId"],
68
+ hash["personal"],
69
+ hash["enacted"],
70
+ hash["subject"],
71
+ hash["method"],
72
+ hash["resultType"],
73
+ hash["time"],
74
+ counts && counts["for"],
75
+ counts && counts["against"],
76
+ counts && counts["absent"],
77
77
  ]
78
78
 
79
79
  vote = new(*args)
80
- vote.representatives = hash.fetch('representatives').map { |e| Representative.from_hash(e) }
81
- vote.propositions = hash.fetch('propositions').map { |e| Proposition.from_hash(e) }
80
+ vote.representatives = Array(hash['representatives']).map { |e| Representative.from_hash(e || {}) }
81
+ vote.propositions = Array(hash['propositions']).map { |e| Proposition.from_hash(e || {}) }
82
82
 
83
83
  vote
84
84
  end
@@ -92,7 +92,7 @@ module Hdo
92
92
  @method = method
93
93
  @result_type = result_type
94
94
  @time = time
95
- @counts = Counts.new(Integer(for_count), Integer(against_count), Integer(absent_count))
95
+ @counts = Counts.new(Integer(for_count || 0), Integer(against_count || 0), Integer(absent_count || 0))
96
96
 
97
97
  @propositions = []
98
98
  @representatives = []
@@ -140,27 +140,27 @@ module Hdo
140
140
 
141
141
  def to_hash
142
142
  {
143
- :kind => self.class.kind,
144
- :externalId => @external_id,
145
- :externalIssueId => @external_issue_id,
146
- :counts => @counts.to_hash,
147
- :personal => @personal,
148
- :enacted => @enacted,
149
- :subject => @subject,
150
- :method => @method,
151
- :resultType => @result_type,
152
- :time => @time,
153
- :representatives => @representatives.map(&:to_hash),
154
- :propositions => @propositions.map(&:to_hash)
143
+ 'kind' => self.class.kind,
144
+ 'externalId' => @external_id,
145
+ 'externalIssueId' => @external_issue_id,
146
+ 'counts' => @counts.to_hash,
147
+ 'personal' => @personal,
148
+ 'enacted' => @enacted,
149
+ 'subject' => @subject,
150
+ 'method' => @method,
151
+ 'resultType' => @result_type,
152
+ 'time' => @time,
153
+ 'representatives' => @representatives.map(&:to_hash),
154
+ 'propositions' => @propositions.map(&:to_hash)
155
155
  }
156
156
  end
157
157
 
158
158
  class Counts < Struct.new(:for, :against, :absent)
159
159
  def to_hash
160
160
  {
161
- :for => self.for,
162
- :against => against,
163
- :absent => absent
161
+ 'for' => self.for,
162
+ 'against' => against,
163
+ 'absent' => absent
164
164
  }
165
165
  end
166
166
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Category do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it 'builds categories from Storting XML list' do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -56,43 +59,6 @@ module Hdo
56
59
  }')
57
60
  end
58
61
 
59
- it 'can deserialize JSON' do
60
- ex = Category.example
61
- Category.from_json(ex.to_json).should == ex
62
- end
63
-
64
- it 'can deserialize a JSON array' do
65
- input = [Category.example]
66
- Category.from_json(input.to_json).should == input
67
- end
68
-
69
- it 'fails if the given JSON is invalid' do
70
- json = '{
71
- "externalId": "1"
72
- }'
73
-
74
- expect { Category.from_json(json) }.to raise_error(ValidationError)
75
- end
76
-
77
- it 'has a kind' do
78
- Category.kind.should == 'hdo#category'
79
- end
80
-
81
- it 'has a description' do
82
- Category.description.should be_kind_of(String)
83
- end
84
-
85
- it 'has a JSON example' do
86
- Category.json_example.should be_kind_of(String)
87
- end
88
-
89
- it 'has a list of fields' do
90
- Category.fields.should_not be_empty
91
- end
92
-
93
- it 'has #short_inspect' do
94
- Category.example.short_inspect.should be_kind_of(String)
95
- end
96
62
 
97
63
  end
98
64
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Committee do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it 'builds committees from Storting XML list' do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -27,7 +30,7 @@ module Hdo
27
30
  end
28
31
 
29
32
  it 'can serialize as JSON' do
30
- str = Committee.new("ARBSOS", 'Arbeids- og sosialkomiteen').to_json
33
+ str = Committee.example.to_json
31
34
  str.should be_json <<-JSON
32
35
  {
33
36
  "kind": "hdo#committee",
@@ -37,47 +40,6 @@ module Hdo
37
40
  JSON
38
41
  end
39
42
 
40
- it 'can deserialize JSON' do
41
- com = Committee.new("ARBSOS", 'Arbeids- og sosialkomiteen')
42
- Committee.from_json(com.to_json).should == com
43
- end
44
-
45
- it 'can deserialize a JSON array' do
46
- input = [Committee.new("ARBSOS", 'Arbeids- og sosialkomiteen')]
47
- Committee.from_json(input.to_json).should == input
48
- end
49
-
50
- it 'fails to deserialize if the JSON input is invalid' do
51
- invalid = <<-JSON
52
- {
53
- "kind": "hdo#committee",
54
- "externalId": "foo"
55
- }
56
- JSON
57
-
58
- expect { Committee.from_json invalid }.to raise_exception
59
- end
60
-
61
- it 'has a kind' do
62
- Committee.kind.should == 'hdo#committee'
63
- end
64
-
65
- it 'has a description' do
66
- Committee.description.should be_kind_of(String)
67
- end
68
-
69
- it 'has a JSON example' do
70
- Committee.json_example.should be_kind_of(String)
71
- end
72
-
73
- it 'has a list of fields' do
74
- Committee.fields.should_not be_empty
75
- end
76
-
77
- it 'has #short_inspect' do
78
- Committee.example.short_inspect.should be_kind_of(String)
79
- end
80
-
81
43
  end
82
44
  end
83
45
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe District do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it 'builds committees from Storting XML list' do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -46,44 +49,6 @@ module Hdo
46
49
  District.new("Ak", "Akershus").to_json.should be_json(expected)
47
50
  end
48
51
 
49
- it 'can deserialize JSON' do
50
- orig = District.new("Ak", "Akershus")
51
- District.from_json(orig.to_json).should == orig
52
- end
53
-
54
- it 'can deserialize a JSON array' do
55
- orig = [ District.new("Ak", "Akershus") ]
56
- District.from_json(orig.to_json).should == orig
57
- end
58
-
59
- it 'fails if the given JSON is invalid' do
60
- json = '{
61
- "externalId": "1"
62
- }'
63
-
64
- expect { District.from_json(json) }.to raise_error(ValidationError)
65
- end
66
-
67
- it 'has a kind' do
68
- District.kind.should == 'hdo#district'
69
- end
70
-
71
- it 'has a description' do
72
- District.description.should be_kind_of(String)
73
- end
74
-
75
- it 'has a JSON example' do
76
- District.json_example.should be_kind_of(String)
77
- end
78
-
79
- it 'has a list of fields' do
80
- District.fields.should_not be_empty
81
- end
82
-
83
- it 'has #short_inspect' do
84
- District.example.short_inspect.should be_kind_of(String)
85
- end
86
-
87
52
  end
88
53
  end
89
54
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Issue do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it 'builds issues from Storting XML list' do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -87,56 +90,6 @@ module Hdo
87
90
  JSON
88
91
  end
89
92
 
90
- it 'can deserialize JSON' do
91
- orig = Issue.example
92
- Issue.from_json(orig.to_json).should == orig
93
- end
94
-
95
- it 'can deserialize an HDO XML doc' do
96
- orig = [Issue.example]
97
- Issue.from_json(orig.to_json).should == orig
98
- end
99
-
100
- it 'fails if the given JSON is invalid' do
101
- # summary is number.
102
-
103
- json = '{
104
- "kind": "hdo#issue",
105
- "externalId" : "53520",
106
- "summary": 1,
107
- "description": "Samtykke til inngåelse av avtale av 25. november 2011 om opprettelse av sekretariatet for Den nordlige dimensjons partnerskap for helse og livskvalitet (NDPHS)",
108
- "type": "alminneligsak",
109
- "status": "mottatt",
110
- "lastUpdate": "2012-04-20T00:00:00",
111
- "reference": "Prop. 90 S (2011-2012)",
112
- "documentGroup": "proposisjon",
113
- "committee": "Transport- og kommunikasjonskomiteen",
114
- "categories": ["UTENRIKSSAKER", "TRAKTATER", "NORDISK SAMARBEID"]
115
- }'
116
-
117
- expect { Issue.from_json(json) }.to raise_error(ValidationError)
118
- end
119
-
120
- it 'has a kind' do
121
- Issue.kind.should == 'hdo#issue'
122
- end
123
-
124
- it 'has a description' do
125
- Issue.description.should be_kind_of(String)
126
- end
127
-
128
- it 'has a JSON example' do
129
- Issue.json_example.should be_kind_of(String)
130
- end
131
-
132
- it 'has a list of fields' do
133
- Issue.fields.should_not be_empty
134
- end
135
-
136
- it 'has #short_inspect' do
137
- Issue.example.short_inspect.should be_kind_of(String)
138
- end
139
-
140
93
  end
141
94
  end
142
95
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Party do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it 'builds parties from Storting XML list' do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -42,49 +45,11 @@ module Hdo
42
45
  JSON
43
46
  end
44
47
 
45
- it 'can deserialize JSON' do
46
- orig = Party.new('Sp', 'Senterpartiet')
47
- Party.from_json(orig.to_json).should == orig
48
- end
49
-
50
- it 'can deserialize a JSON array' do
51
- orig = [Party.new('Sp', 'Senterpartiet')]
52
- Party.from_json(orig.to_json).should == orig
53
- end
54
-
55
- it 'fails if the given JSON is invalid' do
56
- json = '{
57
- "externalId": "1"
58
- }'
59
-
60
- expect { Party.from_json(json) }.to raise_error(ValidationError)
61
- end
62
-
63
- it 'has a kind' do
64
- Party.kind.should == 'hdo#party'
65
- end
66
-
67
- it 'has a description' do
68
- Party.description.should be_kind_of(String)
69
- end
70
-
71
- it 'has a JSON example' do
72
- Party.json_example.should be_kind_of(String)
73
- end
74
-
75
- it 'has a list of fields' do
76
- Party.fields.should_not be_empty
77
- end
78
-
79
48
  it 'unescapes non-ASCII characters in the external id' do
80
49
  party = Party.new('_AE_O_A', "Senterpartiet")
81
50
  party.external_id.should == "ÆØÅ"
82
51
  end
83
52
 
84
- it 'has #short_inspect' do
85
- Party.example.short_inspect.should be_kind_of(String)
86
- end
87
-
88
53
  end
89
54
  end
90
55
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Promise do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it 'builds promises from CSV' do
9
12
  csv = <<-CSV
10
13
  Parti;Løftetekst;Generell;Emner;Kilde;Side
@@ -48,60 +51,13 @@ module Hdo
48
51
  JSON
49
52
  end
50
53
 
51
- it 'deserializes from JSON' do
52
- orig = Promise.example
53
- Promise.from_json(orig.to_json).should == orig
54
- end
55
-
56
- it 'deserializes from a JSON array' do
57
- orig = [Promise.example]
58
- Promise.from_json(orig.to_json).should == orig
59
- end
60
-
61
- it 'deserializes from hash with externalId missing' do
54
+ it 'deserializes from JSON with externalId missing' do
62
55
  pr = Promise.example
63
56
  pr.external_id = nil
64
57
 
65
58
  Promise.from_json(pr.to_json).should be_kind_of(Promise)
66
59
  end
67
60
 
68
- it 'fails if the given JSON is invalid' do
69
- # missing general
70
-
71
- json = '
72
- {
73
- "kind": "hdo#promise",
74
- "externalId": "1",
75
- "party": "H",
76
- "categories": ["GRUNNSKOLE"],
77
- "source": "PP",
78
- "page": 8,
79
- "body": "Stille strengere krav til orden og oppførsel for å hindre at uro ødelegger undervisningen."
80
- }'
81
-
82
- expect { Promise.from_json(json) }.to raise_error(ValidationError)
83
- end
84
-
85
- it 'has a description' do
86
- Promise.description.should be_kind_of(String)
87
- end
88
-
89
- it 'has fields' do
90
- Promise.fields.should_not be_empty
91
- end
92
-
93
- it 'has a kind' do
94
- Promise.kind.should == 'hdo#promise'
95
- end
96
-
97
- it 'has a JSON example' do
98
- Promise.json_example.should be_kind_of(String)
99
- end
100
-
101
- it 'has #short_inspect' do
102
- Promise.example.short_inspect.should be_kind_of(String)
103
- end
104
-
105
61
  it 'strips trailing space from the body' do
106
62
  promise = Promise.new("Party", "Body ", true, ["æøå"], "PP", 8)
107
63
  promise.body.should == "Body"
@@ -4,40 +4,8 @@ module Hdo
4
4
  module StortingImporter
5
5
  describe Proposition do
6
6
 
7
- it 'can deserialize JSON' do
8
- orig = Proposition.example
9
- Proposition.from_json(orig.to_json).should == orig
10
- end
11
-
12
- it 'can deserialize a JSON array' do
13
- orig = [Proposition.example]
14
- Proposition.from_json(orig.to_json).should == orig
15
- end
16
-
17
- it 'fails if the given JSON is invalid' do
18
- json = Proposition.example.to_hash
19
- json.delete :description
20
-
21
- expect { Proposition.from_json(json.to_json) }.to raise_error(ValidationError)
22
- end
23
-
24
- it 'has a kind' do
25
- Proposition.kind.should == 'hdo#proposition'
26
- end
27
-
28
- it 'has a description' do
29
- Proposition.description.should be_kind_of(String)
30
- end
31
-
32
- it 'has a JSON example' do
33
- Proposition.json_example.should be_kind_of(String)
34
- end
35
-
36
- it 'has #short_inspect' do
37
- str = Proposition.example.short_inspect
38
- str.should be_kind_of(String)
39
- str.should_not include("nil")
40
- end
7
+ it_behaves_like 'type with JSON schema'
8
+ it_behaves_like 'type with #short_inspect'
41
9
 
42
10
  end
43
11
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Representative do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it "builds representatives from the Storting XML list" do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -67,17 +70,7 @@ module Hdo
67
70
  JSON
68
71
  end
69
72
 
70
- it 'can deserialize JSON' do
71
- rep = Representative.example
72
- Representative.from_json(rep.to_json).should == rep
73
- end
74
-
75
- it 'can deserialize a JSON array' do
76
- reps = [Representative.example]
77
- Representative.from_json(reps.to_json).should == reps
78
- end
79
-
80
- it 'raises if the JSON if voteResult is invalid' do
73
+ it 'is invalid if the voteResult property is invalid' do
81
74
  pending "find a validator that checks format"
82
75
 
83
76
  invalid = <<-JSON
@@ -98,46 +91,7 @@ module Hdo
98
91
 
99
92
  JSON
100
93
 
101
- expect {
102
- Representative.from_json(invalid)
103
- }.to raise_error(ValidationError)
104
- end
105
-
106
- it 'raises if the JSON data is invalid' do
107
- invalid = <<-JSON
108
- {
109
- "kind": "hdo#representative",
110
- "externalId": "ADA",
111
- "lastName": "Dahl",
112
- "gender": "M",
113
- "dateOfBirth": "1975-07-07T00:00:00",
114
- "dateOfDeath": "0001-01-01T00:00:00",
115
- "district": "Akershus",
116
- "party": "Høyre",
117
- "committees": ["Justiskomiteen"],
118
- "period": "2011-2012"
119
- }
120
- JSON
121
-
122
- expect {
123
- Representative.from_json(invalid)
124
- }.to raise_error(ValidationError)
125
- end
126
-
127
- it 'has a kind' do
128
- Representative.kind.should == 'hdo#representative'
129
- end
130
-
131
- it 'has a description' do
132
- Representative.description.should be_kind_of(String)
133
- end
134
-
135
- it 'has an JSON example' do
136
- Representative.json_example.should be_kind_of(String)
137
- end
138
-
139
- it 'has a list of fields' do
140
- Representative.fields.should_not be_empty
94
+ Representative.from_json(invalid).should_not be_valid
141
95
  end
142
96
 
143
97
  it 'unescapes non-ASCII characters in the external id' do
@@ -147,10 +101,6 @@ module Hdo
147
101
  rep.external_id.should == "ÆØÅ"
148
102
  end
149
103
 
150
- it 'has #short_inspect' do
151
- Representative.example.short_inspect.should be_kind_of(String)
152
- end
153
-
154
104
  end
155
105
  end
156
106
  end
@@ -5,6 +5,9 @@ module Hdo
5
5
  module StortingImporter
6
6
  describe Vote do
7
7
 
8
+ it_behaves_like 'type with JSON schema'
9
+ it_behaves_like 'type with #short_inspect'
10
+
8
11
  it "builds votes from the Storting XML list" do
9
12
  xml = <<-XML
10
13
  <?xml version="1.0" encoding="utf-8"?>
@@ -131,43 +134,6 @@ module Hdo
131
134
  JSON
132
135
  end
133
136
 
134
- it 'can deserialize JSON' do
135
- orig = Vote.example
136
- Vote.from_json(orig.to_json).should == orig
137
- end
138
-
139
- it 'can deserialize a JSON array' do
140
- orig = [Vote.example]
141
- Vote.from_json(orig.to_json).should == orig
142
- end
143
-
144
- it 'fails if the given JSON is invalid' do
145
- json = Vote.example.to_hash
146
- json.delete :personal
147
-
148
- expect { Vote.from_json(json.to_json) }.to raise_error(ValidationError)
149
- end
150
-
151
- it 'has a kind' do
152
- Vote.kind.should == 'hdo#vote'
153
- end
154
-
155
- it 'has a description' do
156
- Vote.description.should be_kind_of(String)
157
- end
158
-
159
- it 'has a JSON example' do
160
- Vote.json_example.should be_kind_of(String)
161
- end
162
-
163
- it 'has a list of fields' do
164
- Vote.fields.should_not be_empty
165
- end
166
-
167
- it 'has #short_inspect' do
168
- Vote.example.short_inspect.should be_kind_of(String)
169
- end
170
-
171
137
  end
172
138
  end
173
139
  end
data/spec/spec_helper.rb CHANGED
@@ -3,6 +3,10 @@ require 'hdo/storting_importer'
3
3
  require 'pry'
4
4
  require 'pp'
5
5
 
6
+ Dir[File.expand_path("../support/**/*.rb", __FILE__)].each do |f|
7
+ require f
8
+ end
9
+
6
10
  module Hdo
7
11
  module StortingImporter
8
12
  module SpecHelper
@@ -0,0 +1,53 @@
1
+ module Hdo
2
+ module StortingImporter
3
+ shared_examples_for "type with JSON schema" do
4
+
5
+ it 'can deserialize JSON' do
6
+ ex = described_class.example
7
+ described_class.from_json(ex.to_json).should == ex
8
+ end
9
+
10
+ it 'can deserialize a JSON array' do
11
+ input = [described_class.example]
12
+ described_class.from_json(input.to_json).should == input
13
+ end
14
+
15
+ it 'fails if the given JSON is invalid' do
16
+ expect {
17
+ described_class.from_json('{}')
18
+ }.to raise_error(ValidationError)
19
+ end
20
+
21
+ it 'knows if itself is valid' do
22
+ valid = described_class.example
23
+
24
+ valid.should be_valid
25
+ valid.validate!
26
+ end
27
+
28
+ it 'knows if itself is invalid' do
29
+ invalid = described_class.from_hash({})
30
+ invalid.should_not be_valid
31
+
32
+ expect { invalid.validate! }.to raise_error(ValidationError)
33
+ end
34
+
35
+ it 'has a kind' do
36
+ described_class.kind.should be_kind_of(String)
37
+ end
38
+
39
+ it 'has a description' do
40
+ described_class.description.should be_kind_of(String)
41
+ end
42
+
43
+ it 'has a JSON example' do
44
+ described_class.json_example.should be_kind_of(String)
45
+ end
46
+
47
+ it 'has a list of fields' do
48
+ described_class.fields.should_not be_empty
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,9 @@
1
+ module Hdo
2
+ module StortingImporter
3
+ shared_examples_for "type with #short_inspect" do
4
+ it 'has #short_inspect' do
5
+ described_class.example.short_inspect.should be_kind_of(String)
6
+ end
7
+ end
8
+ end
9
+ 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.9
4
+ version: 0.1.0
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-08-06 00:00:00.000000000 Z
12
+ date: 2012-08-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder
@@ -171,6 +171,38 @@ dependencies:
171
171
  - - ! '>='
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: guard
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: guard-rspec
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
174
206
  - !ruby/object:Gem::Dependency
175
207
  name: rspec
176
208
  requirement: !ruby/object:Gem::Requirement
@@ -198,6 +230,7 @@ files:
198
230
  - .gitignore
199
231
  - .travis.yml
200
232
  - Gemfile
233
+ - Guardfile
201
234
  - LICENSE
202
235
  - README.md
203
236
  - Rakefile
@@ -277,6 +310,8 @@ files:
277
310
  - spec/hdo/storting_importer/representative_spec.rb
278
311
  - spec/hdo/storting_importer/vote_spec.rb
279
312
  - spec/spec_helper.rb
313
+ - spec/support/shared_examples_for_json_schema.rb
314
+ - spec/support/shared_examples_for_short_inspect.rb
280
315
  homepage: http://github.com/holderdeord/hdo-storting-importer
281
316
  licenses: []
282
317
  post_install_message:
@@ -343,4 +378,6 @@ test_files:
343
378
  - spec/hdo/storting_importer/representative_spec.rb
344
379
  - spec/hdo/storting_importer/vote_spec.rb
345
380
  - spec/spec_helper.rb
381
+ - spec/support/shared_examples_for_json_schema.rb
382
+ - spec/support/shared_examples_for_short_inspect.rb
346
383
  has_rdoc: