Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix warnings caused by Ruby 2.7 update #530

Merged
merged 7 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ rvm:
- 2.4
- 2.5
- 2.6
- 2.7
- ruby-head
- jruby-9.1.6.0
env:
Expand Down
14 changes: 13 additions & 1 deletion lib/roo/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ def download_uri(uri, tmpdir)
tempfilename = File.join(tmpdir, find_basename(uri))
begin
File.open(tempfilename, "wb") do |file|
open(uri, "User-Agent" => "Ruby/#{RUBY_VERSION}") do |net|
open_uri(uri, "User-Agent" => "Ruby/#{RUBY_VERSION}") do |net|
file.write(net.read)
end
end
Expand All @@ -554,6 +554,18 @@ def download_uri(uri, tmpdir)
tempfilename
end

# Kernel#open re-defined by 'open-uri' is deprecated from Ruby 2.7.
# Before Ruby 2.5, URI.open is not defined by 'open-uri'.
# Therefore, this workaround is needed.
def open_uri(uri, options, &block)
require "open-uri"
if RUBY_VERSION >= '2.5.0'
URI.open(uri, options, &block)
else
open(uri, options, &block)
end
end

def open_from_stream(stream, tmpdir)
tempfilename = File.join(tmpdir, "spreadsheet")
File.open(tempfilename, "wb") do |file|
Expand Down
27 changes: 23 additions & 4 deletions lib/roo/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,36 @@ def read_cells(sheet = default_sheet)
def each_row(options, &block)
if uri?(filename)
each_row_using_tempdir(options, &block)
elsif is_stream?(filename_or_stream)
::CSV.new(filename_or_stream, options).each(&block)
else
::CSV.foreach(filename, options, &block)
csv_foreach(filename_or_stream, options, &block)
end
end

def each_row_using_tempdir(options, &block)
::Dir.mktmpdir(Roo::TEMP_PREFIX, ENV["ROO_TMP"]) do |tmpdir|
tmp_filename = download_uri(filename, tmpdir)
::CSV.foreach(tmp_filename, options, &block)
csv_foreach(tmp_filename, options, &block)
end
end

# From Ruby 2.5, options argument of CSV.new/CSV.foreach is a keyword argument.
# Before Ruby 2.5, that argument is a Hash.
# Therefore, this workaround can be removed if Ruby 2.3 and 2.4 are dropped.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, please remove such workaround.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I edit gemspec and .travis.yml to drop ruby 2.3 & 2.4 in this branch: ruby_27_support?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed WA for Ruby 2.3 & 2.4 and also edited gemspec and .travis.yml too.

if RUBY_VERSION >= '2.5.0'
def csv_foreach(path_or_io, options, &block)
if is_stream?(path_or_io)
::CSV.new(path_or_io, **options).each(&block)
else
::CSV.foreach(path_or_io, **options, &block)
end
end
else
def csv_foreach(path_or_io, options, &block)
if is_stream?(path_or_io)
::CSV.new(path_or_io, options).each(&block)
else
::CSV.foreach(path_or_io, options, &block)
end
end
end

Expand Down
10 changes: 8 additions & 2 deletions lib/roo/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ def extension_for(path, options)
options[:file_warning] = :ignore
extension.tr('.', '').downcase.to_sym
else
res = ::File.extname((path =~ /\A#{::URI::DEFAULT_PARSER.make_regexp}\z/) ? ::URI.parse(::URI.encode(path)).path : path)
res.tr('.', '').downcase.to_sym
parsed_path =
if path =~ /\A#{::URI::DEFAULT_PARSER.make_regexp}\z/
# path is 7th match
Regexp.last_match[7]
else
path
end
::File.extname(parsed_path).tr('.', '').downcase.to_sym
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion roo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'nokogiri', '~> 1'
spec.add_dependency 'rubyzip', '>= 1.3.0', '< 3.0.0'

spec.add_development_dependency 'rake', '~> 10.1'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'minitest', '~> 5.4', '>= 5.4.3'
spec.add_development_dependency 'rack', '~> 1.6', '< 2.0.0'
end