Module: Aspera::UriReader

Defined in:
lib/aspera/uri_reader.rb

Overview

Read some content from some URI, support file: , http: and https: schemes

Class Method Summary collapse

Class Method Details

.read(uri_to_read) ⇒ Object

Read some content from some URI, support file: , http: and https: schemes



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/aspera/uri_reader.rb', line 17

def read(uri_to_read)
  uri = URI.parse(uri_to_read)
  case uri.scheme
  when 'http', 'https'
    return Rest.new(base_url: uri_to_read, redirect_max: 5).call(operation: 'GET', headers: {'Accept' => '*/*'})[:data]
  when SCHEME_FILE, NilClass
    local_file_path = uri.path
    raise Error, 'URL shall have a path, check syntax' if local_file_path.nil?
    local_file_path = File.expand_path(local_file_path.gsub(%r{^/}, '')) if %r{^/(~|.|..)/}.match?(local_file_path)
    return File.read(local_file_path)
  else Aspera.error_unexpected_value(uri.scheme){"scheme for [#{uri_to_read}]"}
  end
end

.read_as_file(url) ⇒ Object

Returns Path to file with content at URL.

Returns:

  • Path to file with content at URL



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/aspera/uri_reader.rb', line 32

def read_as_file(url)
  if url.start_with?(SCHEME_FILE_PFX1)
    # for file scheme, return directly the path
    # require specific file scheme: the path part is "relative", or absolute if there are 4 slash
    raise "use format: #{SCHEME_FILE_PFX2}<path>" unless url.start_with?(SCHEME_FILE_PFX2)
    return File.expand_path(url[SCHEME_FILE_PFX2.length..-1])
  else
    # download to temp file
    # auto-delete on exit
    sdk_archive_path = TempFileManager.instance.new_file_path_global(suffix: File.basename(url))
    Aspera::Rest.new(base_url: url, redirect_max: 3).call(operation: 'GET', save_to_file: sdk_archive_path)
    return sdk_archive_path
  end
end