Class: Geoblacklight::Download
- Inherits:
-
Object
- Object
- Geoblacklight::Download
show all
- Defined in:
- lib/geoblacklight/download.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(document, options = {}) ⇒ Download
Returns a new instance of Download.
3
4
5
6
|
# File 'lib/geoblacklight/download.rb', line 3
def initialize(document, options = {})
@document = document
@options = options
end
|
Class Method Details
.file_path ⇒ Object
16
17
18
|
# File 'lib/geoblacklight/download.rb', line 16
def self.file_path
Settings.DOWNLOAD_PATH || "#{Rails.root}/tmp/cache/downloads"
end
|
Instance Method Details
#create_download_file ⇒ String
Creates temporary file on file system and renames it if download completes successfully. Will raise and rescue if the wrong file format is downloaded
#download_exists? ⇒ Boolean
24
25
26
|
# File 'lib/geoblacklight/download.rb', line 24
def download_exists?
File.file?(file_path_and_name)
end
|
#downloadable? ⇒ Boolean
8
9
10
|
# File 'lib/geoblacklight/download.rb', line 8
def downloadable?
@document.downloadable?
end
|
#file_name ⇒ Object
12
13
14
|
# File 'lib/geoblacklight/download.rb', line 12
def file_name
"#{@document.id}-#{@options[:type]}.#{@options[:extension]}"
end
|
#file_path_and_name ⇒ Object
20
21
22
|
# File 'lib/geoblacklight/download.rb', line 20
def file_path_and_name
"#{self.class.file_path}/#{file_name}"
end
|
#get ⇒ Object
28
29
30
31
32
33
34
|
# File 'lib/geoblacklight/download.rb', line 28
def get
if download_exists?
file_name
else
create_download_file
end
end
|
#initiate_download ⇒ Faraday::Request
Initiates download from a remote source url using the ‘request_params`. Will catch Faraday::Error::ConnectionFailed and Faraday::Error::TimeoutError
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/geoblacklight/download.rb', line 61
def initiate_download
conn = Faraday.new(url: url)
conn.get do |request|
request.params = @options[:request_params]
request.options.timeout = timeout
request.options.open_timeout = timeout
end
rescue Faraday::Error::ConnectionFailed
raise Geoblacklight::Exceptions::ExternalDownloadFailed,
message: 'Download connection failed',
url: conn.url_prefix.to_s
rescue Faraday::Error::TimeoutError
raise Geoblacklight::Exceptions::ExternalDownloadFailed,
message: 'Download timed out',
url: conn.url_prefix.to_s
end
|
#url_with_params ⇒ String
Creates a download url for the object
81
82
83
|
# File 'lib/geoblacklight/download.rb', line 81
def url_with_params
url + '/?' + URI.encode_www_form(@options[:request_params])
end
|