Sha256: 48aedd6680996b7948cbf6ab8768480504fbdd6af52be704ca3ee2117c9f40da
Contents?: true
Size: 1.69 KB
Versions: 4
Compression:
Stored size: 1.69 KB
Contents
require 'open-uri' module TaliaUtil # Import data files into the Talia store. This can be used to bootstrap # simple installations module IoHelper # 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 def open_generic(url, options = {}) 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, options) { |io| yield(io) } end end # 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] def open_from_url(url, options = {}) url = URI.encode(url) url.gsub!(/\[/, '%5B') # URI class doesn't like unescaped brackets url.gsub!(/\]/, '%5D') open_args = [ url ] open_args << options if(options) 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 # Get the "file url" for the given url, stripping a possible file:// from the front def file_url(uri) uri.gsub(/\Afile:\/\//, '') end end # End modules end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
talia_core-0.4.3 | lib/talia_util/io_helper.rb |
talia_core-0.4.2 | lib/talia_util/io_helper.rb |
talia_core-0.4.1 | lib/talia_util/io_helper.rb |
talia_core-0.4.0 | lib/talia_util/io_helper.rb |