hdo-storting-importer 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,6 +23,7 @@ Gem::Specification.new do |gem|
23
23
  gem.add_runtime_dependency "unicode_utils"
24
24
  gem.add_runtime_dependency "multi_json"
25
25
  gem.add_runtime_dependency "yajl-ruby"
26
+ gem.add_runtime_dependency "roo"
26
27
  gem.add_runtime_dependency "jschematic", ">= 0.1.0"
27
28
 
28
29
  gem.add_development_dependency 'pry'
@@ -7,7 +7,7 @@ module Hdo
7
7
 
8
8
  class FusionTable
9
9
  def initialize(api_key)
10
- @api_key = api_key
10
+ @api_key = api_key
11
11
  end
12
12
 
13
13
  def query(sql, opts = {})
@@ -1,6 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require 'csv'
3
+ require 'roo'
4
4
 
5
5
  module Hdo
6
6
  module StortingImporter
@@ -8,8 +8,7 @@ module Hdo
8
8
  include HasJsonSchema
9
9
  include IvarEquality
10
10
 
11
- attr_reader :party, :body, :general, :categories, :source, :page
12
- attr_accessor :external_id
11
+ attr_reader :external_id, :party, :body, :general, :categories, :source, :page, :date
13
12
  alias_method :general?, :general
14
13
  alias_method :short_inspect, :inspect
15
14
 
@@ -23,7 +22,8 @@ module Hdo
23
22
  true,
24
23
  ["GRUNNSKOLE"],
25
24
  "PP",
26
- 8
25
+ 8,
26
+ '2009-06-01'
27
27
  )
28
28
  end
29
29
 
@@ -31,23 +31,70 @@ module Hdo
31
31
  Util.json_pretty example
32
32
  end
33
33
 
34
- # doesn't really belong here - data source?
35
- def self.from_fusion_table(table_id, api_key)
36
- table = FusionTable.new(api_key)
37
-
38
- column_names = table.columns_for(table_id).map { |e| e['name'] }.map { |e| e.inspect }.join(",")
39
- row_count = Integer(table.query("select count(rowid) from #{table_id}", :rows => true).flatten.first)
40
-
41
- # do this in batches of 100
42
- limit = 100
43
- result = []
44
-
45
- (0..row_count).step(limit) do |offset|
46
- sql = "SELECT #{column_names},\"rowid\" FROM #{table_id} OFFSET #{offset} LIMIT #{limit}"
47
- result.concat table.query(sql, :rows => true).map { |data| new(*data) }
34
+ def self.from_xlsx(path)
35
+ table = Excelx.new(path)
36
+ raise "empty spreadsheet" unless table.first_row
37
+
38
+ errors = []
39
+
40
+ # 0 is empty, 1 is header
41
+ promises = 2.upto(table.last_row).map { |idx|
42
+ data = table.row(idx)
43
+
44
+ Hdo::StortingImporter.logger.info "promise row: #{data.inspect}"
45
+
46
+ external_id = data.fetch(0).to_i.to_s
47
+ date = data.fetch(1)
48
+ party = data.fetch(2)
49
+ body = data.fetch(3)
50
+ general = data.fetch(4).to_s.strip.downcase
51
+ categories = data.fetch(5)
52
+ source = data.fetch(6)
53
+ page = data.fetch(7)
54
+
55
+ unless %w[ja nei].include?(general)
56
+ errors << "row #{external_id}: unexpected value #{general.inspect} for general"
57
+ next
58
+ end
59
+
60
+ if page.to_s !~ /^\d+(\.\d+)?$/ || page.to_i == 0
61
+ errors << "row #{external_id}: unexpected value #{page.inspect} for page"
62
+ next
63
+ end
64
+
65
+ unless party
66
+ errors << "row #{external_id}: party missing"
67
+ next
68
+ end
69
+
70
+ unless body =~ /\.$/
71
+ body << "."
72
+ end
73
+
74
+ promise = new external_id,
75
+ party,
76
+ body,
77
+ general.downcase == "ja",
78
+ categories,
79
+ source,
80
+ Integer(page),
81
+ date
82
+
83
+ begin
84
+ promise.validate!
85
+ rescue Hdo::StortingImporter::ValidationError => ex
86
+ errors << "row #{external_id}: #{ex.message}"
87
+ next
88
+ end
89
+
90
+ promise
91
+ }.compact
92
+
93
+ if errors.any?
94
+ raise "found errors:\n#{errors.join("\n")}"
48
95
  end
49
96
 
50
- result
97
+ promises
51
98
  end
52
99
 
