Module: CGI::QueryString

Defined in:
lib/cgi/query_string.rb,
lib/cgi/query_string/version.rb

Overview

:nodoc:

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.build_params(prefix, obj, add) ⇒ Object

Builds params

Helper function

  • Args :

    • string -> Hash to serialize

    • Hash -> Hash to serialize

    • lambda -> Hash to serialize

  • Returns :

    • false



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cgi/query_string.rb', line 46

def build_params(prefix, obj, add)
	if obj.is_a?(Array)
		obj.each { |v, i|
			if !(/\[\]$/.match(prefix).nil?)
				add.call(prefix, v)
			else
				build_params("#{prefix}[#{ ((v.is_a?(Array) || v.is_a?(Hash)) ? i : "") }]", v, add)
			end
		}
	elsif obj.is_a?(Hash)
		obj.each_pair { |name,o|
			build_params("#{prefix}[#{name}]", o, add)
		}
	else
		add.call(prefix, obj)
	end

	true
end

.param(a) ⇒ Object

Serialize an array into a query string

Ruby version of jQuerys param method

  • Args :

    • hash -> Hash to serialize

  • Returns :

    • a query string



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cgi/query_string.rb', line 16

def param(a)
	s = []
	add = lambda { |k,v|
		s << "#{::CGI.escape(k.to_s)}=#{::CGI.escape(v.to_s)}"
	}

	if a.is_a?(Array)
		a.each { |v,i|
			self.build_params("", v)
		}
	elsif a.is_a?(Hash)
		a.each_pair { |prefix,v|
			build_params(prefix, v, add)
		}
	end

	s.join("&").gsub(/%20/, "+")
end