blib/lib/HTTP/Promise/Headers/Accept.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 75 CC 4 SCAR 18.3
CC
4
Cov
0%
init
20.0
BEGIN
2.0
add
2.0
CRAP 6.2
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 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use parent qw( HTTP::Promise::Headers::Generic );
21 our $VERSION = 'v0.1.0';
22 };
23  
24 use strict;
25 use warnings;
26  
27 sub init
28 {
29 my $self = shift( @_ );
30 $self->{_qv_elements} = [];
31 @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
32 if( @_ )
33 {
34 my $val = shift( @_ );
35 return( $self->error( "No value was provided." ) ) if( !defined( $val ) || !length( $val ) );
36 my $choices = $self->_parse_quality_value( $val ) ||
37 return( $self->pass_error );
38 $self->elements( $choices );
39 }
40 $self->SUPER::init( @_ ) || return( $self->pass_error );
41 $self->_field_name( 'Accept' );
42 return( $self );
43 }
44  
45 sub add { return( shift->_qv_add( @_ ) ); }
46  
47 sub as_string { return( shift->_qv_as_string( @_ ) ); }
48  
49 sub elements { return( shift->_qv_elements( @_ ) ); }
50  
51 sub get { return( shift->_qv_get( @_ ) ); }
52  
53 sub match { return( shift->_qv_match( @_ ) ); }
54  
55 sub remove { return( shift->_qv_remove( @_ ) ); }
56  
57 sub sort { return( shift->_qv_sort( @_ ) ); }
58  
59 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