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
-
.file?(url) ⇒ Boolean
True if the URL is a file:// URL.
-
.file_path(url) ⇒ String
The path of a file:// URL.
-
.file_url(path) ⇒ String
A file:// URL for the given path.
-
.read(uri_to_read) ⇒ Object
Read some content from some URI, support file: , http: and https: schemes.
-
.read_as_file(url) ⇒ Object
Path to file with content at URL.
Class Method Details
.file?(url) ⇒ Boolean
Returns true if the URL is a file:// URL.
18 19 20 |
# File 'lib/aspera/uri_reader.rb', line 18 def file?(url) url.start_with?(SCHEME_FILE_PFX2) end |
.file_path(url) ⇒ String
Returns the path of a file:// URL.
28 29 30 31 |
# File 'lib/aspera/uri_reader.rb', line 28 def file_path(url) Aspera.assert(file?(url)){"use format: #{file_url('<path>')}"} File.(url[SCHEME_FILE_PFX2.length..-1]) end |
.file_url(path) ⇒ String
Returns a file:// URL for the given path.
23 24 25 |
# File 'lib/aspera/uri_reader.rb', line 23 def file_url(path) return "#{SCHEME_FILE_PFX2}#{path}" end |
.read(uri_to_read) ⇒ Object
Read some content from some URI, support file: , http: and https: schemes
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/aspera/uri_reader.rb', line 34 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).read(nil, headers: {'Accept' => '*/*'}) when 'data' , encoded_data = uri.opaque.split(',', 2) if .end_with?(';base64') Base64.decode64(encoded_data) else URI.decode_www_form_component(encoded_data) end 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.(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.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/aspera/uri_reader.rb', line 56 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 return file_path(url) elsif url.start_with?('data:') # download to temp file # auto-delete on exit temp_file = TempFileManager.instance.new_file_path_global('uri_reader') File.write(temp_file, read(url), binmode: true) return temp_file else # download to temp file # auto-delete on exit temp_file = 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: temp_file) return temp_file end end |