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

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.

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.



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.

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)


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

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.



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?

Returns:

  • (Boolean)


267
268
269
# File 'lib/uri_template/utils.rb', line 267

def use_unicode?
  eval('Regexp.compile("\u0020")') =~ " " rescue false
end