Role/Signature.pm

filter: Statementstmt 100% 34 / 34 Branchbran 75.0% 9 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 94.7% 54 / 57 CC 8 SCAR 8.7
CC
8
Cov
93%
load_signature
4.0
_load
3.0
__ANON__
2.0
CRAP 2.4
« Signature/Base.pm ↑ Summary
1 package Mojo::UserAgent::Role::Signature;
2 2 ▶use Mojo::Base -role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::Loader qw(load_class);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::Util qw(camelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::UserAgent::Signature::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.01';
9  
10 2 ▶has signature_namespaces => sub { ['Mojo::UserAgent::Signature'] };
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 has 'signature';
12  
13 7 ▶around build_tx => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
14 7 my ($orig, $self) = (shift, shift);
15 7 ▶ return $orig->($self, @_) unless $self->signature;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->signature 0 7 7
16 7 $self->signature->apply_signature($orig->($self, @_));
17 2 };
18  
19 2 ▶sub initialize_signature {
Subroutine initialize_signature: called CC=2 SCAR=6.9
Pod documented
20 2 my $self = shift;
21 2 ▶ $self->load_signature(shift)->init($self, ref $_[0] ? $_[0] : {@_});
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 1 2
22 }
23  
24 2 ▶sub load_signature {
Subroutine load_signature: called CC=4 SCAR=13.9
Pod documented
25 2 my ($self, $name) = @_;
26  
27 # Try all namespaces and full module name
28 2 ▶ my $suffix = $name =~ /^[a-z]/ ? camelize $name : $name;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^[a-z]/u ? : 0 2 2
29 2 my @classes = map {"${_}::$suffix"} @{$self->signature_namespaces};
30 2 ▶ for my $class (@classes, $name) { return $class->new if _load($class) }
Branch: 1 branch
BranchTrueFalseTotal
if _load($class) 1 2 3
31 1 return Mojo::UserAgent::Signature::None->new;
32 }
33  
34 3 ▶sub _load {
Subroutine _load: called CC=3 SCAR=11.1
35 3 my $module = shift;
36 3 ▶ return $module->isa('Mojo::UserAgent::Signature::Base')
Branch: 1 branch
BranchTrueFalseTotal
unless my $e = load_class($module) 1 2 3
37 unless my $e = load_class $module;
38 2 ▶ ref $e ? die $e : return undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 2 2
39 }
40  
41 2 1;
42  
43 =encoding utf8
44  
45 =head1 NAME
46  
47 Mojo::UserAgent::Role::Signature - Role for Mojo::UserAgent that automatically
48 signs request transactions
49  
50 =head1 SYNOPSIS
51  
52 use Mojo::UserAgent;
53  
54 my $ua = Mojo::UserAgent->with_roles('+Signature')->new;
55 $ua->initialize_signature(SomeService => {%args});
56 my $tx = $ua->get('/api/for/some/service');
57 say $tx->req->headers->authorization;
58  
59 =head1 DESCRIPTION
60  
61 L<Mojo::UserAgent::Role::Signature> is a role for the full featured non-blocking
62 I/O HTTP and WebSocket user agent L<Mojo::UserAgent>, that automatically signs
63 request transactions.
64  
65 This module modifies the L<Mojo::UserAgent> by wrapping L<Role::Tiny/"around">
66 the L<Mojo::UserAgent/"build_tx"> method with L</"apply_signature"> signing the
67 final built transaction using the object instance set in the L</"signature">
68 attribute that is this module adds to the L<Mojo::UserAgent> class.
69  
70 =head1 ATTRIBUTES
71  
72 =head2 signature
73  
74 $signature = $ua->signature;
75 $ua = $ua->signature(SomeService->new);
76  
77 If this attribute is not defined, the method modifier provided by this
78 L<role|Role::Tiny> will have no effect on the transaction being built
79 by L<Mojo::UserAgent>.
80  
81 =head2 signature_namespaces
82  
83 $namespaces = $ua->signature_namespaces;
84 $ua = $ua->signature_namespaces(['Mojo::UserAgent::Signature']);
85  
86 Namespaces to load signature from, defaults to C<Mojo::UserAgent::Signature>.
87  
88 # Add another namespace to load signature from
89 push @{$ua->namespaces}, 'MyApp::Signature';
90  
91 =head1 METHODS
92  
93 L<Mojo::UserAgent::Role::Signature> inherits all methods from L<Mojo::Base> and
94 implements the following new ones.
95  
96 =head2 initialize_signature
97  
98 $ua->initialize_signature('some_service');
99 $ua->initialize_signature('some_service', foo => 23);
100 $ua->initialize_signature('some_service', {foo => 23});
101 $ua->initialize_signature('SomeService');
102 $ua->initialize_signature('SomeService', foo => 23);
103 $ua->initialize_signature('SomeService', {foo => 23});
104 $ua->initialize_signature('MyApp::Signature::SomeService');
105 $ua->initialize_signature('MyApp::Signature::SomeService', foo => 23);
106 $ua->initialize_signature('MyApp::Signature::SomeService', {foo => 23});
107  
108 Load a signature from the configured namespaces or by full module name and run
109 init, optional arguments are passed through.
110  
111 =head2 load_signature
112  
113 my $signature = $ua->load_signature('some_service');
114 my $signature = $ua->load_signature('SomeService');
115 my $signature = $ua->load_signature('MyApp::Signature::SomeService');
116  
117 Load a signature from the configured namespaces or by full module name. Will
118 fallback to L<Mojo::UserAgent::Signature::None> if the specified signature
119 cannot be loaded.
120  
121 =head1 COPYRIGHT AND LICENSE
122  
123 Copyright (C) 2020, Stefan Adams.
124  
125 This program is free software, you can redistribute it and/or modify it under
126 the terms of the Artistic License version 2.0.
127  
128 =head1 SEE ALSO
129  
130 L<https://github.com/stefanadams/mojo-useragent-role-signature>, L<Mojo::UserAgent>.
131  
132 =cut
« Signature/Base.pm ↑ Summary