data_spork 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.
- checksums.yaml +8 -8
- data/lib/data_spork/base_writer.rb +46 -0
- data/lib/data_spork/importer.rb +40 -49
- data/lib/data_spork/json_writer.rb +2 -0
- data/lib/data_spork/version.rb +1 -1
- data/lib/data_spork/xml_writer.rb +8 -28
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ODkyMWQ2NjNjMGJmY2M0MWZjZGZhNTlhYjhmMmJhZDA2NDJlMjQ2ZA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MWViN2E3NjJhYWIzNzJhZjZjMmI2N2RiODc2MmRlMWI1NjA2ZDMwMA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZWY4NTE3ZmM1YzY5YzNkNzIwOTZiNjdkN2NlOTg0NDc4Yjk5MGZiNDQ1MjYx
|
10
|
+
YzA4MWNhNTEzNTMxYjVhNDIzYTFlN2UyN2ZhZDMzYjgxZjExOGU4YTA4N2I3
|
11
|
+
NzhhN2I3OGExNTBmZTFiNzcwMThjZDY4NjY5MGNlYmUyYjk4ZDY=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
YjY0YWQ3MTRhNTkyZDk4MzFlNGVhYTAwOWQ4NDIxNjZhOGUzMGExMWEzYzQ5
|
14
|
+
ZDhhZjgwYTIxMTkyNTUyNzM5YmFlMWFmMzdiMTIzOTc3YWQwNzNmNDRiNTg5
|
15
|
+
NWNiYWI4Y2RjZTRiZWVmNzE1YzkwNWFmNGVhN2UwOGY4ZmU2OTI=
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module DataSpork
|
2
|
+
class BaseWriter
|
3
|
+
attr_accessor :started
|
4
|
+
attr_reader :owner
|
5
|
+
|
6
|
+
delegate :print, :print_error, :to => :owner
|
7
|
+
delegate :effective_date, :col_tags, :root_tag, :row_tag, :header, :col_value, :to => :owner
|
8
|
+
|
9
|
+
def initialize(owner)
|
10
|
+
@owner = owner
|
11
|
+
@started = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def started?
|
15
|
+
started
|
16
|
+
end
|
17
|
+
|
18
|
+
# Start the document output - subclasses usually override :start_document instead.
|
19
|
+
def start
|
20
|
+
start_document
|
21
|
+
self.started = true
|
22
|
+
end
|
23
|
+
|
24
|
+
def start_document
|
25
|
+
# override this
|
26
|
+
end
|
27
|
+
|
28
|
+
def begin_put_row
|
29
|
+
# override this
|
30
|
+
end
|
31
|
+
|
32
|
+
def put_column
|
33
|
+
# override this
|
34
|
+
end
|
35
|
+
|
36
|
+
def end_put_row
|
37
|
+
# override this
|
38
|
+
end
|
39
|
+
|
40
|
+
# Close out the document.
|
41
|
+
def finish
|
42
|
+
# override this
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
data/lib/data_spork/importer.rb
CHANGED
@@ -1,19 +1,21 @@
|
|
1
1
|
require 'data_spork/base_reader'
|
2
|
+
require 'data_spork/base_writer'
|
2
3
|
require 'data_spork/xml_writer'
|
4
|
+
require 'data_spork/json_writer'
|
3
5
|
require 'data_spork/google_spreadsheet'
|
4
6
|
|
5
7
|
module DataSpork
|
6
8
|
|
7
9
|
class Importer
|
8
|
-
attr_reader :headers, :input_type, :options, :
|
10
|
+
attr_reader :headers, :input_type, :options, :writers
|
9
11
|
attr_reader :row_num, :row, :col_map
|
10
|
-
attr_reader :root_tag, :row_tag, :
|
12
|
+
attr_reader :root_tag, :row_tag, :col_tags
|
11
13
|
attr_accessor :col_num, :setup_state, :blank_row
|
12
14
|
attr_accessor :effective_date
|
13
15
|
|
14
|
-
# Entry point to convert the input
|
16
|
+
# Entry point to convert the input document and output it to the selected format(s).
|
15
17
|
#
|
16
|
-
# @param :input_type symbol indicating whether to output :xlsx or :
|
18
|
+
# @param :input_type symbol indicating whether to output :xlsx, :csv, or :json
|
17
19
|
# @param :options hash with options to control the behavior of the conversion
|
18
20
|
def self.convert(input_type, options = nil)
|
19
21
|
self.new(input_type, options).convert
|
@@ -23,7 +25,7 @@ module DataSpork
|
|
23
25
|
def initialize(input_type, options = nil)
|
24
26
|
@input_type = input_type
|
25
27
|
init_options options
|
26
|
-
|
28
|
+
init_writers
|
27
29
|
end
|
28
30
|
|
29
31
|
def init_options(options)
|
@@ -31,6 +33,15 @@ module DataSpork
|
|
31
33
|
@options[:output_path] = @options[:source_path] if @options[:output_path].nil? and @options[:output_file]
|
32
34
|
end
|
33
35
|
|
36
|
+
def init_writers
|
37
|
+
@writers = [ ]
|
38
|
+
add_writers
|
39
|
+
end
|
40
|
+
|
41
|
+
def add_writers
|
42
|
+
writers << XmlWriter.new(self)
|
43
|
+
end
|
44
|
+
|
34
45
|
def effective_date_pattern
|
35
46
|
/^[Ee]ffective [Dd]ate+/
|
36
47
|
end
|
@@ -43,6 +54,10 @@ module DataSpork
|
|
43
54
|
$stderr.puts str
|
44
55
|
end
|
45
56
|
|
57
|
+
def write(msg)
|
58
|
+
writers.each {|writer| writer.send msg }
|
59
|
+
end
|
60
|
+
|
46
61
|
def source_name
|
47
62
|
base = DEFAULT_INPUT_NAME
|
48
63
|
modifier = ''
|
@@ -108,12 +123,11 @@ module DataSpork
|
|
108
123
|
end
|
109
124
|
@row_num = 0
|
110
125
|
@headers = []
|
111
|
-
self.setup_state = :
|
126
|
+
self.setup_state = :setup_writers
|
112
127
|
end
|
113
128
|
|
114
129
|
def finish
|
115
|
-
|
116
|
-
finish_capture
|
130
|
+
write :finish
|
117
131
|
end
|
118
132
|
|
119
133
|
# Appends the specified row to the output.
|
@@ -182,39 +196,6 @@ module DataSpork
|
|
182
196
|
!headers.empty?
|
183
197
|
end
|
184
198
|
|
185
|
-
def begin_menu_group(name)
|
186
|
-
self.menu_group_name = name
|
187
|
-
id = menu_board.size + 1
|
188
|
-
self.menu_group = { id: id, name: name, display_order: id, choices: [] }
|
189
|
-
menu_board[name] = menu_group
|
190
|
-
end
|
191
|
-
|
192
|
-
def capture_row
|
193
|
-
order = menu_group[:choices].size + 1
|
194
|
-
self.menu_choice = { id: row_num, display_order: order, menu_group_id: menu_group[:id], options: { } }
|
195
|
-
self.menu_group[:choices] << menu_choice
|
196
|
-
end
|
197
|
-
|
198
|
-
def capture_column
|
199
|
-
key = case header
|
200
|
-
when 'ingredients'
|
201
|
-
'description'
|
202
|
-
when 'trademarked'
|
203
|
-
'is_trademarked'
|
204
|
-
else
|
205
|
-
header
|
206
|
-
end
|
207
|
-
if %w(trademarked image_url).include?(key)
|
208
|
-
menu_choice[:options][key.to_sym] = col_value
|
209
|
-
else
|
210
|
-
menu_choice[key.to_sym] = col_value
|
211
|
-
end
|
212
|
-
end
|
213
|
-
|
214
|
-
def finish_capture
|
215
|
-
# empty - subclass should override
|
216
|
-
end
|
217
|
-
|
218
199
|
# Output the current row of data, which were parsed from the CSV input.
|
219
200
|
def output
|
220
201
|
unless reject?.tap { |r| print "rejected #{row_num}: #{row}" if r and VERBOSE }
|
@@ -242,8 +223,8 @@ module DataSpork
|
|
242
223
|
end
|
243
224
|
|
244
225
|
# Initializes the xml document and transfers setup_state to :setup
|
245
|
-
def
|
246
|
-
|
226
|
+
def setup_writers
|
227
|
+
write :start
|
247
228
|
self.setup_state = :setup
|
248
229
|
send setup_state # automatically transition to next state
|
249
230
|
end
|
@@ -268,21 +249,31 @@ module DataSpork
|
|
268
249
|
|
269
250
|
# Answer true when the current column should be included in the output.
|
270
251
|
def output_column?
|
271
|
-
|
252
|
+
col_tags.include? header
|
253
|
+
end
|
254
|
+
|
255
|
+
def on_begin_row
|
256
|
+
write :begin_put_row
|
257
|
+
end
|
258
|
+
|
259
|
+
def on_output_column
|
260
|
+
write :put_column
|
261
|
+
end
|
262
|
+
|
263
|
+
def on_end_row
|
264
|
+
write :end_put_row
|
272
265
|
end
|
273
266
|
|
274
267
|
# Output the current row, one column at a time.
|
275
268
|
def put_row
|
276
|
-
|
277
|
-
capture_row
|
269
|
+
on_begin_row
|
278
270
|
row.each_index do |index|
|
279
271
|
self.col_num = index
|
280
272
|
if output_column?
|
281
|
-
|
282
|
-
capture_column
|
273
|
+
on_output_column
|
283
274
|
end
|
284
275
|
end
|
285
|
-
|
276
|
+
on_end_row
|
286
277
|
end
|
287
278
|
|
288
279
|
end
|
data/lib/data_spork/version.rb
CHANGED
@@ -1,43 +1,23 @@
|
|
1
|
-
class DataSpork::XmlWriter
|
2
|
-
|
3
|
-
attr_accessor :started
|
4
|
-
attr_reader :owner
|
5
|
-
|
6
|
-
delegate :print, :print_error, :to => :owner
|
7
|
-
delegate :effective_date, :xml_tags, :root_tag, :row_tag, :header, :col_value, :to => :owner
|
8
|
-
|
9
|
-
def initialize(owner)
|
10
|
-
@owner = owner
|
11
|
-
@started = false
|
12
|
-
end
|
13
|
-
|
14
|
-
# Output the XML preface.
|
15
|
-
def start
|
16
|
-
start_document
|
17
|
-
start_schema
|
18
|
-
self.started = true
|
19
|
-
end
|
1
|
+
class DataSpork::XmlWriter < DataSpork::BaseWriter
|
20
2
|
|
21
3
|
# Output the XML document preface.
|
22
4
|
def start_document
|
23
5
|
print '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
|
6
|
+
start_schema
|
24
7
|
end
|
25
8
|
|
26
9
|
# Output the schema with the root tag.
|
27
10
|
def start_schema
|
28
|
-
|
29
|
-
print owner.xml_start_schema(self)
|
30
|
-
else
|
31
|
-
print begin_tag %Q(#{root_tag} #{schema_uri} effective_date="#{effective_date}")
|
32
|
-
end
|
11
|
+
print begin_tag(build_schema_tag)
|
33
12
|
end
|
34
13
|
|
35
|
-
|
36
|
-
|
14
|
+
# Output the schema with the root tag.
|
15
|
+
def build_schema_tag
|
16
|
+
%Q(#{root_tag} #{schema_uri} effective_date="#{effective_date}")
|
37
17
|
end
|
38
18
|
|
39
|
-
def
|
40
|
-
|
19
|
+
def schema_uri
|
20
|
+
%q(xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance")
|
41
21
|
end
|
42
22
|
|
43
23
|
# Close out the XML document.
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_spork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Jackson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -89,8 +89,10 @@ extra_rdoc_files: []
|
|
89
89
|
files:
|
90
90
|
- lib/data_spork.rb
|
91
91
|
- lib/data_spork/base_reader.rb
|
92
|
+
- lib/data_spork/base_writer.rb
|
92
93
|
- lib/data_spork/google_spreadsheet.rb
|
93
94
|
- lib/data_spork/importer.rb
|
95
|
+
- lib/data_spork/json_writer.rb
|
94
96
|
- lib/data_spork/version.rb
|
95
97
|
- lib/data_spork/xml_writer.rb
|
96
98
|
homepage: http://bitbucket.org/leadbaxter/data_spork
|
@@ -116,5 +118,6 @@ rubyforge_project:
|
|
116
118
|
rubygems_version: 2.2.1
|
117
119
|
signing_key:
|
118
120
|
specification_version: 4
|
119
|
-
summary: Import CSV, Excel [.XLS, .XLSX] and Google Drive Spreadsheets.
|
121
|
+
summary: Import CSV, Excel [.XLS, .XLSX] and Google Drive Spreadsheets. Output to
|
122
|
+
XML or JSON (more formats to come!)
|
120
123
|
test_files: []
|