summaryrefslogtreecommitdiff
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
parent6b9bc57547e7bc84b17f33a4807f22be6bf91ecb (diff)
Add parse_url
This allows us to parse URLs into a hash.
-rw-r--r--README.markdown10
-rw-r--r--lib/puppet/parser/functions/parse_url.rb31
-rw-r--r--spec/unit/puppet/parser/functions/parse_url_spec.rb33
3 files changed, 74 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index 96b4b6e..a89a7fe 100644
--- a/README.markdown
+++ b/README.markdown
@@ -7,6 +7,16 @@ called "stdlib minus minus".
# Functions #
+parse_url
+---------
+Parse an URL into its parts returning a hash.
+
+ parse_url('http://example.com:8080/foo')
+
+would result in
+
+ {'host' => 'example.com', 'path' => '/foo', 'port': 8080, 'scheme': 'http' }
+
validate_nonemptystring
-----------------------
Validate that all passed values are strings with a length greater 0
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
diff --git a/spec/unit/puppet/parser/functions/parse_url_spec.rb b/spec/unit/puppet/parser/functions/parse_url_spec.rb
new file mode 100644
index 0000000..dd665d3
--- /dev/null
+++ b/spec/unit/puppet/parser/functions/parse_url_spec.rb
@@ -0,0 +1,33 @@
+#! /usr/bin/env ruby -S rspec
+#
+require 'spec_helper'
+
+describe 'the parse_url function' do
+ let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
+
+ it "should exist" do
+ Puppet::Parser::Functions.function("parse_url").should == "function_parse_url"
+ end
+
+ it 'should parse protocoll, host and path' do
+ result = scope.function_parse_url(['http://example.com/foo'])
+ result.should eq({"port" => 80,
+ "path" => "/foo",
+ "scheme" => "http",
+ "host" => "example.com"})
+ end
+
+
+ it "should raise a ParseError if there is less than 1 arguments" do
+ lambda { scope.function_parse_url([]) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError if there is more than 1 arguments" do
+ lambda { scope.function_parse_url(['one', 'two']) }.should( raise_error(Puppet::ParseError))
+ end
+
+ it "should raise a ParseError if the URL format is invalid" do
+ lambda { scope.function_parse_url(['not_a_url://foo://'])}.should( raise_error(Puppet::ParseError))
+ end
+end
+