Object
Inspiration for this is taken straight from Usher. github.com/joshbuddy/usher
# File lib/url_mount.rb, line 10 def initialize(path, opts = {}, &blk) @raw_path, @options = path, opts @url_split_regex = Regexp.new("[^#{DELIMETERS.collect{|d| Regexp.quote(d)}.join}]+|[#{DELIMETERS.collect{|d| Regexp.quote(d)}.join}]") @host, @scheme = opts[:host], opts[:scheme] @callbacks = [] @callbacks << blk if blk end
# File lib/url_mount.rb, line 18 def callback(&blk) @callbacks << blk if blk @callbacks end
# File lib/url_mount.rb, line 23 def local_segments @local_segments || parse_local_segments end
# File lib/url_mount.rb, line 49 def optional_variable_segments @optional_variable_segments ||= begin local_segments.map{|s| s.optional_variable_segments}.flatten.compact end end
# File lib/url_mount.rb, line 37 def optional_variables @optional_variables ||= begin optional_variable_segments.map{|s| s.name} end end
# File lib/url_mount.rb, line 43 def required_variable_segments @required_variable_segments ||= begin local_segments.map{|s| s.required_variable_segments}.flatten.compact end end
# File lib/url_mount.rb, line 31 def required_variables @required_variables ||= begin required_variable_segments.map{|s| s.name} end end
# File lib/url_mount.rb, line 64 def url(env = {}, opts = {}) unless env.key?('rack.version') opts = env env = nil end @callbacks.each{|blk| blk.call(env,opts)} if env requirements_met = (local_required_variables - (opts.keys + options.keys)).empty? if !required_to_generate? && !requirements_met nil else raise Ungeneratable, "Missing required variables" if !requirements_met path = local_segments.inject([]){|url, segment| str = segment.to_s(opts); url << str if str; url}.join match = /(.*?)\/?$/.match(path) result = match[1] path = url_mount.nil? ? result : File.join(url_mount.to_s(opts), result) _host = opts.delete(:host) || host _scheme = opts.delete(:scheme) || scheme if _host || _scheme _scheme ||= "http" raise Ungeneratable, "Missing host when generating absolute url" if _scheme && !_host uri = URI.parse(path) uri.host = _host uri.scheme = _scheme || "http" uri.to_s else path end end end
Generated with the Darkfish Rdoc Generator 2.