lib/HTTP/Promise/Headers/Accept.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 62.5% 5 / 8 Conditioncond 50.0% 4 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 85.7% 12 / 14 Podpod 100% 8 / 8 totaltotal 79.7% 63 / 79 CC 8 SCAR 14.3
CC
8
Cov
83%
init
9.1
add
2.0
match
2.0
CRAP 4.2
« blib/lib/HTTP/Promise/Pool.pm ↑ Summary lib/HTTP/Promise/Headers/ExpectCT.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/Accept.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/05/06
7 ## Modified 2022/05/06
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Headers::Accept;
15 BEGIN
16 {
17 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use parent qw( HTTP::Promise::Headers::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 our $VERSION = 'v0.1.0';
22 };
23  
24 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub init
28 {
29 20 ▶ my $self = shift( @_ );
Subroutine init: called CC=8 SCAR=22.1
Pod documented
30 20 $self->{_qv_elements} = [];
31 20 ▶ @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and $self->_is_a($_[0], "Module::Generic::Null") 0 20 20
Condition66%2/3
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
!ll&&!rl&&r
5150
MC/DC0%0/2
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
@_ == 1 $self->_is_a($_[0], "Module::Generic::Null")
Truth table66%2/3
A and B
A= @_ == 1B= $self->_is_a($_[0], "Module::Generic::Null")
ABresult
0X0
100
111
32 20 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 5 20
33 {
34 15 my $val = shift( @_ );
35 15 ▶ return( $self->error( "No value was provided." ) ) if( !defined( $val ) || !length( $val ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and length $val 0 15 15
Condition33%1/3
defined $val and length $val
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $val and length $val
defined $val length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
36 15 ▶ my $choices = $self->_parse_quality_value( $val ) ||
Condition50%1/2
$self->_parse_quality_value($val) || (return $self->pass_error)
l!l
150
MC/DC0%0/1
$self->_parse_quality_value($val) || (return $self->pass_error)
$self->_parse_quality_value($val)
Truth table50%1/2
A
A= $self->_parse_quality_value($val)
Aresult
11
00
37 return( $self->pass_error );
38 15 $self->elements( $choices );
39 }
40 20 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 20 20
41 20 $self->_field_name( 'Accept' );
42 20 return( $self );
43 }
44  
45 0 ▶sub add { return( shift->_qv_add( @_ ) ); }
Subroutine add: not called CC=1 SCAR=6.9
Pod documented
46  
47 25 ▶sub as_string { return( shift->_qv_as_string( @_ ) ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
48  
49 64 ▶sub elements { return( shift->_qv_elements( @_ ) ); }
Subroutine elements: called CC=1 SCAR=0.0
Pod documented
50  
51 5 ▶sub get { return( shift->_qv_get( @_ ) ); }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
52  
53 0 ▶sub match { return( shift->_qv_match( @_ ) ); }
Subroutine match: not called CC=1 SCAR=6.9
Pod documented
54  
55 1 ▶sub remove { return( shift->_qv_remove( @_ ) ); }
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
56  
57 1 ▶sub sort { return( shift->_qv_sort( @_ ) ); }
Subroutine sort: called CC=1 SCAR=0.0
Pod documented
58  
59 3 1;
60 # NOTE: POD
61 __END__
62  
63 =encoding utf-8
64  
65 =head1 NAME
66  
67 HTTP::Promise::Headers::Accept - Accept Header Field
68  
69 =head1 SYNOPSIS
70  
71 use HTTP::Promise::Headers::Accept;
72 my $ac = HTTP::Promise::Headers::Accept->new ||
73 die( HTTP::Promise::Headers::Accept->error, "\n" );
74 my $ac = HTTP::Promise::Headers::Accept->new( 'text/html, application/json, application/xml;q=0.9, */*;q=0.8' ) ||
75 die( HTTP::Promise::Headers::Accept->error, "\n" );
76 $ac->add( 'text/html' );
77 $ac->add( 'application/json' => 0.7 );
78 $h->accept( $ac->as_string ); Accept: text/html, application/json;q=0.7
79 # or
80 $h->accept( "$ac" );
81 my $qv_elements = $ac->elements;
82 my $obj = $ac->get( 'text/html' );
83 # change the weight
84 $obj->value( 0.3 );
85 $ac->remove( 'text/html' );
86 my $sorted_objects = $ac->sort;
87 my $asc_sorted = $ac->sort(1);
88 # Returns a Module::Generic::Array object
89 my $ok = $ac->match( [qw( application/json text/html )] );
90  
91 =head1 VERSION
92  
93 v0.1.0
94  
95 =head1 DESCRIPTION
96  
97 The following description is taken from Mozilla documentation.
98  
99 Accept: image/*
100 Accept: text/html
101 Accept: */*
102 Accept: text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
103  
104 See L<rfc7231 section 5.3.2|https://tools.ietf.org/html/rfc7231#section-5.3.2> and L<rfc7231, section 5.3.5|https://tools.ietf.org/html/rfc7231#section-5.3.5>
105  
106 =head1 METHODS
107  
108 =head2 add
109  
110 Provided with a mime type and a quality weight (q-value), and this will push a new C<HTTP::Promise::Field::QualityValue> object to the stack of elements.
111  
112 =head2 as_string
113  
114 Returns a string representation of the C<Accept> object.
115  
116 =head2 elements
117  
118 Retrieve an L<array object|Module::Generic::Array> of C<HTTP::Promise::Field::QualityValue> objects.
119  
120 =head2 get
121  
122 Provided with a mime type and this returns its C<HTTP::Promise::Field::QualityValue> object entry, if any. If nothing corresponding was found, it returns an empty string (false, but not C<undef>)
123  
124 =head2 match
125  
126 Provided with a either an array reference of proposed values, or a string, or something that stringifies, and this will return an L<array object|Module::Generic::Array> of values that match the supported one and in the order of preference.
127  
128 For example, consider:
129  
130 Accept: text/html;q=0.3, application/json;q=0.7; text/plain;q=0.2
131  
132 # We prefer text/html first and application/json second
133 my $ok = $ac->match( [qw( text/html application/json )] );
134  
135 C<$ok> would contain 2 entries: C<application/json> and C<text/html>
136  
137 # Only take the first one
138 my $mime_type = $ac->match( [qw( text/html application/json )] )->first;
139 # or get it as a list
140 my @ok_types = $ac->match( [qw( text/html application/json )] )->list;
141  
142 =head2 remove
143  
144 Provided with a mime type and this removes its C<HTTP::Promise::Field::QualityValue> object from the list of elements.
145  
146 =head2 sort
147  
148 This returns a sorted L<array object|Module::Generic::Array> of C<HTTP::Promise::Field::QualityValue> objects, based on their weight factor. If the option C<reverse> is provided and true, this will sort the elements in reverse order, that is in incremental order, from smallest to biggest quality factor.
149  
150 For example, the following:
151  
152 text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8
153  
154 will be sorted as:
155  
156 text/html, application/xhtml+xml, image/webp, application/xml;q=0.9, */*;q=0.8
157  
158 =head1 THREAD-SAFETY
159  
160 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
161  
162 =head1 AUTHOR
163  
164 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
165  
166 =head1 SEE ALSO
167  
168 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
169  
170 L<HTTP::Accept>
171  
172 =head1 COPYRIGHT & LICENSE
173  
174 Copyright(c) 2022 DEGUEST Pte. Ltd.
175  
176 All rights reserved.
177  
178 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
179  
180 =cut
« blib/lib/HTTP/Promise/Pool.pm ↑ Summary lib/HTTP/Promise/Headers/ExpectCT.pm »