53
100
  def self.from_hash(hash)
@@ -57,12 +104,13 @@ module Hdo
57
104
  hash['general'],
58
105
  hash['categories'],
59
106
  hash['source'],
60
- hash['page']
107
+ hash['page'],
108
+ hash['date']
61
109
 
62
110
  pr
63
111
  end
64
112
 
65
- def initialize(external_id, party, body, general, categories, source, page)
113
+ def initialize(external_id, party, body, general, categories, source, page, date)
66
114
  @external_id = external_id
67
115
  @party = strip_if_string(party)
68
116
  @body = strip_if_string(body)
@@ -70,10 +118,11 @@ module Hdo
70
118
  @categories = clean_categories(categories)
71
119
  @source = strip_if_string(source)
72
120
  @page = page
121
+ @date = date
73
122
  end
74
123
 
75
124
  def to_hash
76
- h = {
125
+ {
77
126
  'kind' => self.class.kind,
78
127
  'externalId' => @external_id,
79
128
  'party' => @party,
@@ -81,10 +130,9 @@ module Hdo
81
130
  'categories' => @categories,
82
131
  'source' => @source,
83
132
  'page' => @page,
84
- 'body' => @body
133
+ 'body' => @body,
134
+ 'date' => @date
85
135
  }
86
-
87
- h
88
136
  end
89
137
 
90
138
  private
@@ -32,13 +32,23 @@
32
32
  },
33
33
  "source": {
34
34
  "type": "string",
35
- "description": "The source of the promise. (TODO: this should always be a URL)",
35
+ "description": "The source of the promise.",
36
36
  "required": true
37
37
  },
38
+ "page": {
39
+ "type": "number",
40
+ "description": "What page in the source the promise was found on"
41
+ },
38
42
  "body": {
39
43
  "type": "string",
40
44
  "description": "The body text of the promise.",
41
45
  "required": true
46
+ },
47
+ "date": {
48
+ "type": "string",
49
+ "description": "The date the promise was made.",
50
+ "format": "date",
51
+ "required": true
42
52
  }
43
53
  }
44
54
  }
@@ -55,7 +55,7 @@
55
55
  "voteResult": {
56
56
  "type": "string",
57
57
  "description": "If this representative object is part of a hdo#vote, this property describes how the representative voted. Valid values are for|against|absent.",
58
- "format": "for|against|absent"
58
+ "pattern": "for|against|absent"
59
59
  }
60
60
  }
61
61
  }
@@ -1,5 +1,5 @@
1
1
  module Hdo
2
2
  module StortingImporter
3
- VERSION = "0.1.5"
3
+ VERSION = "0.1.6"
4
4
  end
5
5
  end
@@ -18,23 +18,24 @@ module Hdo
18
18
  "categories": ["GRUNNSKOLE"],
19
19
  "source": "PP",
20
20
  "page": 8,
21
- "body": "Stille strengere krav til orden og oppførsel for å hindre at uro ødelegger undervisningen."
21
+ "body": "Stille strengere krav til orden og oppførsel for å hindre at uro ødelegger undervisningen.",
22
+ "date": "2009-06-01"
22
23
  }
23
24
  JSON
24
25
  end
25
26
 
26
27
  it 'strips trailing space from the body' do
27
- promise = Promise.new("1", "Party", "Body ", true, ["æøå"], "PP", 8)
28
+ promise = Promise.new("1", "Party", "Body ", true, ["æøå"], "PP", 8, '2012-06-01')
28
29
  promise.body.should == "Body"
29
30
  end
30
31
 
31
32
  it 'correctly upcases non-ASCII category names' do
32
- promise = Promise.new("1", "Party", "Body", true, ["æøå"], "PP", 8)
33
+ promise = Promise.new("1", "Party", "Body", true, ["æøå"], "PP", 8, '2012-06-01')
33
34
  promise.categories.should == ["ÆØÅ"]
34
35
  end
35
36
 
36
37
  it 'ignores empty categories' do
37
- promise = Promise.new("1", "Party", "Body", true, ["FOO", ""], "PP", 8)
38
+ promise = Promise.new("1", "Party", "Body", true, ["FOO", ""], "PP", 8, '2012-06-01')
38
39
  promise.categories.should == ["FOO"]
39
40
  end
40
41
 
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.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -139,6 +139,22 @@ dependencies:
139
139
  - - ! '>='
140
140
  - !ruby/object:Gem::Version
141
141
  version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: roo
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
142
158
  - !ruby/object:Gem::Dependency
143
159
  name: jschematic
144
160
  requirement: !ruby/object:Gem::Requirement