File Coverage

blib/lib/XML/OPDS/OpenSearch/Query.pm
Criterion Covered Total %
statement 24 26 92.3
branch 3 6 50.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 34 39 87.1


line stmt bran cond sub pod time code
1             package XML::OPDS::OpenSearch::Query;
2 5     5   30 use strict;
  5         10  
  5         150  
3 5     5   24 use warnings FATAL => 'all';
  5         9  
  5         171  
4 5     5   26 use Types::Standard qw/Maybe Str Enum Int/;
  5         10  
  5         38  
5 5     5   4002 use Moo;
  5         10  
  5         27  
6 5     5   1495 use URI::Escape ();
  5         10  
  5         1837  
7              
8             =head1 NAME
9              
10             XML::OPDS::OpenSearch::Query - OpenSearch query element for XML::OPDS
11              
12             =head1 DESCRIPTION
13              
14             This module is mostly a helper to provide validation to create
15             C<Query> elements in an OPDS search result. Notably, this doesn't
16             expand to XML. The serialization is left to L<XML::OPDS>.
17              
18             =head1 SYNOPSIS
19              
20             use XML::OPDS;
21             use XML::OPDS::OpenSearch::Query;
22             use Data::Page;
23             my $feed = XML::OPDS->new;
24             my $query = XML::OPDS::OpenSearch::Query->new(role => "example",
25             searchTerms => "my terms",
26             );
27             my $pager = Data::Page->new;
28             $pager->total_entries(50);
29             $pager->entries_per_page(20);
30             $pager->current_page(1);
31             $feed->search_result_pager($pager);
32             $feed->search_result_terms('my terms');
33            
34             # please note that if you set the search_result_pager and
35             # search_result_terms a role:request query is added automatically, so
36             # here we add only the example.
37            
38             $feed->search_result_queries([$query]);
39             print $feed->render;
40              
41             =head1 SETTERS/ACCESSORS
42              
43             All of them are optionals except C<role>.
44              
45             =head2 role
46              
47             Role values (from L<http://www.opensearch.org/Specifications/OpenSearch/1.1#Role_values>)
48              
49             =over 4
50              
51             =item request
52              
53             Represents the search query that can be performed to retrieve the same
54             set of search results.
55              
56             =item example
57              
58             Represents a search query that can be performed to demonstrate the search engine.
59              
60             =item related
61              
62             Represents a search query that can be performed to retrieve similar
63             but different search results.
64              
65             =item correction
66              
67             Represents a search query that can be performed to improve the result
68             set, such as with a spelling correction.
69              
70             =item subset
71              
72             Represents a search query that will narrow the current set of search results.
73              
74             =item superset
75              
76             Represents a search query that will broaden the current set of search results.
77              
78             =back
79              
80             =cut
81              
82             has role => (is => 'rw',
83             isa => Enum[qw/role request example related correction subset superset/]);
84              
85             =head2 title
86              
87             Contains a human-readable plain text string describing the search request.
88              
89             Restrictions: The value must contain 256 or fewer characters of plain
90             text. The value must not contain HTML or other markup.
91              
92             This object stores an arbitrary string, but cut it at 256 when
93             producing the attributes.
94              
95             =head2 totalResults
96              
97             Integer.
98              
99             Contains the expected number of results to be found if the search request were made.
100              
101             =head2 searchTerms
102              
103             String.
104              
105             Contains the value representing the "searchTerms" as an OpenSearch 1.1 parameter.
106              
107             The URI escaping is performed by the module.
108              
109             =head2 count
110              
111             Integer.
112              
113             Replaced with the number of search results per page desired by the search client.
114              
115             =head2 startIndex
116              
117             Integer.
118              
119             Replaced with the index of the first search result desired by the search client.
120              
121             =head2 startPage
122              
123             Integer.
124              
125             Replaced with the page number of the set of search results desired by the search client.
126              
127             =head2 language
128              
129             String. The value must conform to the XML 1.0 Language Identification,
130             as specified by RFC 5646. In addition, a value of "*" will signify
131             that the search client desires search results in any language.
132              
133             This module passes it verbatim.
134              
135             =head2 inputEncoding
136              
137             The value must conform to the XML 1.0 Character Encodings, as
138             specified by the IANA Character Set Assignments.
139              
140             This module passes it verbatim.
141              
142             =head2 outputEncoding
143              
144             Same as above.
145              
146             =head1 METHODS
147              
148             =head2 attributes_hashref
149              
150             Return the attributes which are defined in an hashref. The C<title> is
151             mangled to 256 characters and
152              
153             =head1 SEE ALSO
154              
155             Specification: L<http://www.opensearch.org/Specifications/OpenSearch/1.1>
156              
157             =cut
158              
159             has title => (is => 'rw', isa => Maybe[Str]);
160             has totalResults => (is => 'rw', isa => Maybe[Int]);
161             has searchTerms => (is => 'rw', isa => Maybe[Str]);
162             has count => (is => 'rw', isa => Maybe[Int]);
163             has startIndex => (is => 'rw', isa => Maybe[Int]);
164             has startPage => (is => 'rw', isa => Maybe[Int]);
165             has language => (is => 'rw', isa => Maybe[Str]);
166             has inputEncoding => (is => 'rw', isa => Maybe[Str]);
167             has outputEncoding => (is => 'rw', isa => Maybe[Str]);
168              
169             sub attributes_hashref {
170 24     24 1 38 my $self = shift;
171 24         418 my %out = (role => $self->role);
172 24 50       516 if (defined $self->title) {
173 0         0 $out{title} = substr $self->title, 0, 256;
174             }
175 24 50       475 if (defined $self->searchTerms) {
176 24         456 $out{searchTerms} = URI::Escape::uri_escape_utf8($self->searchTerms);
177             }
178 24         1202 foreach my $accessor (qw/totalResults
179             count
180             startIndex
181             startPage
182             language
183             inputEncoding
184             outputEncoding/) {
185 168         2608 my $v = $self->$accessor;
186 168 50       990 if (defined $v) {
187 0         0 $out{$accessor} = $v;
188             }
189             }
190 24         90 return \%out;
191             }
192              
193             1;