Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

can load api from a spec file

  • Loading branch information...
commit d651409dab0a448725e4fb338bf341438b54626f 1 parent 856a81b
@franckcuny authored
View
1  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;
View
11 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;
View
45 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) {
Please sign in to comment.
Something went wrong with that request. Please try again.