Module: TaliaUtil::IoHelper
- Included in:
- TaliaCore::ActiveSourceParts::Xml::GenericReader, TaliaCore::ActiveSourceParts::Xml::GenericReader, TaliaCore::DataTypes::FileRecord, ImportJobHelper
- Defined in:
- lib/talia_util/io_helper.rb
Overview
Import data files into the Talia store. This can be used to bootstrap simple installations
Instance Method Summary collapse
-
#base_for(url) ⇒ Object
Will try to figure out the “base” (that is the parent directory or path) If the base is a directory, this will return the directory name, but if it is an URL, this will return an URI object.
-
#file_url(uri) ⇒ Object
Get the “file url” for the given url, stripping a possible file:// from the front.
-
#open_from_url(url, options = {}) ⇒ Object
Opens the given (web) URL, using URL encoding and necessary substitutions.
-
#open_generic(url, options = {}) ⇒ Object
Generic “open” method for files and urls.
Instance Method Details
#base_for(url) ⇒ Object
Will try to figure out the “base” (that is the parent directory or path) If the base is a directory, this will return the directory name, but if it is an URL, this will return an URI object.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/talia_util/io_helper.rb', line 27 def base_for(url) url = file_url(url) if(File.exist?(url)) file = File.(url) File.directory?(file) ? file : File.dirname(file) else uri = URI.parse(url) # Remove everything after the last '/' uri.path.gsub!(/\/[^\/]+\Z/, '/') uri.fragment = nil uri end end |
#file_url(uri) ⇒ Object
Get the “file url” for the given url, stripping a possible file:// from the front
69 70 71 |
# File 'lib/talia_util/io_helper.rb', line 69 def file_url(uri) uri.gsub(/\Afile:\/\//, '') end |
#open_from_url(url, options = {}) ⇒ Object
Opens the given (web) URL, using URL encoding and necessary substitutions. The user must pass a block which will receive the io object from the url.
The options may contain the http authentication information and such. See the documentation for open-uri for more information. Example for options:
:http_basic_authentication => [login, password]
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/talia_util/io_helper.rb', line 49 def open_from_url(url, = {}) # Encode the URI (the inner decode will save already-encoded URI and should # do nothing to non-encoded URIs) url = URI.encode(URI.decode(url)) url.gsub!(/\[/, '%5B') # URI class doesn't like unescaped brackets url.gsub!(/\]/, '%5D') open_args = [ url ] open_args << if() begin open(*open_args) do |io| yield(io) end rescue Exception => e raise(IOError, "Error loading #{url} (when file: #{url}, open_args: [#{open_args.join(', ')}]) #{e}") end end |
#open_generic(url, options = {}) ⇒ Object
Generic “open” method for files and urls. This won’t choke on file:// URLs and will do some extra escaping on the URL.
See open_from_url for an explanation of the options
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/talia_util/io_helper.rb', line 13 def open_generic(url, = {}) url = file_url(url) # Even though open-uri would also open local files, we avoid to mangle # the URL in open_from_url if(File.exist?(url)) File.open(url) { |io| yield(io) } else open_from_url(url, ) { |io| yield(io) } end end |