smarter_csv 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -151,6 +151,7 @@ The options and the block are optional.
151
151
  | :headers_in_file | true | Whether or not the file contains headers as the first line. |
152
152
  | | | Important if the file does not contain headers, |
153
153
  | | | otherwise you would lose the first line of data. |
154
+ | :file_encoding | utf-8 | Set the file encoding eg.: 'windows-1252' or 'iso-8859-1' |
154
155
 
155
156
 
156
157
  #### NOTES about CSV Headers:
@@ -196,6 +197,12 @@ Or install it yourself as:
196
197
 
197
198
  ## Changes
198
199
 
200
+ #### 1.0.7 (2013-05-20)
201
+
202
+ * allowing process to work with objects with a 'readline' method (thanks to taq)
203
+ * added options:
204
+ * :file_encoding : defaults to utf8 (thanks to MrTin, Paxa)
205
+
199
206
  #### 1.0.6 (2013-05-19)
200
207
 
201
208
  * bugfix : quoted fields are now correctly parsed
@@ -3,11 +3,11 @@ module SmarterCSV
3
3
  class HeaderSizeMismatch < Exception
4
4
  end
5
5
 
6
- def SmarterCSV.process(filename, options={}, &block)
6
+ def SmarterCSV.process(input, options={}, &block) # first parameter: filename or input object with readline method
7
7
  default_options = {:col_sep => ',' , :row_sep => $/ , :quote_char => '"',
8
8
  :remove_empty_values => true, :remove_zero_values => false , :remove_values_matching => nil , :remove_empty_hashes => true , :strip_whitespace => true,
9
9
  :convert_values_to_numeric => true, :strip_chars_from_headers => nil , :user_provided_headers => nil , :headers_in_file => true,
10
- :comment_regexp => /^#/, :chunk_size => nil , :key_mapping_hash => nil , :downcase_header => true, :strings_as_keys => false
10
+ :comment_regexp => /^#/, :chunk_size => nil , :key_mapping_hash => nil , :downcase_header => true, :strings_as_keys => false, :file_encoding => 'utf-8'
11
11
  }
12
12
  options = default_options.merge(options)
13
13
  headerA = []
@@ -15,7 +15,7 @@ module SmarterCSV
15
15
  old_row_sep = $/
16
16
  begin
17
17
  $/ = options[:row_sep]
18
- f = File.open(filename, "r")
18
+ f = input.respond_to?(:readline) ? input : File.open(input, "r:#{options[:file_encoding]}")
19
19
 
20
20
  if options[:headers_in_file] # extract the header line
21
21
  # process the header line in the CSV file..
@@ -38,7 +38,7 @@ module SmarterCSV
38
38
  headerA = options[:user_provided_headers]
39
39
  if defined?(file_header_size) && ! file_header_size.nil?
40
40
  if headerA.size != file_header_size
41
- raise SmarterCSV::HeaderSizeMismatch , "ERROR [smarter_csv]: :user_provided_headers defines #{headerA.size} headers != CSV-file #{filename} has #{file_header_size} headers"
41
+ raise SmarterCSV::HeaderSizeMismatch , "ERROR [smarter_csv]: :user_provided_headers defines #{headerA.size} headers != CSV-file #{input} has #{file_header_size} headers"
42
42
  else
43
43
  # we could print out the mapping of file_headerA to headerA here
44
44
  end
@@ -1,3 +1,3 @@
1
1
  module SmarterCSV
2
- VERSION = "1.0.6"
2
+ VERSION = "1.0.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarter_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: