Skip to content

Commit

Permalink
added scheme_data attribute to Mojo::URL
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Apr 23, 2013
1 parent cbdfddd commit 8b87105
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,5 +1,6 @@

3.97 2013-04-23
- Added scheme_data attribute to Mojo::URL.
- Improved tests.

3.96 2013-04-22
Expand Down
20 changes: 15 additions & 5 deletions lib/Mojo/URL.pm
Expand Up @@ -10,7 +10,7 @@ use Mojo::Path;
use Mojo::Util qw(punycode_decode punycode_encode url_escape url_unescape);

has base => sub { Mojo::URL->new };
has [qw(fragment host port scheme userinfo)];
has [qw(fragment host port scheme scheme_data userinfo)];

sub new { shift->SUPER::new->parse(@_) }

Expand Down Expand Up @@ -45,8 +45,7 @@ sub clone {
my $self = shift;

my $clone = $self->new;
$clone->{data} = $self->{data};
$clone->$_($self->$_) for qw(scheme userinfo host port fragment);
$clone->$_($self->$_) for qw(scheme scheme_data userinfo host port fragment);
$clone->path($self->path->clone);
$clone->query($self->query->clone);
$clone->base($self->base->clone) if $self->{base};
Expand Down Expand Up @@ -90,7 +89,7 @@ sub parse {
}

# Preserve scheme data
else { $self->{data} = $url }
else { $self->scheme_data(substr($url, length($proto) + 1)) }

return $self;
}
Expand Down Expand Up @@ -204,7 +203,8 @@ sub to_string {
my $self = shift;

# Scheme data
return $self->{data} if defined $self->{data};
my $data = $self->scheme_data;
return join ':', $self->scheme, $data if defined $data;

# Protocol
my $url = '';
Expand Down Expand Up @@ -313,6 +313,16 @@ Port part of this URL.
Scheme part of this URL.
=head2 scheme_data
my $data = $url->scheme_data;
$url = $url->scheme_data('foo')
Data for unknown schemes.
# "sri@example.com"
Mojo::URL->new('mailto:sri@example.com')->scheme_data;
=head2 userinfo
my $userinfo = $url->userinfo;
Expand Down
14 changes: 10 additions & 4 deletions t/mojo/url.t
Expand Up @@ -118,13 +118,19 @@ is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is "$url", 'DATA:helloworld123', 'right format';
$url = Mojo::URL->new->parse('mailto:sri@example.com');
is $url->scheme, 'mailto', 'right scheme';
is $url->protocol, 'mailto', 'right protocol';
is $url->scheme, 'mailto', 'right scheme';
is $url->protocol, 'mailto', 'right protocol';
is $url->scheme_data, 'sri@example.com', 'right scheme data';
is "$url", 'mailto:sri@example.com', 'right format';
$url = Mojo::URL->new->parse('foo://test/123');
is $url->scheme, 'foo', 'right scheme';
is $url->protocol, 'foo', 'right protocol';
is $url->scheme, 'foo', 'right scheme';
is $url->protocol, 'foo', 'right protocol';
is $url->scheme_data, '//test/123', 'right scheme data';
is "$url", 'foo://test/123', 'right format';
is $url->scheme('Bar')->to_string, 'Bar://test/123', 'right format';
is $url->scheme, 'Bar', 'right scheme';
is $url->protocol, 'bar', 'right protocol';
is $url->scheme_data, '//test/123', 'right scheme data';

# Relative
$url = Mojo::URL->new('foo?foo=bar#23');
Expand Down

0 comments on commit 8b87105

Please sign in to comment.