Skip to content

Commit

Permalink
Figure out what country search results are in and only use miles insi…
Browse files Browse the repository at this point in the history
…de the USA
  • Loading branch information
autarch committed Oct 6, 2012
1 parent 32070b1 commit 0c9ebf0
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 24 deletions.
24 changes: 13 additions & 11 deletions lib/VegGuide/Controller/Search.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
};

Expand Down Expand Up @@ -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(),
Expand All @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions share/mason/site/api-docs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
78 changes: 65 additions & 13 deletions t/rest-api/Search.t
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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'
);

Expand All @@ -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},
{
Expand Down Expand Up @@ -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'
);

Expand All @@ -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'
);

Expand Down Expand Up @@ -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' );

Expand All @@ -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' );

Expand All @@ -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},
{
Expand Down Expand Up @@ -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.