Skip to content

Commit

Permalink
Update Spreadsheet::Open to support Tempfiles. Fixes #84.
Browse files Browse the repository at this point in the history
  • Loading branch information
Empact committed Dec 23, 2013
1 parent 321a6cb commit 298237c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/roo/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 15 additions & 5 deletions spec/lib/roo/spreadsheet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand All @@ -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
Expand All @@ -46,7 +56,7 @@
end
end

context "is .xls" do
context ".xls" do
let(:options) { { extension: ".xls" } }

it 'loads with .xls extension options' do
Expand Down

0 comments on commit 298237c

Please sign in to comment.