/
Search.pm
181 lines (135 loc) · 4.42 KB
/
Search.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package VegGuide::Controller::Search;
use strict;
use warnings;
use namespace::autoclean;
use URI::Escape qw( uri_escape );
use VegGuide::Geocoder;
use VegGuide::Search::Vendor::ByLatLong;
use VegGuide::Search::Vendor::ByName;
use VegGuide::SiteURI qw( static_uri );
use Moose;
BEGIN { extends 'VegGuide::Controller::Base'; }
with 'VegGuide::Role::Controller::Search';
{
my %SearchConfig = (
captured_path_position => 2,
search_class => 'VegGuide::Search::Vendor::ByLatLong',
extra_params => sub {
my $caps = $_[0]->request()->captures();
return (
latitude => $caps->[0],
longitude => $caps->[1],
address => 'your location',
);
},
);
sub by_lat_long :
LocalRegex('^by-lat-long/(-?[\d\.]+),(-?[\d\.]+)(?:/filter(?:/(.*)))?$')
: ActionClass('+VegGuide::Action::REST') { }
sub by_lat_long_GET : Private {
my $self = shift;
my $c = shift;
$self->_set_search_in_stash( $c, %SearchConfig );
$self->_set_cursor_params($c);
my $search = $c->stash()->{search};
my $path
= '/search/by-lat-long/'
. uri_escape( $search->latitude() ) . ','
. uri_escape( $search->longitude() );
$self->_search_rest_response( $c, $search, $path );
return;
}
}
{
my %SearchConfig = (
captured_path_position => 1,
search_class => 'VegGuide::Search::Vendor::ByLatLong',
);
sub by_address :
LocalRegex('^by-address/([^/]+)(?:/filter(?:/(.*)))?$')
: ActionClass('+VegGuide::Action::REST') { }
sub by_address_GET : Private {
my $self = shift;
my $c = shift;
my $address = $c->request()->captures()->[0];
my $country = ( split /,\s*/, $address )[-1];
my $geocoder = VegGuide::Geocoder->new( country => $country );
$geocoder ||= VegGuide::Geocoder->new( country => 'USA' );
my $result = $geocoder->geocode_full_address($address);
unless ($result) {
$self->_rest_error_response(
$c,
"The address your provided ($address) could not be resolved to a latitude and longitude.",
'not_found',
);
return;
}
my %config = %SearchConfig;
$config{extra_params} = sub {
return (
latitude => $result->latitude(),
longitude => $result->longitude(),
address => $address,
);
};
$self->_set_search_in_stash( $c, %config );
$self->_set_cursor_params($c);
my $search = $c->stash()->{search};
my $path = '/search/by-address/' . uri_escape( $search->address() );
$self->_search_rest_response( $c, $search, $path );
return;
}
}
sub _set_cursor_params {
my $self = shift;
my $c = shift;
my $search = $c->stash()->{search};
my $limit = $c->request()->parameters()->{limit} || 50;
$limit = 100 if $limit > 100;
my $page = $c->request()->parameters()->{page} || 1;
$search->set_cursor_params(
limit => $limit,
page => $page,
);
return;
}
sub _search_rest_response {
my $self = shift;
my $c = shift;
my $search = shift;
my $path = shift;
my %rest = (
entry_count => $search->count(),
distance_unit => $search->unit(),
);
my $country;
my $vendors = $search->vendors();
while ( my $vendor = $vendors->next() ) {
$country ||= $vendor->location()->country();
my $entry_rest = $vendor->rest_data( include_related => 0 );
my $distance = $vendor->distance_from(
latitude => $search->latitude(),
longitude => $search->longitude(),
unit => $search->unit(),
);
$entry_rest->{distance} = $distance;
push @{ $rest{entries} }, $entry_rest;
$rest{region}
||= $vendor->location()->rest_data( include_related => 0 );
}
$rest{uri} = static_uri(
path => $path,
query => {
distance => $search->distance(),
unit => ( $country && $country eq 'USA' ? 'mile' : 'km' ),
},
with_host => 1,
);
$self->_rest_response(
$c,
'search',
\%rest,
);
}
__PACKAGE__->meta()->make_immutable();
1;