summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-10-02 07:54:40 +0200
committerGuido Günther <agx@sigxcpu.org>2014-10-02 08:23:25 +0200
commit165d5daf93f394c8eff20f77f699f782000596e5 (patch)
tree820d7ea7a0b47a55a34bc73f13b57cab19c57c44 /lib
parent6b9bc57547e7bc84b17f33a4807f22be6bf91ecb (diff)
Add parse_url
This allows us to parse URLs into a hash.
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/functions/parse_url.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/puppet/parser/functions/parse_url.rb b/lib/puppet/parser/functions/parse_url.rb
new file mode 100644
index 0000000..6570d92
--- /dev/null
+++ b/lib/puppet/parser/functions/parse_url.rb
@@ -0,0 +1,31 @@
+require "uri"
+
+module Puppet::Parser::Functions
+
+ newfunction(:parse_url, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
+ Parse an URL into its parts returning a hash.
+
+ parse_url('http://example.com/foo')
+
+ would result in
+
+ {'host' => 'example.com', 'path' => '/foo', 'port': 8080, 'scheme': 'http' }
+ ENDHEREDOC
+
+ parts = {}
+ if args.length != 1
+ raise Puppet::ParseError, ("parse_url(): wrong number of arguments (#{args.length}: must be 1")
+ end
+
+ begin
+ uri = URI::parse(args[0])
+ rescue Exception => e
+ raise Puppet::ParseError, e
+ end
+ parts['host'] = uri.host
+ parts['path'] = uri.path
+ parts['port'] = uri.port
+ parts['scheme'] = uri.scheme
+ parts
+ end
+end