Puppet Function: parse_url

Defined in:
lib/puppet/parser/functions/parse_url.rb
Function type:
Ruby 3.x API

Overview

parse_url()Any

Returns information about an url This function expects two arguments, an URL and the part of the url you want to retrieve. It is derived from url_parse function from the puppi module.

Example: $source_filename = parse_url($source_url,path)

Given an url like: my_user:my_pass@www.example.com:8080/path/to/file.php?id=1&ret=0 You obtain the following results according to the second argument: scheme : https userinfo : my_user:my_pass user : my_user password : my_pass host : www.example.com port : 8080 path : /path/to/file.php query : id=1&ret=0 filename : file.php filetype : php filedir : file

Returns:

  • (Any)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/parser/functions/parse_url.rb', line 3

Puppet::Parser::Functions.newfunction(:parse_url, :type => :rvalue, :doc => <<-'ENDHEREDOC') do |args|
    Returns information about an url
    This function expects two arguments, an URL and the part of the url you want to retrieve.
    It is derived from url_parse function from the puppi module.

    Example:
    $source_filename = parse_url($source_url,path)

    Given an url like: https://my_user:my_pass@www.example.com:8080/path/to/file.php?id=1&ret=0
    You obtain the following results according to the second argument:
    scheme   : https
    userinfo : my_user:my_pass
    user     : my_user
    password : my_pass
    host     : www.example.com
    port     : 8080
    path     : /path/to/file.php
    query    : id=1&ret=0
    filename : file.php
    filetype : php
    filedir  : file

  ENDHEREDOC

  raise ArgumentError, ("parse_url(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2
  url = URI.parse args[0]
  case args[1]
  when 'scheme' then url.scheme
  when 'userinfo' then url.userinfo
  when 'user' then url.user
  when 'password' then url.password
  when 'host' then url.host
  when 'port' then url.port
  when 'path' then url.path
  when 'query' then url.query
  when 'filename' then File.basename url.path
  when 'filetype' then File.extname url.path
  when 'filedir' then (File.basename url.path).chomp(File.extname(url.path))
  else url
  end
end