Blossom/PaymentRequired.pm

filter: Statementstmt 87.8% 36 / 41 Branchbran 64.2% 9 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 80.0% 8 / 10 Podpod 100% 4 / 4 totaltotal 83.7% 62 / 74 CC 9 SCAR 12.3
CC
9
Cov
83%
payment_challenge
6.0
BUILDARGS
5.0
BUILD
4.1
CRAP 3.4
« Blossom/_ConstructorArgs.pm ↑ Summary Blossom/AuthToken.pm »
1 package Net::Blossom::PaymentRequired;
2  
3 13 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 13 ▶use parent 'Net::Blossom::Error';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Net::Blossom::_ConstructorArgs ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Class::Tiny qw(method url status reason x_reason headers body), {
Subroutine BEGIN: called CC=1 SCAR=0.2
11 0 ▶ payment_challenges => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
12 13 };
13  
14 7 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=5 SCAR=16.1
Pod documented
15 7 my $class = shift;
16 7 my %args = Net::Blossom::_ConstructorArgs::normalize(@_);
17 7 my %known = map { $_ => 1 } qw(method url status reason x_reason headers body payment_challenges);
18 7 my @unknown = grep { !exists $known{$_} } keys %args;
19 7 ▶ croak "unknown argument(s): " . join(', ', sort @unknown) if @unknown;
Branch: 1 branch
BranchTrueFalseTotal
if @unknown 0 7 7
20 croak "payment_challenges must be a hash reference"
21 7 ▶ if defined $args{payment_challenges} && ref($args{payment_challenges}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"payment_challenges"} and ref $args{"payment_challenges"} ne "HASH" 1 6 7
Condition100%3/3
defined $args{"payment_challenges"} and ref $args{"payment_challenges"} ne "HASH"
!ll&&!rl&&r
151
MC/DC100%2/2
defined $args{"payment_challenges"} and ref $args{"payment_challenges"} ne "HASH"
defined $args{"payment_challenges"} ref $args{"payment_challenges"} ne "HASH"
Truth table100%3/3
A and B
A= defined $args{"payment_challenges"}B= ref $args{"payment_challenges"} ne "HASH"
ABresult
0X0
100
111
22  
23 6 my $payment_challenges = delete $args{payment_challenges};
24 6 my $normalized = $class->SUPER::BUILDARGS(%args);
25 6 ▶ $normalized->{payment_challenges} = $payment_challenges
Branch: 1 branch
BranchTrueFalseTotal
if defined $payment_challenges 5 1 6
26 if defined $payment_challenges;
27 6 return $normalized;
28 }
29  
30 2 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=14.1
Pod documented
31 2 my ($self) = @_;
32 2 ▶ croak "status must be 402 for PaymentRequired" unless $self->status == 402;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status == 402 1 1 2
33 1 ▶ $self->payment_challenges({}) unless defined $self->payment_challenges;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->payment_challenges 0 1 1
34 1 ▶ croak "payment_challenges must be a hash reference"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->payment_challenges eq "HASH" 0 1 1
35 unless ref($self->payment_challenges) eq 'HASH';
36 1 return;
37 }
38  
39 1 ▶sub payment_methods {
Subroutine payment_methods: called CC=1 SCAR=0.0
Pod documented
40 1 my ($self) = @_;
41 1 return sort keys %{$self->payment_challenges};
42 }
43  
44 0 ▶sub payment_challenge {
Subroutine payment_challenge: not called CC=2 SCAR=17.9
Pod documented
45 0 my ($self, $method) = @_;
46 0 ▶ return undef unless defined $method;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method 0 0 0
47 0 $method =~ s/\AX-//i;
48 0 return $self->payment_challenges->{lc $method};
49 }
50  
51 13 1;
52  
53 =pod
54  
55 =head1 NAME
56  
57 Net::Blossom::PaymentRequired - Blossom 402 payment challenge error
58  
59 =head1 SYNOPSIS
60  
61 my $error = eval { $client->get_blob($sha256); 1 } ? undef : $@;
62  
63 if (ref($error) && $error->isa('Net::Blossom::PaymentRequired')) {
64 my @methods = $error->payment_methods;
65 my $cashu = $error->payment_challenge('cashu');
66 }
67  
68 =head1 DESCRIPTION
69  
70 C<Net::Blossom::PaymentRequired> represents a C<402 Payment Required> response
71 from a Blossom server. It is a subclass of C<Net::Blossom::Error>.
72  
73 When this object is produced by C<Net::Blossom::Client>, payment challenges are
74 parsed from non-reserved C<X-*> response headers. Known C<cashu> and
75 C<lightning> challenges are validated before being exposed. Cashu validation
76 supports the NUT-24 HTTP payment profile in both NUT-18 C<creqA> and NUT-26
77 C<creqB> encodings. Unknown future payment methods are preserved when they
78 have a scalar non-empty payload.
79  
80 =head1 CONSTRUCTOR
81  
82 =head2 new
83  
84 my $error = Net::Blossom::PaymentRequired->new(%args);
85  
86 Accepts the same required arguments as C<Net::Blossom::Error-E<gt>new>, but
87 C<status> must be C<402>. Optional C<payment_challenges> must be a hash reference
88 and defaults to an empty hash reference.
89  
90 =head1 ACCESSORS
91  
92 =head2 method
93  
94 Returns the HTTP method.
95  
96 =head2 url
97  
98 Returns the request URL.
99  
100 =head2 status
101  
102 Returns the HTTP status code.
103  
104 =head2 reason
105  
106 Returns the HTTP reason phrase.
107  
108 =head2 x_reason
109  
110 Returns the optional C<X-Reason> diagnostic.
111  
112 =head2 headers
113  
114 Returns the response headers hash reference.
115  
116 =head2 body
117  
118 Returns the response body.
119  
120 =head2 payment_challenges
121  
122 Returns the payment challenge hash reference keyed by normalized method name.
123  
124 =head1 METHODS
125  
126 =head2 payment_methods
127  
128 my @methods = $error->payment_methods;
129  
130 Returns sorted payment method names as a list.
131  
132 =head2 payment_challenge
133  
134 my $challenge = $error->payment_challenge($method);
135  
136 Returns the challenge string for C<$method>. C<$method> may include an C<X->
137 prefix. Returns C<undef> when the method is unknown or undefined.
138  
139 =head2 as_string
140  
141 Inherited from C<Net::Blossom::Error>.
142  
143 =head1 INTERNAL METHODS
144  
145 =head2 BUILDARGS
146  
147 Normalizes constructor arguments for Class::Tiny.
148  
149 =head2 BUILD
150  
151 Validates the constructed object for Class::Tiny.
152  
153 =cut
« Blossom/_ConstructorArgs.pm ↑ Summary Blossom/AuthToken.pm »