1
2
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#! /usr/bin/env false
use v6.d;
use URL::Grammar;
use URL::Grammar::Actions;
unit class URL;
has Str $.scheme;
has Str $.username;
has Str $.password;
has Str $.hostname;
has Int $.port;
has @.path;
has %.query;
has Str $.fragment;
multi method new (
Str:D $url,
) {
my %match = URL::Grammar.parse($url, actions => URL::Grammar::Actions).made;
die "'$url' failed to parse. Please report the URL you tried to p.spek+perl6@tyil.work." unless %match;
samewith(
|%match,
path => %match<path>.list
);
}
multi method new (
Str :$scheme,
Str :$username,
Str :$password,
Str :$hostname,
Int :$port,
:@path = [],
:%query = {},
Str :$fragment,
) {
self.bless(
:$scheme,
:$username,
:$password,
:$hostname,
:$port,
:@path,
:%query,
:$fragment,
);
}
method Hash (
--> Hash
) {
{
:$!scheme,
:$!username,
:$!password,
:$!hostname,
:$!port,
:@!path,
:%!query,
:$!fragment,
}
}
multi method Str (
--> Str
) {
my $s = $!scheme ~ "://";
$s ~= "{self.Str(:userinfo)}@" if $!username;
$s ~= $!hostname;
$s ~= ":$!port" if $!port;
$s ~= "/{self.Str(:path)}" if @!path;
$s ~= "?{self.Str(:query)}" if %!query;
$s ~= "#$!fragment" if $!fragment;
$s;
}
multi method Str (
:$path! where { $_ },
--> Str
) {
@!path.join("/");
}
multi method Str (
:$query! where { $_ },
--> Str
) {
%!query.keys.sort.map({ "$_={%!query{$_}}" }).join("&")
}
multi method Str (
:$userinfo! where { $_ },
--> Str
) {
return "$!username:$!password" if $!password;
$!username // "";
}
=begin pod
=NAME URL
=AUTHOR Patrick Spek <p.spek@tyil.work>
=VERSION 0.1.0
=head1 Synopsis
=head1 Description
=head1 Examples
=head1 See also
=end pod
# vim: ft=perl6 noet
|