module URITemplate::Utils

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.

Constants

KCODE_UTF8

Public Instance Methods

compact_regexp(rx) click to toggle source

@api private

# File lib/uri_template/utils.rb, line 327
def compact_regexp(rx)
  rx.split("\n").map(&:strip).join
end
object_to_param(object) click to toggle source

Converts an object to a param value. Tries to call :to_param and then :to_s on that object. @raise Unconvertable if the object could not be converted. @example

URITemplate::Utils.object_to_param(5) #=> "5"
o = Object.new
def o.to_param
  "42"
end
URITemplate::Utils.object_to_param(o) #=> "42"
# File lib/uri_template/utils.rb, line 252
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) click to toggle source

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. @example

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
# File lib/uri_template/utils.rb, line 278
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 ) click to toggle source

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

@example

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}

@example 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']

@param x the value to convert @param careful [true,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.

# File lib/uri_template/utils.rb, line 298
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) click to toggle source

@api privat

# File lib/uri_template/utils.rb, line 309
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?() click to toggle source

@api private Should we use u.… or x.. in regexps?

# File lib/uri_template/utils.rb, line 266
def use_unicode?
  eval('Regexp.compile("\u0020")') =~ " " rescue false
end