lib/rufus/verbs.rb in rufus-verbs-0.7 vs lib/rufus/verbs.rb in rufus-verbs-0.8

- old
+ new

@@ -85,8 +85,40 @@ def options (*args) EndPoint.request :options, args end + # + # Opens a file or a URI (GETs it and return the reply). Will tolerate + # a HTTP[S] URI or a file path. + # + # It is not named open() in order not to collide with File.open and + # open-uri's open. + # + def fopen (uri, *args, &block) + + u = URI.parse uri.to_s + + return File.open(uri.to_s, &block) \ + if u.scheme == nil + + return File.open(uri.to_s[5..-1], &block) \ + if u.scheme == 'file' + + if u.scheme == 'http' or u.scheme == 'https' + + r = EndPoint.request(:get, [ uri ] + args) \ + + if block + block.call r + return + else + return r + end + end + + raise "can't handle scheme '#{u.scheme}' for #{u.to_s}" + end + extend self end