From d651409dab0a448725e4fb338bf341438b54626f Mon Sep 17 00:00:00 2001 From: franck cuny Date: Tue, 31 Aug 2010 15:50:10 +0200 Subject: can load api from a spec file --- lib/Net/HTTP/Console.pm | 1 + lib/Net/HTTP/Console/Dispatcher/Load.pm | 11 ++++++-- lib/Net/HTTP/Console/Role/API.pm | 45 +++++++++++++++++++++++---------- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/lib/Net/HTTP/Console.pm b/lib/Net/HTTP/Console.pm index a1bc1dd..4f0b342 100644 --- a/lib/Net/HTTP/Console.pm +++ b/lib/Net/HTTP/Console.pm @@ -16,6 +16,7 @@ class Net::HTTP::Console { has url => (isa => 'Str', is => 'rw', predicate => 'has_url'); has format => (isa => 'Str', is => 'rw', predicate => 'has_format'); has format_mode => (isa => 'Str', is => 'rw', predicate => 'has_format_mode'); + has spec => (isa => 'Str', is => 'rw', predicate => 'has_specification'); } 1; diff --git a/lib/Net/HTTP/Console/Dispatcher/Load.pm b/lib/Net/HTTP/Console/Dispatcher/Load.pm index 3986b79..3d73eb5 100644 --- a/lib/Net/HTTP/Console/Dispatcher/Load.pm +++ b/lib/Net/HTTP/Console/Dispatcher/Load.pm @@ -5,13 +5,20 @@ use MooseX::Declare; class Net::HTTP::Console::Dispatcher::Load with Net::HTTP::Console::Dispatcher { method dispatch($input) { - $self->application->load_api_lib($input); + (my $type, my $name) = $input =~ /(\w+)\s(.*)$/; + + if ($type eq 'spec') { + $self->application->load_api_spec($name); + }elsif($type eq 'lib') { + $self->application->load_api_lib($name); + }else{ + $self->logger('error', "can't load for $type"); + } } method pattern($input) { $input =~ /load\s(.*)$/ ? $1 : 0; } - } 1; diff --git a/lib/Net/HTTP/Console/Role/API.pm b/lib/Net/HTTP/Console/Role/API.pm index cd3ba75..bc1dbe3 100644 --- a/lib/Net/HTTP/Console/Role/API.pm +++ b/lib/Net/HTTP/Console/Role/API.pm @@ -5,11 +5,12 @@ use MooseX::Declare; role Net::HTTP::Console::Role::API { use Try::Tiny; + use Net::HTTP::API::Spec; has api_lib => ( isa => 'Str', is => 'rw', - default => 'Net::HTTP::Console::Dummy' + default => 'Net::HTTP::API::Core' ); has api_object => ( @@ -23,19 +24,37 @@ role Net::HTTP::Console::Role::API { ); method _load_api_lib($lib) { + my $api; - try { - Class::MOP::load_class($lib); - $self->api_lib($lib); - $api = $lib->new(); - $api->api_base_url($self->url) if $self->has_url; - $api->api_format($self->format) if $self->has_format; - $api->api_format_mode($self->format_mode) - if $self->has_format_mode; - }catch { - $self->logger('error', "failed to load $lib: $_"); - }; - return $api if $api; + + if ($self->has_specification) { + $api = Net::HTTP::API::Spec->load_from_spec($self->spec); + }else{ + try { + Class::MOP::load_class($lib); + $self->api_lib($lib); + $api = $lib->new(); + } catch { + $self->logger('error', "failed to load $lib: $_"); + }; + } + + if (!$api) { + $self->logger('error', "unable to load an API!"); + } + + $api->api_base_url($self->url) if $self->has_url; + $api->api_format($self->format) if $self->has_format; + $api->api_format_mode($self->format_mode) + if $self->has_format_mode; + + return $api; + } + + method load_api_spec($path) { + my $object = Net::HTTP::API::Spec->new_from_spec($path); + $self->api_object($object); + $self->message("successfully loaded $path"); } method load_api_lib($lib) { -- cgit 1.4.1