comparison modules/stdlib/lib/puppet/parser/functions/uriescape.rb @ 0:956e484adc12

Initial public release of Puppet configs
author IBBoard <dev@ibboard.co.uk>
date Sat, 16 Aug 2014 19:47:38 +0000
parents
children addb0ea390a1
comparison
equal deleted inserted replaced
-1:000000000000 0:956e484adc12
1 #
2 # uriescape.rb
3 #
4 require 'uri'
5
6 module Puppet::Parser::Functions
7 newfunction(:uriescape, :type => :rvalue, :doc => <<-EOS
8 Urlencodes a string or array of strings.
9 Requires either a single string or an array as an input.
10 EOS
11 ) do |arguments|
12
13 raise(Puppet::ParseError, "uriescape(): Wrong number of arguments " +
14 "given (#{arguments.size} for 1)") if arguments.size < 1
15
16 value = arguments[0]
17 klass = value.class
18 unsafe = ":/?#[]@!$&'()*+,;= "
19
20 unless [Array, String].include?(klass)
21 raise(Puppet::ParseError, 'uriescape(): Requires either ' +
22 'array or string to work with')
23 end
24
25 if value.is_a?(Array)
26 # Numbers in Puppet are often string-encoded which is troublesome ...
27 result = value.collect { |i| i.is_a?(String) ? URI.escape(i,unsafe) : i }
28 else
29 result = URI.escape(value,unsafe)
30 end
31
32 return result
33 end
34 end
35
36 # vim: set ts=2 sw=2 et :