Articulate/Service.pm

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