Class: BrowseEverything::Retriever
- Inherits:
-
Object
- Object
- BrowseEverything::Retriever
- Defined in:
- lib/browse_everything/retriever.rb
Overview
Class for retrieving a file or resource from a storage provider
Constant Summary collapse
- CHUNK_SIZE =
16384
Instance Attribute Summary collapse
-
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
Class Method Summary collapse
-
.can_retrieve?(uri, headers = {}) ⇒ Boolean
Determines whether or not a remote resource can be retrieved.
Instance Method Summary collapse
-
#download(spec, target = nil) ⇒ Object
Download a file or resource.
-
#initialize ⇒ Retriever
constructor
Constructor.
-
#retrieve(options, &block) ⇒ Object
Retrieve the resource from the storage service.
Constructor Details
#initialize ⇒ Retriever
Constructor
45 46 47 |
# File 'lib/browse_everything/retriever.rb', line 45 def initialize @chunk_size = CHUNK_SIZE end |
Instance Attribute Details
#chunk_size ⇒ Object
Returns the value of attribute chunk_size.
31 32 33 |
# File 'lib/browse_everything/retriever.rb', line 31 def chunk_size @chunk_size end |
Class Method Details
.can_retrieve?(uri, headers = {}) ⇒ Boolean
Determines whether or not a remote resource can be retrieved
37 38 39 40 41 |
# File 'lib/browse_everything/retriever.rb', line 37 def can_retrieve?(uri, headers = {}) request_headers = headers.merge(Range: 'bytes=0-0') response = Faraday.get(uri, headers: request_headers) response.success? end |
Instance Method Details
#download(spec, target = nil) ⇒ Object
Download a file or resource
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/browse_everything/retriever.rb', line 52 def download(spec, target = nil) if target.nil? ext = File.extname(spec['file_name']) base = File.basename(spec['file_name'], ext) target = Dir::Tmpname.create([base, ext]) {} end File.open(target, 'wb') do |output| retrieve(spec) do |chunk, retrieved, total| output.write(chunk) yield(target, retrieved, total) if block_given? end end target end |
#retrieve(options, &block) ⇒ Object
Retrieve the resource from the storage service
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/browse_everything/retriever.rb', line 70 def retrieve(, &block) expiry_time_value = .fetch('expires', nil) if expiry_time_value expiry_time = Time.parse(expiry_time_value) raise ArgumentError, "Download expired at #{expiry_time}" if expiry_time < Time.now end = () url = [:url] case url.scheme when 'file' retrieve_file(, &block) when /https?/ retrieve_http(, &block) else raise URI::BadURIError, "Unknown URI scheme: #{url.scheme}" end end |