diff options
author | Brian Phillips <bphillips@cpan.org> | 2012-12-04 22:27:23 -0600 |
---|---|---|
committer | Ash Berlin <ash_github@firemirror.com> | 2013-04-19 14:21:43 +0100 |
commit | 80232d581e7a82f0ec8bd85d269a1604fdbe58c7 (patch) | |
tree | 25e542e234b6e84980377f32e6bcb60fd9f6e29c /lib/Net/HTTP/Spore/Meta | |
parent | support for anonymous middlewares (diff) | |
download | net-http-spore-80232d581e7a82f0ec8bd85d269a1604fdbe58c7.tar.gz |
clean up types
- Split out type declarations into separate class - Use namespaced types instead of global types (via MooseX::Types) - use declared types where possible, instead of quoted type names (i.e. isa => Str, instead of isa => 'Str') - Allow "authentication" attribute to coerce from a JSON::is_bool() value (instead of expecting it to be a 1 or a 0 all the time)
Diffstat (limited to 'lib/Net/HTTP/Spore/Meta')
-rw-r--r-- | lib/Net/HTTP/Spore/Meta/Method.pm | 60 | ||||
-rw-r--r-- | lib/Net/HTTP/Spore/Meta/Types.pm | 31 |
2 files changed, 44 insertions, 47 deletions
diff --git a/lib/Net/HTTP/Spore/Meta/Method.pm b/lib/Net/HTTP/Spore/Meta/Method.pm index 1a04eca..50e9f92 100644 --- a/lib/Net/HTTP/Spore/Meta/Method.pm +++ b/lib/Net/HTTP/Spore/Meta/Method.pm @@ -4,55 +4,21 @@ package Net::HTTP::Spore::Meta::Method; use JSON; use Moose; -use Moose::Util::TypeConstraints; use MooseX::Types::Moose qw/Str Int ArrayRef HashRef/; use MooseX::Types::URI qw/Uri/; extends 'Moose::Meta::Method'; use Net::HTTP::Spore::Response; +use Net::HTTP::Spore::Meta::Types qw(UriPath HTTPMethod Boolean); -subtype UriPath - => as 'Str' - => where { $_ =~ m!^/! } - => message {"path must start with /"}; - -enum Method => qw(OPTIONS HEAD GET POST PUT DELETE TRACE PATCH); - -subtype 'JSON::XS::Boolean' => as 'JSON::XS::Boolean'; -subtype 'JSON::PP::Boolean' => as 'JSON::PP::Boolean'; -subtype 'Boolean' => as Int => where { $_ eq 1 || $_ eq 0 }; - -coerce 'Boolean' - => from 'JSON::XS::Boolean' - => via { - if ( JSON::is_bool($_) && $_ == JSON::true ) { - return 1 - } - return 0; - } - => from 'JSON::PP::Boolean' - => via { - if ( JSON::is_bool($_) && $_ == JSON::true ) { - return 1; - } - return 0; - } - => from Str - => via { - if ($_ eq 'true') { - return 1; - } - return 0; - }; - -has path => ( is => 'ro', isa => 'UriPath', required => 1 ); -has method => ( is => 'ro', isa => 'Method', required => 1 ); -has description => ( is => 'ro', isa => 'Str', predicate => 'has_description' ); +has path => ( is => 'ro', isa => UriPath, required => 1 ); +has method => ( is => 'ro', isa => HTTPMethod, required => 1 ); +has description => ( is => 'ro', isa => Str, predicate => 'has_description' ); has required_payload => ( is => 'ro', - isa => 'Boolean', + isa => Boolean, predicate => 'payload_is_required', lazy => 1, default => 0, @@ -60,7 +26,7 @@ has required_payload => ( ); has authentication => ( is => 'ro', - isa => 'Boolean', + isa => Boolean, predicate => 'has_authentication', default => 0, coerce => 1, @@ -73,18 +39,18 @@ has base_url => ( ); has formats => ( is => 'ro', - isa => ArrayRef [Str], + isa => ArrayRef[Str], predicate => 'has_formats', ); has headers => ( is => 'ro', - isa => HashRef [Str], + isa => HashRef[Str], predicate => 'has_headers', ); has expected_status => ( traits => ['Array'], is => 'ro', - isa => ArrayRef [Int], + isa => ArrayRef[Int], auto_deref => 1, predicate => 'has_expected_status', handles => { find_expected_status => 'grep', }, @@ -92,27 +58,27 @@ has expected_status => ( has optional_params => ( traits => ['Array'], is => 'ro', - isa => ArrayRef [Str], + isa => ArrayRef[Str], predicate => 'has_optional_params', auto_deref => 1, ); has required_params => ( traits => ['Array'], is => 'ro', - isa => ArrayRef [Str], + isa => ArrayRef[Str], predicate => 'has_required_params', auto_deref => 1, ); has form_data => ( traits => ['Hash'], is => 'ro', - isa => 'HashRef', + isa => HashRef, predicate => 'has_form_data', auto_deref => 1, ); has documentation => ( is => 'ro', - isa => 'Str', + isa => Str, lazy => 1, default => sub { my $self = shift; diff --git a/lib/Net/HTTP/Spore/Meta/Types.pm b/lib/Net/HTTP/Spore/Meta/Types.pm new file mode 100644 index 0000000..3089da3 --- /dev/null +++ b/lib/Net/HTTP/Spore/Meta/Types.pm @@ -0,0 +1,31 @@ +package Net::HTTP::Spore::Meta::Types; + +# ABSTRACT: Moose type definitions for Net::HTTP::Spore + +use Moose::Util::TypeConstraints; +use MooseX::Types -declare => [ qw(UriPath Boolean HTTPMethod JSONBoolean) ]; +use MooseX::Types::Moose qw(Str Int Defined); +use JSON; + +subtype UriPath, + as Str, + where { $_ =~ m!^/! }, + message {"path must start with /"}; + +enum HTTPMethod, qw(OPTIONS HEAD GET POST PUT DELETE TRACE PATCH); + +subtype Boolean, + as Int, + where { $_ eq 1 || $_ eq 0 }; + +subtype JSONBoolean, + as Defined, + where { JSON::is_bool($_) }; + +coerce Boolean, + from JSONBoolean, + via { return $_ == JSON::true() ? 1 : 0 }, + from Str, + via { return $_ eq 'true' ? 1 : 0 }; + +1; |