Permalink
Browse files

Figure out what country search results are in and only use miles insi…

…de the USA
  • Loading branch information...
autarch committed Oct 6, 2012
1 parent 32070b1 commit 0c9ebf00d5342fb68c97bcaaf17eb40670cde980
Showing with 83 additions and 24 deletions.
  1. +13 −11 lib/VegGuide/Controller/Search.pm
  2. +5 −0 share/mason/site/api-docs
  3. +65 −13 t/rest-api/Search.t
@@ -87,15 +87,12 @@ with 'VegGuide::Role::Controller::Search';
return;
}
- my $unit = $geocoder->country() eq 'USA' ? 'mile' : 'km';
-
my %config = %SearchConfig;
$config{extra_params} = sub {
return (
latitude => $result->latitude(),
longitude => $result->longitude(),
address => $address,
- unit => $unit,
);
};
@@ -139,18 +136,14 @@ sub _search_rest_response {
my %rest = (
entry_count => $search->count(),
- uri => static_uri(
- path => $path,
- query => {
- distance => $search->distance(),
- unit => $search->unit(),
- },
- with_host => 1,
- )
);
+ 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(),
@@ -169,6 +162,15 @@ sub _search_rest_response {
||= $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',
@@ -1152,6 +1152,11 @@ unless ($toc) {
listings, and entry images. The user end point and entry review end points
still return the complete User data structure.
</li>
+ <li>
+ Lat/long searches always returned results based on miles, even though the
+ docs said that miles are only used for locations inside the USA. This has
+ been fixed.
+ </li>
</ul>
<h3>0.0.1 - September 1, 2012</h3>
View
@@ -8,6 +8,7 @@ use Test::More 0.88;
use Test::VegGuide qw( json_ok path_to_uri rest_request use_test_database );
use URI;
+use URI::QueryParam;
use_test_database();
@@ -22,7 +23,8 @@ use_test_database();
is(
$response->header('Content-Type'),
- 'application/vnd.vegguide.org-search+json; charset=UTF-8; version=' . $VegGuide::REST_VERSION,
+ 'application/vnd.vegguide.org-search+json; charset=UTF-8; version='
+ . $VegGuide::REST_VERSION,
'got the right RESTful content type'
);
@@ -35,6 +37,12 @@ use_test_database();
'got expected uri path for search'
);
+ is(
+ $uri->query_param('unit'),
+ 'mile',
+ 'unit query param is mile for lat/long in USA'
+ );
+
is_deeply(
$search->{region},
{
@@ -81,15 +89,17 @@ use_test_database();
{
my $response = request(
rest_request(
- GET => '/search/by-lat-long/44.9479791,-93.2935778?distance=1;limit=5'
+ GET =>
+ '/search/by-lat-long/44.9479791,-93.2935778?distance=1;limit=5'
)
);
is( $response->code(), '200', 'got a 200 response' );
is(
$response->header('Content-Type'),
- 'application/vnd.vegguide.org-search+json; charset=UTF-8; version=' . $VegGuide::REST_VERSION,
+ 'application/vnd.vegguide.org-search+json; charset=UTF-8; version='
+ . $VegGuide::REST_VERSION,
'got the right RESTful content type'
);
@@ -114,18 +124,35 @@ use_test_database();
);
}
+{
+ my $response = request(
+ rest_request( GET => '/search/by-lat-long/48.1189,11.5713' ) );
+
+ my $search = json_ok($response);
+
+ my $uri = URI->new( $search->{uri} );
+
+ is(
+ $uri->query_param('unit'),
+ 'km',
+ 'unit query param is km for lat/long in Germany'
+ );
+}
+
{
my $response = request(
rest_request(
- GET => '/search/by-lat-long/44.9479791,-93.2935778?distance=1;limit=5;page=2'
+ GET =>
+ '/search/by-lat-long/44.9479791,-93.2935778?distance=1;limit=5;page=2'
)
);
is( $response->code(), '200', 'got a 200 response' );
is(
$response->header('Content-Type'),
- 'application/vnd.vegguide.org-search+json; charset=UTF-8; version=' . $VegGuide::REST_VERSION,
+ 'application/vnd.vegguide.org-search+json; charset=UTF-8; version='
+ . $VegGuide::REST_VERSION,
'got the right RESTful content type'
);
@@ -157,10 +184,7 @@ use_test_database();
};
my $response = request(
- rest_request(
- GET => '/search/by-address/not-found?distance=1'
- )
- );
+ rest_request( GET => '/search/by-address/not-found?distance=1' ) );
is( $response->code(), '404', 'got a 404 response for bad address' );
@@ -185,10 +209,7 @@ use_test_database();
};
my $response = request(
- rest_request(
- GET => '/search/by-address/good-address?distance=1'
- )
- );
+ rest_request( GET => '/search/by-address/good-address?distance=1' ) );
is( $response->code(), '200', 'got a 200 response for good address' );
@@ -201,6 +222,12 @@ use_test_database();
'got expected uri path for search'
);
+ is(
+ $uri->query_param('unit'),
+ 'mile',
+ 'unit query param is mile for address in USA'
+ );
+
is_deeply(
$search->{region},
{
@@ -235,5 +262,30 @@ use_test_database();
);
}
+{
+ no warnings 'redefine';
+ local *VegGuide::Geocoder::geocode_full_address = sub {
+ return VegGuide::Geocoder::Result->new(
+ {
+ Point => { coordinates => [ 11.5713, 48.1189 ] },
+ address => 'good address',
+ },
+ );
+ };
+
+ my $response
+ = request( rest_request( GET => '/search/by-address/good-address' ) );
+
+ my $search = json_ok($response);
+
+ my $uri = URI->new( $search->{uri} );
+
+ is(
+ $uri->query_param('unit'),
+ 'km',
+ 'unit query param is km for lat/long in Germany'
+ );
+}
+
done_testing();

0 comments on commit 0c9ebf0

Please sign in to comment.