| 1 | package Interchange::Search::Solr::Builder; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Moo; | ||
| 7 | use Types::Standard qw/ArrayRef HashRef Int/; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Interchange::Search::Solr::Builder | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | L<WebService::Solr::Response> subclass for building url. | ||
| 16 | |||
| 17 | =head1 ACCESSORS | ||
| 18 | |||
| 19 | =head2 terms | ||
| 20 | |||
| 21 | An arrayref contains search terms | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | has terms => ( | ||
| 26 | is => 'rw', | ||
| 27 | isa => ArrayRef | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head2 filters | ||
| 31 | |||
| 32 | A hashref which key is a feild of data and the value is keyword | ||
| 33 | that needs to filter. | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | has filters => ( | ||
| 38 | is => 'rw', | ||
| 39 | isa => HashRef | ||
| 40 | ); | ||
| 41 | |||
| 42 | =head2 facets | ||
| 43 | |||
| 44 | A string or an arrayref with the fields which will generate a facet. | ||
| 45 | Defaults to | ||
| 46 | [qw/suchbegriffe manufacturer/] | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | has facets => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => ArrayRef, | ||
| 53 | default => sub { | ||
| 54 | return [qw/suchbegriffe manufacturer/]; | ||
| 55 | } | ||
| 56 | ); | ||
| 57 | |||
| 58 | =head2 page | ||
| 59 | |||
| 60 | A number of page and must be positive number (>= 1) | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | has page => ( | ||
| 65 | is => 'rw', | ||
| 66 | isa => sub { | ||
| 67 | if (defined $_[0]){ | ||
| 68 | die "$_[0] is not integer" if $_[0] !~ /^\d+$/; | ||
| 69 | die "must be positive number" unless $_[0] >= 1 | ||
| 70 | } | ||
| 71 | } | ||
| 72 | ); | ||
| 73 | |||
| 74 | =head1 METHODS | ||
| 75 | |||
| 76 | In addition to all the L<WebService::Solr::Response> methods this | ||
| 77 | class have the following methods: | ||
| 78 | |||
| 79 | =head2 url_builder; | ||
| 80 | |||
| 81 | Build a query url with the parameter passed | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | sub url_builder { | ||
| 86 | my $self = shift; | ||
| 87 | |||
| 88 | my @fragments; | ||
| 89 | if (@{$self->terms}) { | ||
| 90 | push @fragments, 'words', @{$self->terms}; | ||
| 91 | } | ||
| 92 | |||
| 93 | if (%{$self->filters}) { | ||
| 94 | foreach my $facet (@{ $self->facets }) { | ||
| 95 | if (my $terms = $self->filters->{$facet}) { | ||
| 96 | push @fragments, $facet, @$terms; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | if ($self->page and $self->page > 1) { | ||
| 101 | push @fragments, page => $self->page; | ||
| 102 | } | ||
| 103 | return join ('/', @fragments); | ||
| 104 | } | ||
| 105 | |||
| 106 | 1; |