| 1 | package Net::HTTP::Spore; | ||||||||||||||||||||
| 2 | 24 | $Net::HTTP::Spore::VERSION = '0.09'; | |||||||||||||||||||
| 3 | # ABSTRACT: SPORE client | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 24 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 24 | ▶ | use IO::All; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 24 | ▶ | use JSON; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 24 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 24 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 24 | ▶ | use Scalar::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 24 | ▶ | use Net::HTTP::Spore::Core; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | # XXX should we let the possibility to override this super class, or add | ||||||||||||||||||||
| 16 | # another superclasses? | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 31 | ▶ | sub new_from_string { | ||||||||||||||||||
|
Subroutine
new_from_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 19 | 31 | my ($class, $string, %args) = @_; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 31 | my $spore_class = | |||||||||||||||||||
| 22 | Class::MOP::Class->create_anon_class( | ||||||||||||||||||||
| 23 | superclasses => ['Net::HTTP::Spore::Core'] ); | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 31 | my $spore_object = $class->_attach_spec_to_class($string, \%args, $spore_class); | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 28 | return $spore_object; | |||||||||||||||||||
| 28 | } | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 5 | ▶ | sub new_from_strings { | ||||||||||||||||||
|
Subroutine
new_from_strings: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 31 | 5 | my $class = shift; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | my $opts; | |||||||||||||||||||
| 34 | 5 | ▶ | if (ref ($_[-1]) eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 35 | 3 | $opts = pop @_; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 5 | my @strings = @_; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 5 | my $spore_class = | |||||||||||||||||||
| 40 | Class::MOP::Class->create_anon_class( | ||||||||||||||||||||
| 41 | superclasses => ['Net::HTTP::Spore::Core'] ); | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 5 | my $spore_object = undef; | |||||||||||||||||||
| 44 | 5 | foreach my $string (@strings) { | |||||||||||||||||||
| 45 | 8 | $spore_object = $class->_attach_spec_to_class($string, $opts, $spore_class, $spore_object); | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | 3 | return $spore_object; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 23 | ▶ | sub new_from_spec { | ||||||||||||||||||
|
Subroutine
new_from_spec: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 51 | 23 | my ( $class, $spec_file, %args ) = @_; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 23 | ▶ | Carp::confess("specification file is missing") unless $spec_file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 22 | my $content = _read_spec($spec_file); | |||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | 21 | $class->new_from_string( $content, %args ); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 2 | ▶ | sub new_from_specs { | ||||||||||||||||||
|
Subroutine
new_from_specs: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 61 | 2 | my $class = shift; | |||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2 | my $opts; | |||||||||||||||||||
| 64 | 2 | ▶ | if (ref ($_[-1]) eq 'HASH') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | 1 | $opts = pop @_; | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 2 | my @specs = @_; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | my @strings; | |||||||||||||||||||
| 70 | 2 | foreach my $spec (@specs) { | |||||||||||||||||||
| 71 | 4 | push @strings,_read_spec($spec); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 2 | $class->new_from_strings(@strings, $opts); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 39 | ▶ | sub _attach_spec_to_class { | ||||||||||||||||||
|
Subroutine
_attach_spec_to_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 78 | 39 | my ( $class, $string, $opts, $spore_class, $object ) = @_; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 39 | my $spec; | |||||||||||||||||||
| 81 | 39 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 82 | 39 | $spec = JSON::decode_json($string); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 1 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 85 | 1 | Carp::confess( "unable to parse JSON spec: " . $_ ); | |||||||||||||||||||
| 86 | 39 | }; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 38 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||
| 89 | 38 | ▶ | $opts->{base_url} ||= $spec->{base_url}; | ||||||||||||||||||
|
Condition100%3/3
$opts->{"base_url"} ||= $spec->{"base_url"}
MC/DC100%2/2
$opts->{"base_url"} ||= $spec->{"base_url"}
$opts->{"base_url"}
$spec->{"base_url"}
Truth table100%3/3
A ||= B
A= $opts->{"base_url"}B= $spec->{"base_url"}
| |||||||||||||||||||||
| 90 | 38 | ▶ | die "base_url is missing!" if !$opts->{base_url}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 36 | ▶ | if ( $spec->{formats} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $opts->{formats} = $spec->{formats}; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 36 | ▶ | if ( $spec->{authentication} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 1 | $opts->{authentication} = $spec->{authentication}; | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 36 | ▶ | if ( !$object ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 33 | $object = $spore_class->new_object(%$opts); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | 36 | $object = $class->_add_methods( $object, $spec->{methods} ); | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | 4 | ▶ | catch { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 106 | 4 | Carp::confess( "unable to create new Net::HTTP::Spore object: " . $_ ); | |||||||||||||||||||
| 107 | 38 | }; | |||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 34 | return $object; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 26 | ▶ | sub _read_spec { | ||||||||||||||||||
|
Subroutine
_read_spec: called CC=4 SCAR=18.4
| |||||||||||||||||||||
| 113 | 26 | my $spec_file = shift; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 26 | my $content; | |||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 26 | ▶ | if ( $spec_file =~ m!^http(s)?://! ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 118 | 0 | my $uri = URI->new($spec_file); | |||||||||||||||||||
| 119 | 0 | my $req = HTTP::Request->new( GET => $spec_file ); | |||||||||||||||||||
| 120 | 0 | my $ua = LWP::UserAgent->new(); | |||||||||||||||||||
| 121 | 0 | my $res = $ua->request($req); | |||||||||||||||||||
| 122 | 0 | ▶ | unless( $res->is_success ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 123 | 0 | my $status = $res->status_line; | |||||||||||||||||||
| 124 | 0 | Carp::confess("Unabled to fetch $spec_file ($status)"); | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | 0 | $content = $res->content; | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | else { | ||||||||||||||||||||
| 129 | 26 | ▶ | unless ( -f $spec_file ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 1 | Carp::confess("$spec_file does not exists"); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | 25 | $content < io($spec_file); | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 25 | return $content; | |||||||||||||||||||
| 136 | } | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 36 | ▶ | sub _add_methods { | ||||||||||||||||||
|
Subroutine
_add_methods: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 139 | 36 | my ($class, $spore, $methods_spec) = @_; | |||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 36 | foreach my $method_name (keys %$methods_spec) { | |||||||||||||||||||
| 142 | $spore->meta->add_spore_method($method_name, | ||||||||||||||||||||
| 143 | 127 | %{$methods_spec->{$method_name}}); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 34 | $spore; | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 24 | 1; | |||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||