Class: BrowseEverything::Retriever

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRetriever

Constructor


40
41
42
# File 'lib/browse_everything/retriever.rb', line 40

def initialize
  @chunk_size = CHUNK_SIZE
end

Instance Attribute Details

#chunk_sizeObject

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) ⇒ Boolean


34
35
36
# File 'lib/browse_everything/retriever.rb', line 34

def can_retrieve?(uri)
  Typhoeus.get(uri, headers: { Range: 'bytes=0-0' }).success?
end

Instance Method Details

#download(spec, target = nil) ⇒ Object

Download a file or resource


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/browse_everything/retriever.rb', line 47

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


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/browse_everything/retriever.rb', line 65

def retrieve(options, &block)
  expiry_time_value = options.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

  download_options = extract_download_options(options)
  url = download_options[:url]

  case url.scheme
  when 'file'
    retrieve_file(download_options, &block)
  when /https?/
    retrieve_http(download_options, &block)
  else
    raise URI::BadURIError, "Unknown URI scheme: #{url.scheme}"
  end
end