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



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

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 'file', NilClass
    local_file_path = uri.path
    raise '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



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/aspera/uri_reader.rb', line 30

def read_as_file(url)
  if url.start_with?('file:')
    # require specific file scheme: the path part is "relative", or absolute if there are 4 slash
    raise "use format: #{FILE_SCHEME_PREFIX}<path>" unless url.start_with?(FILE_SCHEME_PREFIX)
    return File.expand_path(url[FILE_SCHEME_PREFIX.length..-1])
  else
    # autodelete 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