Module: URITemplate::Utils
- Extended by:
- Utils
- Includes:
- Escaping::EscapeUtils, Escaping::Pure, StringEncoding
- Included in:
- RFC6570::Utils, 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 collapse
- KCODE_UTF8 =
(Regexp::KCODE_UTF8 rescue 0)
Constants included from Escaping::Pure
Escaping::Pure::PCT, Escaping::Pure::URI_ESCAPED, Escaping::Pure::URL_ESCAPED
Instance Method Summary collapse
- #compact_regexp(rx) ⇒ Object private
-
#object_to_param(object) ⇒ Object
Converts an object to a param value.
-
#pair_array?(a) ⇒ Boolean
Returns true when the given value is an array and it only consists of arrays with two items.
-
#pair_array_to_hash(x, careful = false) ⇒ Object
Turns the given value into a hash if it is an array of pairs.
- #pair_array_to_hash2(x) ⇒ Object
-
#use_unicode? ⇒ Boolean
private
Should we use u.…
Methods included from Escaping::Pure
#escape_uri, #escape_url, #unescape_uri, #unescape_url, #using_escape_utils?
Methods included from Escaping::EscapeUtils
#escape_uri, #escape_url, #unescape_uri, #unescape_url, #using_escape_utils?
Methods included from StringEncoding::Fallback
#force_utf8, #to_ascii, #to_utf8
Methods included from StringEncoding::Encode
#force_utf8, #to_ascii, #to_utf8
Instance Method Details
#compact_regexp(rx) ⇒ Object
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.
328 329 330 |
# File 'lib/uri_template/utils.rb', line 328 def compact_regexp(rx) rx.split("\n").map(&:strip).join end |
#object_to_param(object) ⇒ Object
Converts an object to a param value. Tries to call :to_param and then :to_s on that object.
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/uri_template/utils.rb', line 253 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 |
#pair_array?(a) ⇒ Boolean
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.
279 280 281 282 |
# File 'lib/uri_template/utils.rb', line 279 def pair_array?(a) return false unless a.kind_of? Array return a.all?{|p| p.kind_of? Array and p.size == 2 } end |
#pair_array_to_hash(x, careful = false) ⇒ Object
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?.
299 300 301 302 303 304 305 |
# File 'lib/uri_template/utils.rb', line 299 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 |
#pair_array_to_hash2(x) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/uri_template/utils.rb', line 310 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 |
#use_unicode? ⇒ Boolean
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?
267 268 269 |
# File 'lib/uri_template/utils.rb', line 267 def use_unicode? eval('Regexp.compile("\u0020")') =~ " " rescue false end |