lib/deas/url.rb in deas-0.34.0 vs lib/deas/url.rb in deas-0.35.0
- old
+ new
@@ -1,14 +1,22 @@
-require 'deas/cgi'
-
module Deas
class Url
+ def self.http_query(hash, &escape_value_proc)
+ escape_value_proc ||= proc{ |v| v.to_s }
+ hash.map do |(key, value)|
+ "#{key}=#{escape_value_proc.call(value)}"
+ end.sort.join('&')
+ end
+
attr_reader :name, :path
+ attr_reader :escape_query_value_proc
- def initialize(name, path)
+ def initialize(name, path, options = nil)
+ options ||= {}
@name, @path = name, path
+ @escape_query_value_proc = options[:escape_query_value]
end
def path_for(*args)
hashed, ordered = [
args.last.kind_of?(::Hash) ? args.pop : {},
@@ -49,10 +57,12 @@
end
end
end
def apply_extra(path, params)
- params.empty? ? path : "#{path}?#{Deas::Cgi.http_query(params)}"
+ return path if params.empty?
+ query_string = Deas::Url.http_query(params, &self.escape_query_value_proc)
+ "#{path}?#{query_string}"
end
def apply_anchor(path, anchor)
anchor.to_s.empty? ? path : "#{path}##{anchor}"
end