| 1 | package URI::Platonic; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use MooseX::Types::URI qw(Uri); | ||
| 5 | use overload '""' => \&as_string, fallback => 1; | ||
| 6 | |||
| 7 | has 'uri' => ( | ||
| 8 | is => 'ro', | ||
| 9 | isa => Uri, | ||
| 10 | coerce => 1, | ||
| 11 | required => 1, | ||
| 12 | ); | ||
| 13 | |||
| 14 | # no Moose handles ? | ||
| 15 | { | ||
| 16 | my @handles = qw( | ||
| 17 | authority opaque userinfo host_port | ||
| 18 | scheme host port path query fragment | ||
| 19 | path_query path_segments | ||
| 20 | query_form query_keywords | ||
| 21 | as_string | ||
| 22 | ); | ||
| 23 | |||
| 24 | for my $method (@handles) { | ||
| 25 | __PACKAGE__->meta->add_method($method, sub { | ||
| 26 | my $self = shift; | ||
| 27 | $self->uri->$method(@_); | ||
| 28 | }); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | has 'extension' => ( | ||
| 33 | is => 'rw', | ||
| 34 | isa => 'Str', | ||
| 35 | ); | ||
| 36 | |||
| 37 | no Moose; | ||
| 38 | |||
| 39 | our $VERSION = '0.03'; | ||
| 40 | |||
| 41 | sub BUILD { | ||
| 42 | my $self = shift; | ||
| 43 | |||
| 44 | my $path = $self->uri->path; | ||
| 45 | if ($path =~ m![^/]+\.([^/\.]+)$!) { | ||
| 46 | $self->extension($1); | ||
| 47 | $path =~ s/\.$1$//; | ||
| 48 | $self->uri->path($path); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | sub clone { | ||
| 53 | my $self = shift; | ||
| 54 | my $class = ref $self || $self; | ||
| 55 | return $class->new(uri => $self->distinct->clone); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub canonical { | ||
| 59 | my $self = shift; | ||
| 60 | my $class = ref $self || $self; | ||
| 61 | return $class->new(uri => $self->distinct->canonical); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub platonic { | ||
| 65 | my $self = shift; | ||
| 66 | return $self->uri->clone; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub distinct { | ||
| 70 | my $self = shift; | ||
| 71 | |||
| 72 | my $uri = $self->uri->clone; | ||
| 73 | if ($self->extension) { | ||
| 74 | $uri->path(join '.', $uri->path, $self->extension); | ||
| 75 | } | ||
| 76 | |||
| 77 | return $uri; | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __PACKAGE__->meta->make_immutable; | ||
| 83 | |||
| 84 | =head1 NAME | ||
| 85 | |||
| 86 | URI::Platonic - Platonic and Distinct URIs | ||
| 87 | |||
| 88 | =head1 SYNOPSIS | ||
| 89 | |||
| 90 | use URI::Platonic; | ||
| 91 | |||
| 92 | my $uri = URI::Platonic->new(uri => "http://example.com/path/to/resource.html"); | ||
| 93 | $uri = URI::Platonic->new(uri => URI->new("http://example.com/foo.xml")); | ||
| 94 | |||
| 95 | print $uri->path; # "/path/to/resource" | ||
| 96 | print $uri->extension; # "html" | ||
| 97 | print $uri->platonic; # "http://example.com/path/to/resource" | ||
| 98 | print $uri->distinct; # "http://example.com/path/to/resource.html" | ||
| 99 | |||
| 100 | $uri->extension('xml'); | ||
| 101 | print $uri->distinct; # "http://example.com/path/to/resource.xml" | ||
| 102 | |||
| 103 | $uri->path('/path/to/another'); | ||
| 104 | print $uri->platonic; # "http://example.com/path/to/another" | ||
| 105 | print $uri->distinct; # "http://example.com/path/to/another.xml" | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | URI::Platonic is a L<URI>-like module for "Platonic" and "Distinct" URIs, | ||
| 110 | described in RESTful Web Services. | ||
| 111 | |||
| 112 | =head1 METHODS | ||
| 113 | |||
| 114 | =head2 new(uri => $uri) | ||
| 115 | |||
| 116 | Constructs a new L<URI::Platonic> object. | ||
| 117 | |||
| 118 | =head2 extension([ $extension ]) | ||
| 119 | |||
| 120 | Gets/Sets a extension part of the distinct URI. | ||
| 121 | |||
| 122 | =head2 platonic() | ||
| 123 | |||
| 124 | Returns a platonic L<URI>. | ||
| 125 | |||
| 126 | =head2 distinct() | ||
| 127 | |||
| 128 | Returns a distinct L<URI>. | ||
| 129 | |||
| 130 | =head2 clone() | ||
| 131 | |||
| 132 | Returns a copy of the L<URI::Platonic> object. | ||
| 133 | |||
| 134 | =head2 canonical() | ||
| 135 | |||
| 136 | Returns a normalized version of the L<URI::Platonic> object. | ||
| 137 | |||
| 138 | =head2 as_string() | ||
| 139 | |||
| 140 | Returns a plain string of the platonic URI. | ||
| 141 | |||
| 142 | =head1 PRIVATES | ||
| 143 | |||
| 144 | =head2 BUILD | ||
| 145 | |||
| 146 | =head1 AUTHOR | ||
| 147 | |||
| 148 | NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> | ||
| 149 | |||
| 150 | =head1 LICENSE | ||
| 151 | |||
| 152 | This library is free software; you can redistribute it and/or modify | ||
| 153 | it under the same terms as Perl itself. | ||
| 154 | |||
| 155 | =head1 SEE ALSO | ||
| 156 | |||
| 157 | L<URI> | ||
| 158 | |||
| 159 | =cut |