| 1 | package Articulate::Service; | ||||||||||||||||||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use Articulate::Syntax; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # The following provide objects which must be created on a per-request basis | ||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Articulate::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use Articulate::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 11 | with 'Articulate::Role::Service'; | |||||||||||||||||||||||||||
| 14 | 11 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use Exporter::Declare; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | default_exports qw(articulate_service); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Articulate::Service - provide an API to all the core Articulate | ||||||||||||||||||||||||||||
| 23 | features. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | The Articulate Service provides programmatic access to all the core | ||||||||||||||||||||||||||||
| 30 | features of the Articulate app. It is the intermediary between the | ||||||||||||||||||||||||||||
| 31 | B<routes> and all other B<components>. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Mostly, you will want to be calling the service in routes, for | ||||||||||||||||||||||||||||
| 34 | instance: | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | get 'zone/:zone_name/article/:article_name' => sub { | ||||||||||||||||||||||||||||
| 37 | my ($zone_name, $article_name) = param('zone_name'), param('article_name'); | ||||||||||||||||||||||||||||
| 38 | return $self->process_request ( read => "/zone/$zone_name/article/$article_name' ) | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | However, you may also want to call it from one-off scripts, tests, | ||||||||||||||||||||||||||||
| 42 | etc., especially where you want to perform tasks which you don't want | ||||||||||||||||||||||||||||
| 43 | to make available in routes, or where you are already in a perl | ||||||||||||||||||||||||||||
| 44 | environment and mapping to the HTTP layer would be a distraction. In | ||||||||||||||||||||||||||||
| 45 | theory you could create an application which did not have any web | ||||||||||||||||||||||||||||
| 46 | interface at all using this service, e.g. a command-line app on a | ||||||||||||||||||||||||||||
| 47 | shared server. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | sub articulate_service { | ||||||||||||||||||||||||||
|
Subroutine
articulate_service: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 0 | __PACKAGE__->new(@_); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | has providers => ( | ||||||||||||||||||||||||||||
| 56 | is => 'rw', | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 58 | 5 | ▶ | coerce => sub { instantiate_array(@_) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | 11 | ); | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head3 process_request | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | my $response = service->process_request($request); | ||||||||||||||||||||||||||||
| 64 | my $response = service->process_request($verb => $data); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | This is the primary method of the service: Pass in an | ||||||||||||||||||||||||||||
| 67 | Articulate::Request object and the Service will produce a Response | ||||||||||||||||||||||||||||
| 68 | object to match. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | Alternatively, if you pass a string as the first argument, the request | ||||||||||||||||||||||||||||
| 71 | will be created from the verb and the data. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Which verbs are handled, what data they require, and how they will be | ||||||||||||||||||||||||||||
| 74 | processed are determined by the service providers you have set up in | ||||||||||||||||||||||||||||
| 75 | your config: C<process_request> passes the request to each of the | ||||||||||||||||||||||||||||
| 76 | providers in turn and asks them to process the request. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Providers can decline to process the request by returning undef, which | ||||||||||||||||||||||||||||
| 79 | will cause the service to offer the requwst to the next provider. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Note that a provider MAY act on a request and still return undef, e.g. | ||||||||||||||||||||||||||||
| 82 | to perform logging, however it is discouraged to perform acctions which | ||||||||||||||||||||||||||||
| 83 | a user would typically expect a response from (e.g. a create action | ||||||||||||||||||||||||||||
| 84 | should return a response and not just pass to a get to confirm it has | ||||||||||||||||||||||||||||
| 85 | successfully created what it was suppsed to). | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =cut | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | ▶ | sub process_request { | ||||||||||||||||||||||||||
|
Subroutine
process_request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 91 | 0 | my @underscore = @_; # because otherwise the try block will eat it | |||||||||||||||||||||||||||
| 92 | 0 | my $request; | |||||||||||||||||||||||||||
| 93 | 0 | my $response = new_response error => { | |||||||||||||||||||||||||||
| 94 | error => Articulate::Error::NotFound->new( | ||||||||||||||||||||||||||||
| 95 | { simple_message => 'No appropriate Service Provider found' } | ||||||||||||||||||||||||||||
| 96 | ) | ||||||||||||||||||||||||||||
| 97 | }; | ||||||||||||||||||||||||||||
| 98 | 0 | ▶ | try { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( ref $underscore[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | $request = $underscore[0]; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | else { # or accept $verb => $data | ||||||||||||||||||||||||||||
| 103 | 0 | $request = new_request(@underscore); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 0 | foreach my $provider ( @{ $self->providers } ) { | |||||||||||||||||||||||||||
| 106 | 0 | $provider->app( $self->app ); | |||||||||||||||||||||||||||
| 107 | 0 | my $resp = $provider->process_request($request); | |||||||||||||||||||||||||||
| 108 | 0 | ▶ | if ( defined $resp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | $response = $resp; | |||||||||||||||||||||||||||
| 110 | 0 | last; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | catch { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 115 | 0 | local $@ = $_; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | if ( blessed $_ and $_->isa('Articulate::Error') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($_) and $_->isa("Articulate::Error")
MC/DC0%0/2
builtin::blessed($_) and $_->isa("Articulate::Error")
builtin::blessed($_)
$_->isa("Articulate::Error")
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa("Articulate::Error")
| |||||||||||||||||||||||||||||
| 117 | 0 | $response = new_response error => { error => $_ }; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 0 | $response = | |||||||||||||||||||||||||||
| 121 | new_response error => { error => | ||||||||||||||||||||||||||||
| 122 | Articulate::Error->new( { simple_message => 'Unknown error' . $@ } ) | ||||||||||||||||||||||||||||
| 123 | }; | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | 0 | }; | |||||||||||||||||||||||||||
| 126 | 0 | return $response; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head3 enumerate_verbs | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | my @verbs = @{ $self->enumerate_verbs }; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | Returns an arrayref of verbs which at list one provider will process. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =cut | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | sub enumerate_verbs { | ||||||||||||||||||||||||||
|
Subroutine
enumerate_verbs: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 138 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 139 | 1 | my $verbs = {}; | |||||||||||||||||||||||||||
| 140 | 1 | foreach my $provider ( @{ $self->providers } ) { | |||||||||||||||||||||||||||
| 141 | 5 | $verbs->{$_}++ foreach keys %{ $provider->verbs }; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | 1 | return [ sort keys %$verbs ]; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =over | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item * L<Articulate::Role::Service> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =item * L<Articulate::Request> | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =item * L<Articulate::Response> | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =cut | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 11 | 1; | |||||||||||||||||||||||||||