remote_table 3.0.0.beta → 3.0.0.rc1

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.
data/CHANGELOG CHANGED
@@ -1,3 +1,10 @@
1
+ 3.0.0.rc1 / 2013-08-08
2
+
3
+ * Breaking changes
4
+
5
+ * :parser object must respond to #call(row), not #parse(row)
6
+ * Whitespace now automatically "normalized" (/\s+/ -> ' ', stripped) for CSV
7
+
1
8
  3.0.0.beta / 2013-07-30
2
9
 
3
10
  * Breaking changes
@@ -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 parse(row)
355
+ def call(row)
347
356
  [row]
348
357
  end
349
358
  end
350
359
 
351
- # An object that responds to #parse(row) and returns an array of one or more rows.
360
+ # An object that responds to #call(row) and returns an array of one or more rows.
352
361
  #
353
- # @return [#parse]
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.parse(row).each do |virtual_row|
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.to_s
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.to_s
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 = v.to_s.gsub(/\s+/, ' ').strip
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.gsub! WHITESPACE, SINGLE_SPACE
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
@@ -1,3 +1,3 @@
1
1
  class RemoteTable
2
- VERSION = '3.0.0.beta'
2
+ VERSION = '3.0.0.rc1'
3
3
  end
@@ -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 #parse(row) and returns an array of rows" do
5
+ it "takes a parser object that responds to #call(row) and returns an array of rows" do
6
6
  class GradeRangeParser
7
- def parse(row)
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.beta
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-07-31 00:00:00.000000000 Z
13
+ date: 2013-08-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport