-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from sergiandreplace/feature/api_fallback
Feature/api fallback
- Loading branch information
Showing
9 changed files
with
382 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
leku/src/androidTest/java/com/schibsted/leku/geocoder/api/AddressBuilderShould.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.schibsted.leku.geocoder.api; | ||
|
||
import android.location.Address; | ||
import android.support.annotation.NonNull; | ||
import com.schibstedspain.leku.geocoder.api.AddressBuilder; | ||
import java.util.List; | ||
import org.json.JSONException; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.mockito.junit.MockitoRule; | ||
|
||
import static junit.framework.Assert.assertEquals; | ||
import static junit.framework.Assert.assertTrue; | ||
import static org.mockito.junit.MockitoJUnit.rule; | ||
|
||
public class AddressBuilderShould { | ||
|
||
@Rule public MockitoRule mockitoRule = rule(); | ||
|
||
private AddressBuilder addressBuilder; | ||
|
||
@Before | ||
public void setUp() { | ||
addressBuilder = new AddressBuilder(); | ||
} | ||
|
||
@Test | ||
public void returnExpectedAddressWhenJsonProvided() throws JSONException { | ||
String json = getJson(); | ||
|
||
List<Address> addresses = addressBuilder.parseResult(json); | ||
|
||
assertEquals("Barcelona", addresses.get(0).getLocality()); | ||
assertEquals("Carrer del Comte d'Urgell, 102", addresses.get(0).getAddressLine(0)); | ||
assertEquals("08011", addresses.get(0).getPostalCode()); | ||
assertTrue(Double.valueOf(41.3838035).equals(addresses.get(0).getLatitude())); | ||
assertTrue(Double.valueOf(2.1568617).equals(addresses.get(0).getLongitude())); | ||
|
||
} | ||
|
||
@Test | ||
public void returnExpectedAddressWhenJsonWithOnlyCityProvided() throws JSONException { | ||
String json = getJsonForOnlyCity(); | ||
|
||
List<Address> addresses = addressBuilder.parseResult(json); | ||
|
||
assertEquals("Barcelona", addresses.get(0).getLocality()); | ||
assertEquals("", addresses.get(0).getAddressLine(0)); | ||
assertEquals("", addresses.get(0).getPostalCode()); | ||
assertTrue(Double.valueOf(41.3850639).equals(addresses.get(0).getLatitude())); | ||
assertTrue(Double.valueOf(2.1734035).equals(addresses.get(0).getLongitude())); | ||
|
||
} | ||
|
||
@NonNull | ||
private String getJson() { | ||
return "{\"results\": [{\"address_components\": [{\"long_name\": \"102\",\"short_name\": \"102\",\"types\": " | ||
+ "[ \"street_number\"]},{\"long_name\": \"Carrer del Comte d'Urgell\",\"short_name\": \"Carrer del Comte d'Urgell\",\"types\": " | ||
+ "[ \"route\"]},{\"long_name\": \"Barcelona\",\"short_name\": \"Barcelona\",\"types\": [ \"locality\", \"political\"]}," | ||
+ "{\"long_name\": \"Barcelona\",\"short_name\": \"Barcelona\",\"types\": [ \"administrative_area_level_2\", \"political\"]}," | ||
+ "{\"long_name\": \"Catalunya\",\"short_name\": \"CT\",\"types\": [ \"administrative_area_level_1\", \"political\"]}," | ||
+ "{\"long_name\": \"Spain\",\"short_name\": \"ES\",\"types\": [ \"country\", \"political\"]},{\"long_name\": \"08011\"," | ||
+ "\"short_name\": \"08011\",\"types\": [ \"postal_code\"]}],\"formatted_address\"" | ||
+ ": \"Carrer del Comte d'Urgell, 102, 08011 Barcelona, Spain\",\"geometry\": {\"bounds\": {\"northeast\": " | ||
+ "{ \"lat\": 41.3839416, \"lng\": 2.1570442},\"southwest\": { \"lat\": 41.3836653, \"lng\": 2.1566792}},\"location\": " | ||
+ "{\"lat\": 41.3838035,\"lng\": 2.1568617},\"location_type\": \"ROOFTOP\",\"viewport\": {\"northeast\": " | ||
+ "{ \"lat\": 41.3851524302915, \"lng\": 2.158210680291502},\"southwest\": { \"lat\": 41.3824544697085, " | ||
+ "\"lng\": 2.155512719708498}}},\"partial_match\": true,\"place_id\": \"ChIJdehx-YiipBIR8hitzOckUuo\",\"types\": [\"premise\"] } " | ||
+ "], \"status\": \"OK\"}"; | ||
} | ||
|
||
@NonNull | ||
private String getJsonForOnlyCity() { | ||
return "{\"results\": [{\"address_components\": [{\"long_name\": \"Barcelona\",\"short_name\": \"Barcelona\",\"types\": " | ||
+ "[\"locality\",\"political\"]},{\"long_name\": \"Barcelona\",\"short_name\": \"Barcelona\",\"types\": " | ||
+ "[\"administrative_area_level_2\",\"political\"]},{\"long_name\": \"Catalonia\",\"short_name\": \"CT\",\"types\": " | ||
+ "[\"administrative_area_level_1\",\"political\"]},{\"long_name\": \"Spain\",\"short_name\": \"ES\",\"types\": " | ||
+ "[\"country\",\"political\"]}],\"formatted_address\": \"Barcelona, Spain\",\"geometry\": {\"bounds\": {\"northeast\": " | ||
+ "{\"lat\": 41.4695761,\"lng\": 2.2280099},\"southwest\": {\"lat\": 41.320004,\"lng\": 2.0695258}},\"location\":" | ||
+ " {\"lat\": 41.3850639,\"lng\": 2.1734035},\"location_type\": \"APPROXIMATE\",\"viewport\": {\"northeast\": " | ||
+ "{\"lat\": 41.4695761,\"lng\": 2.2280099},\"southwest\": {\"lat\": 41.320004,\"lng\": 2.0695258}}},\"place_id\": " | ||
+ "\"ChIJ5TCOcRaYpBIRCmZHTz37sEQ\",\"types\": [\"locality\",\"political\"]}],\"status\": \"OK\"}"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
leku/src/main/java/com/schibstedspain/leku/geocoder/GeocoderAPIInteractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.schibstedspain.leku.geocoder; | ||
|
||
import android.location.Address; | ||
import com.google.android.gms.maps.model.LatLng; | ||
import com.schibstedspain.leku.geocoder.api.AddressBuilder; | ||
import com.schibstedspain.leku.geocoder.api.NetworkClient; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import org.json.JSONException; | ||
import rx.Observable; | ||
|
||
public class GeocoderAPIInteractor implements GeocoderInteractorInterface { | ||
|
||
private static final String QUERY_REQUEST = "https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s"; | ||
private static final String QUERY_REQUEST_WITH_RECTANGLE | ||
= "https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s&bounds=%3$f,%4$f|%5$f,%6$f"; | ||
private static final String QUERY_LAT_LONG = "https://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&key=%3$s"; | ||
private String apiKey; | ||
private final NetworkClient networkClient; | ||
private final AddressBuilder addressBuilder; | ||
|
||
public GeocoderAPIInteractor(NetworkClient networkClient, AddressBuilder addressBuilder) { | ||
this.networkClient = networkClient; | ||
this.addressBuilder = addressBuilder; | ||
} | ||
|
||
public void setApiKey(String apiKey) { | ||
this.apiKey = apiKey; | ||
} | ||
|
||
@Override | ||
public Observable<List<Address>> getFromLocationName(String query) { | ||
return Observable.create(subscriber -> { | ||
if (apiKey == null) { | ||
subscriber.onCompleted(); | ||
return; | ||
} | ||
try { | ||
String result = networkClient.requestFromLocationName(String.format(Locale.ENGLISH, | ||
QUERY_REQUEST, query.trim(), apiKey)); | ||
List<Address> addresses = addressBuilder.parseResult(result); | ||
subscriber.onNext(addresses); | ||
subscriber.onCompleted(); | ||
} catch (JSONException e) { | ||
subscriber.onError(e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Observable<List<Address>> getFromLocationName(String query, LatLng lowerLeft, | ||
LatLng upperRight) { | ||
return Observable.create(subscriber -> { | ||
if (apiKey == null) { | ||
subscriber.onCompleted(); | ||
return; | ||
} | ||
try { | ||
String result = networkClient.requestFromLocationName(String.format(Locale.ENGLISH, | ||
QUERY_REQUEST_WITH_RECTANGLE, query.trim(), apiKey, lowerLeft.latitude, | ||
lowerLeft.longitude, upperRight.latitude, upperRight.longitude)); | ||
List<Address> addresses = addressBuilder.parseResult(result); | ||
subscriber.onNext(addresses); | ||
subscriber.onCompleted(); | ||
} catch (JSONException e) { | ||
subscriber.onError(e); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public Observable<List<Address>> getFromLocation(double latitude, double longitude) { | ||
return Observable.create(subscriber -> { | ||
if (apiKey == null) { | ||
subscriber.onCompleted(); | ||
return; | ||
} | ||
try { | ||
String result = networkClient.requestFromLocationName(String.format(Locale.ENGLISH, | ||
QUERY_LAT_LONG, latitude, longitude, apiKey)); | ||
List<Address> addresses = addressBuilder.parseResult(result); | ||
subscriber.onNext(addresses); | ||
subscriber.onCompleted(); | ||
} catch (JSONException e) { | ||
subscriber.onError(e); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.