remote_table 3.0.0.beta → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -0
- data/lib/remote_table.rb +13 -4
- data/lib/remote_table/delimited.rb +3 -11
- data/lib/remote_table/processed_by_nokogiri.rb +1 -4
- data/lib/remote_table/version.rb +1 -1
- data/test/test_parser.rb +20 -2
- metadata +2 -2
data/CHANGELOG
CHANGED
data/lib/remote_table.rb
CHANGED
@@ -110,6 +110,13 @@ class RemoteTable
|
|
110
110
|
uri.to_s
|
111
111
|
end
|
112
112
|
|
113
|
+
def normalize_whitespace(v)
|
114
|
+
v = v.to_s.dup
|
115
|
+
v.gsub! WHITESPACE, SINGLE_SPACE
|
116
|
+
v.strip!
|
117
|
+
v
|
118
|
+
end
|
119
|
+
|
113
120
|
private
|
114
121
|
|
115
122
|
def basename(url)
|
@@ -129,6 +136,8 @@ class RemoteTable
|
|
129
136
|
end
|
130
137
|
end
|
131
138
|
|
139
|
+
WHITESPACE = /\s+/
|
140
|
+
SINGLE_SPACE = ' '
|
132
141
|
EXTERNAL_ENCODING = 'UTF-8'
|
133
142
|
EXTERNAL_ENCODING_ICONV = 'UTF-8//TRANSLIT'
|
134
143
|
GOOGLE_DOCS_SPREADSHEET = [
|
@@ -343,14 +352,14 @@ class RemoteTable
|
|
343
352
|
|
344
353
|
# @private
|
345
354
|
class NullParser
|
346
|
-
def
|
355
|
+
def call(row)
|
347
356
|
[row]
|
348
357
|
end
|
349
358
|
end
|
350
359
|
|
351
|
-
# An object that responds to #
|
360
|
+
# An object that responds to #call(row) and returns an array of one or more rows.
|
352
361
|
#
|
353
|
-
# @return [#
|
362
|
+
# @return [#call]
|
354
363
|
def parser
|
355
364
|
@final_parser ||= (@parser || NullParser.new)
|
356
365
|
end
|
@@ -452,7 +461,7 @@ class RemoteTable
|
|
452
461
|
mark_download!
|
453
462
|
preprocess!
|
454
463
|
memo = _each do |row|
|
455
|
-
parser.
|
464
|
+
parser.call(row).each do |virtual_row|
|
456
465
|
virtual_row.row_hash = ::HashDigest.hexdigest row
|
457
466
|
if errata
|
458
467
|
next if errata.rejects? virtual_row
|
@@ -31,7 +31,7 @@ class RemoteTable
|
|
31
31
|
|
32
32
|
# represent the row as an array
|
33
33
|
array = row.map do |v|
|
34
|
-
v = v
|
34
|
+
v = RemoteTable.normalize_whitespace v
|
35
35
|
if not some_value_present and not keep_blank_rows and v.present?
|
36
36
|
some_value_present = true
|
37
37
|
end
|
@@ -46,7 +46,7 @@ class RemoteTable
|
|
46
46
|
# represent the row as a hash
|
47
47
|
hash = ::ActiveSupport::OrderedHash.new
|
48
48
|
row.each do |k, v|
|
49
|
-
v = v
|
49
|
+
v = RemoteTable.normalize_whitespace v
|
50
50
|
if not some_value_present and not keep_blank_rows and v.present?
|
51
51
|
some_value_present = true
|
52
52
|
end
|
@@ -62,14 +62,6 @@ class RemoteTable
|
|
62
62
|
local_copy.cleanup
|
63
63
|
end
|
64
64
|
|
65
|
-
# Passes user-specified options in PASSTHROUGH_CSV_SETTINGS.
|
66
|
-
#
|
67
|
-
# Also maps:
|
68
|
-
# * +:headers+ directly
|
69
|
-
# * +:keep_blank_rows+ to the CSV option +:skip_blanks+
|
70
|
-
# * +:delimiter+ to the CSV option +:col_sep+
|
71
|
-
#
|
72
|
-
# @return [Hash]
|
73
65
|
def csv_options
|
74
66
|
{
|
75
67
|
skip_blanks: !keep_blank_rows,
|
@@ -88,7 +80,7 @@ class RemoteTable
|
|
88
80
|
i = 0
|
89
81
|
line = local_copy.encoded_io.gets
|
90
82
|
Engine.parse_line(line).map do |v|
|
91
|
-
header =
|
83
|
+
header = RemoteTable.normalize_whitespace v
|
92
84
|
header.present? ? header : "empty_#{i+=1}"
|
93
85
|
end
|
94
86
|
when Array
|
@@ -1,8 +1,6 @@
|
|
1
1
|
class RemoteTable
|
2
2
|
# Mixed in to process XML and XHTML.
|
3
3
|
module ProcessedByNokogiri
|
4
|
-
WHITESPACE = /\s+/
|
5
|
-
SINGLE_SPACE = ' '
|
6
4
|
SOFT_HYPHEN = '­'
|
7
5
|
|
8
6
|
def preprocess!
|
@@ -34,8 +32,7 @@ class RemoteTable
|
|
34
32
|
end.map do |cell|
|
35
33
|
memo = cell.content.dup
|
36
34
|
memo = assume_utf8 memo
|
37
|
-
memo.
|
38
|
-
memo.strip!
|
35
|
+
memo = RemoteTable.normalize_whitespace memo
|
39
36
|
if not some_value_present and not keep_blank_rows and memo.present?
|
40
37
|
some_value_present = true
|
41
38
|
end
|
data/lib/remote_table/version.rb
CHANGED
data/test/test_parser.rb
CHANGED
@@ -2,9 +2,9 @@ require 'helper'
|
|
2
2
|
|
3
3
|
describe RemoteTable do
|
4
4
|
describe ":parser option" do
|
5
|
-
it "takes a parser object that responds to #
|
5
|
+
it "takes a parser object that responds to #call(row) and returns an array of rows" do
|
6
6
|
class GradeRangeParser
|
7
|
-
def
|
7
|
+
def call(row)
|
8
8
|
row['range'].split('-').map do |subrange|
|
9
9
|
virtual_row = row.dup
|
10
10
|
virtual_row.delete 'range'
|
@@ -20,5 +20,23 @@ describe RemoteTable do
|
|
20
20
|
t[3].must_equal 'description' => 'bad', 'grade' => 'D'
|
21
21
|
t[4].must_equal 'description' => 'bad', 'grade' => 'F'
|
22
22
|
end
|
23
|
+
|
24
|
+
it "takes a proc and returns an array of rows" do
|
25
|
+
grade_range_parser_proc = lambda do |row|
|
26
|
+
row['range'].split('-').map do |subrange|
|
27
|
+
virtual_row = row.dup
|
28
|
+
virtual_row.delete 'range'
|
29
|
+
virtual_row['grade'] = subrange
|
30
|
+
virtual_row
|
31
|
+
end
|
32
|
+
end
|
33
|
+
t = RemoteTable.new "file://#{File.expand_path('../data/ranges.csv', __FILE__)}", parser: grade_range_parser_proc
|
34
|
+
t[0].must_equal 'description' => 'great', 'grade' => 'A'
|
35
|
+
t[1].must_equal 'description' => 'great', 'grade' => 'B'
|
36
|
+
t[2].must_equal 'description' => 'ok', 'grade' => 'C'
|
37
|
+
t[3].must_equal 'description' => 'bad', 'grade' => 'D'
|
38
|
+
t[4].must_equal 'description' => 'bad', 'grade' => 'F'
|
39
|
+
end
|
40
|
+
|
23
41
|
end
|
24
42
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: remote_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.0.
|
4
|
+
version: 3.0.0.rc1
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-08-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|