Module: TaliaUtil::IoHelper
- Included in:
- TaliaCore::ActiveSourceParts::Rdf::RdfReader, TaliaCore::ActiveSourceParts::Rdf::RdfReader, TaliaCore::ActiveSourceParts::Xml::GenericReader, TaliaCore::ActiveSourceParts::Xml::GenericReader, TaliaCore::DataTypes::FileRecord, ImportJobHelper
- Defined in:
- lib/talia_util/io_helper.rb
Overview
Collection of IO methods than can be mixed into other classes
Instance Method Summary collapse
-
#base_for(url) ⇒ Object
Will try to figure out the “base”, which is the parent directory or path.
-
#file_url(uri) ⇒ Object
Get the “file url” for the given uri, 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”, which 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.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/talia_util/io_helper.rb', line 32 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 uri, stripping a possible file:// from the front
80 81 82 |
# File 'lib/talia_util/io_helper.rb', line 80 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]
Example:
open_from_url('http://foobar.com/', :http_basic_authentication => ['user', 'secret']) do |io|
...
end
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/talia_util/io_helper.rb', line 60 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.
Usage:
open_generic('someurl.foo') do |io|
...
end
See open_from_url for an explanation of the options
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/talia_util/io_helper.rb', line 18 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 |