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)

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.

Examples:

URITemplate::Utils.object_to_param(5) #=> "5"
o = Object.new
def o.to_param
  "42"
end
URITemplate::Utils.object_to_param(o) #=> "42"

Raises:

  • Unconvertable if the object could not be converted.



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.

Examples:

URITemplate::Utils.pair_array?( Object.new ) #=> false
URITemplate::Utils.pair_array?( [] ) #=> true
URITemplate::Utils.pair_array?( [1,2,3] ) #=> false
URITemplate::Utils.pair_array?( [ ['a',1],['b',2],['c',3] ] ) #=> true
URITemplate::Utils.pair_array?( [ ['a',1],['b',2],['c',3],[] ] ) #=> false

Returns:

  • (Boolean)


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?.

Examples:

URITemplate::Utils.pair_array_to_hash( 'x' ) #=> 'x'
URITemplate::Utils.pair_array_to_hash( [ ['a',1],['b',2],['c',3] ] ) #=> {'a'=>1,'b'=>2,'c'=>3}
URITemplate::Utils.pair_array_to_hash( [ ['a',1],['a',2],['a',3] ] ) #=> {'a'=>3}

Carful vs. Ignorant

URITemplate::Utils.pair_array_to_hash( [ ['a',1],'foo','bar'], false ) #UNDEFINED!
URITemplate::Utils.pair_array_to_hash( [ ['a',1],'foo','bar'], true )  #=> [ ['a',1], 'foo', 'bar']

Parameters:

  • x

    the value to convert

  • careful (true, false) (defaults to: false)

    wheter to check every array item. Use this when you expect array with subarrays which are not pairs. Setting this to false however improves runtime by ~30% even with comparetivly short arrays.



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?

Returns:

  • (Boolean)


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