Module: URITemplate::Utils
- Extended by:
- Utils
- Includes:
- Escaping::EscapeUtils, Escaping::Pure, StringEncoding
- Included in:
- Utils
- Defined in:
- lib/uri_template/utils.rb
Overview
A collection of some utility methods. The most methods are used to parse or generate uri-parameters. I will use the escape_utils library if available, but runs happily without.
Defined Under Namespace
Modules: Escaping, StringEncoding
Constant Summary
- KCODE_UTF8 =
(Regexp::KCODE_UTF8 rescue 0)
Instance Method Summary (collapse)
-
- (Object) object_to_param(object)
Converts an object to a param value.
-
- (Boolean) pair_array?(a)
Returns true when the given value is an array and it only consists of arrays with two items.
-
- (Object) pair_array_to_hash(x, careful = false)
Turns the given value into a hash if it is an array of pairs.
- - (Object) pair_array_to_hash2(x)
-
- (Boolean) use_unicode?
private
Should we use u....
Methods included from Escaping::Pure
#escape_uri, #escape_url, #unescape_uri, #unescape_url, #using_escape_utils?
Methods included from StringEncoding
#force_utf8, #force_utf8_encode, #force_utf8_fallback, #to_ascii, #to_ascii_encode, #to_ascii_fallback, #to_utf8, #to_utf8_encode, #to_utf8_fallback
Methods included from Escaping::EscapeUtils
#escape_uri, #escape_url, #unescape_uri, #unescape_url, #using_escape_utils?
Instance Method Details
- (Object) object_to_param(object)
Converts an object to a param value. Tries to call :to_param and then :to_s on that object.
255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/uri_template/utils.rb', line 255 def object_to_param(object) if object.respond_to? :to_param object.to_param elsif object.respond_to? :to_s object.to_s else raise Unconvertable.new(object) end rescue NoMethodError raise Unconvertable.new(object) end |
- (Boolean) pair_array?(a)
Returns true when the given value is an array and it only consists of arrays with two items. This useful when using a hash is not ideal, since it doesn't allow duplicate keys.
283 284 285 286 |
# File 'lib/uri_template/utils.rb', line 283 def pair_array?(a) return false unless a.kind_of? Array return a.all?{|p| p.kind_of? Array and p.size == 2 } end |
- (Object) pair_array_to_hash(x, careful = false)
Turns the given value into a hash if it is an array of pairs. Otherwise it returns the value. You can test whether a value will be converted with #pair_array?.
303 304 305 306 307 308 309 |
# File 'lib/uri_template/utils.rb', line 303 def pair_array_to_hash(x, careful = false ) if careful ? pair_array?(x) : (x.kind_of?(Array) and ( x.empty? or x.first.kind_of?(Array) ) ) return Hash[ x ] else return x end end |
- (Object) pair_array_to_hash2(x)
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/uri_template/utils.rb', line 314 def pair_array_to_hash2(x) c = {} result = [] x.each do | (k,v) | e = c[k] if !e result << c[k] = [k,v] else e[1] = [e[1]] unless e[1].kind_of? Array e[1] << v end end return result end |
- (Boolean) use_unicode?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Should we use u.... or x.. in regexps?
269 270 271 272 273 |
# File 'lib/uri_template/utils.rb', line 269 def use_unicode? return eval('Regexp.compile("\u0020")') =~ " " rescue SyntaxError false end |