diff --git a/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache b/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache index 54854efe332f..d61627fb26ee 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/api_client.mustache @@ -170,23 +170,28 @@ module {{moduleName}} # from the "Content-Disposition" header if provided, otherwise a random filename. # # @see Configuration#temp_folder_path - # @return [File] the file downloaded + # @return [Tempfile] the file downloaded def download_file(response) - tmp_file = Tempfile.new '', @config.temp_folder_path content_disposition = response.headers['Content-Disposition'] if content_disposition filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] - path = File.join File.dirname(tmp_file), filename + prefix = File.basename(filename) else - path = tmp_file.path + prefix = 'download-' end - # close and delete temp file - tmp_file.close! + prefix = prefix + '-' unless prefix.end_with?('-') - File.open(path, 'w') { |file| file.write(response.body) } - @config.logger.info "File written to #{path}. Please move the file to a proper folder "\ - "for further processing and delete the temp afterwards" - File.new(path) + tempfile = nil + encoding = response.body.encoding + Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file| + file.write(response.body) + tempfile = file + end + @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\ + "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ + "will be deleted automatically with GC. It's also recommended to delete the temp file "\ + "explicitly with `tempfile.delete`" + tempfile end def build_request_url(path) diff --git a/samples/client/petstore/ruby/lib/petstore/api_client.rb b/samples/client/petstore/ruby/lib/petstore/api_client.rb index 53be21db3b0f..2706bbdb325b 100644 --- a/samples/client/petstore/ruby/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby/lib/petstore/api_client.rb @@ -170,23 +170,28 @@ module Petstore # from the "Content-Disposition" header if provided, otherwise a random filename. # # @see Configuration#temp_folder_path - # @return [File] the file downloaded + # @return [Tempfile] the file downloaded def download_file(response) - tmp_file = Tempfile.new '', @config.temp_folder_path content_disposition = response.headers['Content-Disposition'] if content_disposition filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1] - path = File.join File.dirname(tmp_file), filename + prefix = File.basename(filename) else - path = tmp_file.path + prefix = 'download-' end - # close and delete temp file - tmp_file.close! + prefix = prefix + '-' unless prefix.end_with?('-') - File.open(path, 'w') { |file| file.write(response.body) } - @config.logger.info "File written to #{path}. Please move the file to a proper folder "\ - "for further processing and delete the temp afterwards" - File.new(path) + tempfile = nil + encoding = response.body.encoding + Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding) do |file| + file.write(response.body) + tempfile = file + end + @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\ + "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\ + "will be deleted automatically with GC. It's also recommended to delete the temp file "\ + "explicitly with `tempfile.delete`" + tempfile end def build_request_url(path) diff --git a/samples/client/petstore/ruby/petstore.gemspec b/samples/client/petstore/ruby/petstore.gemspec index df8a6df24d0b..d9bb07c1902f 100644 --- a/samples/client/petstore/ruby/petstore.gemspec +++ b/samples/client/petstore/ruby/petstore.gemspec @@ -6,12 +6,13 @@ Gem::Specification.new do |s| s.name = "petstore" s.version = Petstore::VERSION s.platform = Gem::Platform::RUBY - s.authors = ["Zeke Sikelianos", "Tony Tam"] - s.email = ["zeke@wordnik.com", "fehguy@gmail.com"] - s.homepage = "http://swagger.io" - s.summary = %q{A ruby wrapper for the swagger APIs} - s.description = %q{This gem maps to a swagger API} - s.license = "Apache-2.0" + s.authors = [""] + s.email = [""] + s.homepage = "" + s.summary = "" + s.description = "" + s.license = "" + s.add_runtime_dependency 'typhoeus', '~> 0.2', '>= 0.2.1' s.add_runtime_dependency 'json', '~> 1.4', '>= 1.4.6'