lib/geoblacklight/download.rb in geoblacklight-4.0.0 vs lib/geoblacklight/download.rb in geoblacklight-4.1.0

- old
+ new

@@ -1,6 +1,7 @@ # frozen_string_literal: true + module Geoblacklight class Download def initialize(document, options = {}) @document = document @options = options @@ -13,11 +14,11 @@ def file_name "#{@document.id}-#{@options[:type]}.#{@options[:extension]}" end def self.file_path - Settings.DOWNLOAD_PATH || Rails.root.join('tmp', 'cache', 'downloads') + Settings.DOWNLOAD_PATH || Rails.root.join("tmp", "cache", "downloads") end def file_path_and_name "#{self.class.file_path}/#{file_name}" end @@ -39,21 +40,21 @@ # successfully. Will raise and rescue if the wrong file format is downloaded # @return [String] filename of the completed download def create_download_file download = initiate_download - File.open("#{file_path_and_name}.tmp", 'wb') do |file| + File.open("#{file_path_and_name}.tmp", "wb") do |file| fail Geoblacklight::Exceptions::WrongDownloadFormat unless matches_mimetype?(download) file.write download.body end File.rename("#{file_path_and_name}.tmp", file_path_and_name) file_name rescue Geoblacklight::Exceptions::WrongDownloadFormat => error Geoblacklight.logger.error "#{error} expected #{@options[:content_type]} " \ - "received #{download.headers['content-type']}" + "received #{download.headers["content-type"]}" File.delete("#{file_path_and_name}.tmp") - raise Geoblacklight::Exceptions::ExternalDownloadFailed, message: 'Wrong download type' + raise Geoblacklight::Exceptions::ExternalDownloadFailed, message: "Wrong download type" end ## # Initiates download from a remote source url using the `request_params`. # Will catch Faraday::ConnectionFailed and @@ -66,29 +67,29 @@ request.options.timeout = timeout request.options.open_timeout = timeout end rescue Faraday::ConnectionFailed raise Geoblacklight::Exceptions::ExternalDownloadFailed, - message: 'Download connection failed', - url: conn.url_prefix.to_s + message: "Download connection failed", + url: conn.url_prefix.to_s rescue Faraday::TimeoutError raise Geoblacklight::Exceptions::ExternalDownloadFailed, - message: 'Download timed out', - url: conn.url_prefix.to_s + message: "Download timed out", + url: conn.url_prefix.to_s end ## # Creates a download url for the object # @return [String] def url_with_params - url + '/?' + URI.encode_www_form(@options[:request_params]) + url + "/?" + URI.encode_www_form(@options[:request_params]) end private def matches_mimetype?(download) - MIME::Type.simplified(download.headers['content-type']) == @options[:content_type] + MIME::Type.simplified(download.headers["content-type"]) == @options[:content_type] end ## # Returns timeout for the download request. `timeout` passed as an option to # the Geoblacklight::Download class @@ -100,10 +101,10 @@ ## # URL for download # @return [String] URL that is checked in download def url url = @document.references.send(@options[:service_type]).endpoint - url += '/reflect' if @options[:reflect] + url += "/reflect" if @options[:reflect] url end end end