URI::Platonic - Platonic and Distinct URIs


URI-Platonic documentation Contained in the URI-Platonic distribution.

Index


Code Index:

NAME

Top

URI::Platonic - Platonic and Distinct URIs

SYNOPSIS

Top

  use URI::Platonic;

  my $uri = URI::Platonic->new(uri => "http://example.com/path/to/resource.html");
     $uri = URI::Platonic->new(uri => URI->new("http://example.com/foo.xml"));

  print $uri->path;      # "/path/to/resource"
  print $uri->extension; # "html"
  print $uri->platonic;  # "http://example.com/path/to/resource"
  print $uri->distinct;  # "http://example.com/path/to/resource.html"

  $uri->extension('xml');
  print $uri->distinct;  # "http://example.com/path/to/resource.xml"

  $uri->path('/path/to/another');
  print $uri->platonic;  # "http://example.com/path/to/another"
  print $uri->distinct;  # "http://example.com/path/to/another.xml"

DESCRIPTION

Top

URI::Platonic is a URI-like module for "Platonic" and "Distinct" URIs, described in RESTful Web Services.

METHODS

Top

new(uri => $uri)

Constructs a new URI::Platonic object.

extension([ $extension ])

Gets/Sets a extension part of the distinct URI.

platonic()

Returns a platonic URI.

distinct()

Returns a distinct URI.

clone()

Returns a copy of the URI::Platonic object.

canonical()

Returns a normalized version of the URI::Platonic object.

as_string()

Returns a plain string of the platonic URI.

PRIVATES

Top

BUILD

AUTHOR

Top

NAKAGAWA Masaki <masaki@cpan.org>

LICENSE

Top

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

Top

URI


URI-Platonic documentation Contained in the URI-Platonic distribution.

package URI::Platonic;

use Moose;
use MooseX::Types::URI qw(Uri);
use overload '""' => \&as_string, fallback => 1;

has 'uri' => (
    is       => 'ro',
    isa      => Uri,
    coerce   => 1,
    required => 1,
);

# no Moose handles ?
{
    my @handles = qw(
        authority opaque userinfo host_port
        scheme host port path query fragment
        path_query path_segments
        query_form query_keywords
        as_string
    );

    for my $method (@handles) {
        __PACKAGE__->meta->add_method($method, sub {
            my $self = shift;
            $self->uri->$method(@_);
        });
    }
}

has 'extension' => (
    is  => 'rw',
    isa => 'Str',
);

no Moose;

our $VERSION = '0.03';

sub BUILD {
    my $self = shift;

    my $path = $self->uri->path;
    if ($path =~ m![^/]+\.([^/\.]+)$!) {
        $self->extension($1);
        $path =~ s/\.$1$//;
        $self->uri->path($path);
    }
}

sub clone {
    my $self = shift;
    my $class = ref $self || $self;
    return $class->new(uri => $self->distinct->clone);
}

sub canonical {
    my $self = shift;
    my $class = ref $self || $self;
    return $class->new(uri => $self->distinct->canonical);
}

sub platonic {
    my $self = shift;
    return $self->uri->clone;
}

sub distinct {
    my $self = shift;

    my $uri = $self->uri->clone;
    if ($self->extension) {
        $uri->path(join '.', $uri->path, $self->extension);
    }

    return $uri;
}

1;

__PACKAGE__->meta->make_immutable;