Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update Spreadsheet::Open to support Tempfiles. Fixes #84.

  • Loading branch information...
commit 298237caa802db5fd1fc752bf213b497a40f353c 1 parent 321a6cb
@Empact Empact authored
View
5 CHANGELOG
@@ -1,3 +1,8 @@
+== unreleased
+
+* enhancements
+ * Extend Roo::Spreadsheet.open to accept Tempfiles and other arguments responding to #path. Note they require an :extension option to be declared, as the tempfile mangles the extension. #84.
+
== 1.13.2 2013-12-23
* bugfixes
View
7 lib/roo/spreadsheet.rb
@@ -2,12 +2,12 @@ module Roo
class Spreadsheet
class << self
def open(file, options = {})
- file = File === file ? file.path : file
+ file = file.respond_to?(:path) ? file.path : file
extension =
if options[:extension]
options[:file_warning] = :ignore
- ".#{options[:extension]}".gsub(/[.]+/, ".")
+ ".#{options.delete(:extension)}".gsub(/[.]+/, ".")
else
File.extname(URI.parse(file).path)
end
@@ -26,7 +26,8 @@ def open(file, options = {})
when '.csv'
Roo::CSV.new(file, options)
else
- raise ArgumentError, "Don't know how to open file #{file}"
+ raise ArgumentError,
+ "Can't detect the type of #{file} - please use the :extension option to declare its type."
end
end
end
View
20 spec/lib/roo/spreadsheet_spec.rb
@@ -11,6 +11,16 @@
end
end
+ context 'for a tempfile' do
+ let(:tempfile) { Tempfile.new('foo.csv') }
+ let(:filename) { tempfile.path }
+
+ it 'loads the proper type' do
+ expect(Roo::CSV).to receive(:new).with(filename, file_warning: :ignore).and_call_original
+ expect(Roo::Spreadsheet.open(tempfile, extension: 'csv')).to be_a(Roo::CSV)
+ end
+ end
+
context 'for a url' do
context 'that is csv' do
let(:filename) { 'http://example.com/file.csv?with=params#and=anchor' }
@@ -26,18 +36,18 @@
let(:filename) { 'file.csv' }
let(:options) { {csv_options: {col_sep: '"'}} }
- context 'with options' do
- it 'passes the options through' do
+ context 'with csv_options' do
+ it 'passes the csv_options through' do
expect(Roo::CSV).to receive(:new).with(filename, options)
Roo::Spreadsheet.open(filename, options)
end
end
end
- context 'when the file extension' do
+ context 'with a file extension option' do
let(:filename) { 'file.xls' }
- context "is xls" do
+ context "xls" do
let(:options) { { extension: "xls" } }
it 'loads with xls extension options' do
@@ -46,7 +56,7 @@
end
end
- context "is .xls" do
+ context ".xls" do
let(:options) { { extension: ".xls" } }
it 'loads with .xls extension options' do
Please sign in to comment.
Something went wrong with that request. Please try again.