Commit d5d430d0 authored by Deepak's avatar Deepak

Made random postcode fethcer better

parent 83d46c9a
......@@ -4,6 +4,10 @@ import org.json.JSONObject;
import com.mashape.unirest.http.Unirest;
/**
* @author Deepak
*
*/
public class NearestPostcode {
private static final String LOOKUP_URL = "http://api.postcodes.io/postcodes/";
......
......@@ -100,7 +100,7 @@ public class Postcode {
this.json = json;
}
public static boolean validatePostcode(String postcode) throws JSONException, UnirestException {
public static boolean isValid(String postcode) throws JSONException, UnirestException {
return Unirest.get("https://api.postcodes.io/postcodes/".concat(postcode).concat("/validate")).asJson()
.getBody().getObject().getBoolean("result");
}
......
......@@ -5,7 +5,6 @@ import java.util.List;
import org.json.JSONException;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.postcode.io.initializers.ReverseGeocoding.Reverse;
......@@ -84,17 +83,12 @@ public class PostcodeLookup {
return new ReverseGeocoding(reverses);
}
public static ReverseGeocoding randomPostcode() {
return new ReverseGeocoding("");
}
public static ReverseGeocoding randomPostcode(String outcode) {
return new ReverseGeocoding(outcode);
public static RandomPostcode randomPostcode() {
return new RandomPostcode();
}
public static boolean isValid(String postcode) throws JSONException, UnirestException {
return Unirest.get("https://api.postcodes.io/postcodes/".concat(postcode).concat("/validate")).asJson()
.getBody().getObject().getBoolean("result");
return Postcode.isValid(postcode);
}
public static NearestPostcode nearestPostcode(String postcode) {
......
package com.postcode.io.initializers;
import java.io.IOException;
import org.json.JSONObject;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
/**
* @author Deepak
*
*/
public class RandomPostcode {
private static final String RANDOM_LOOKUP_URL = "https://api.postcodes.io/random/postcodes";
private static String outcode = "";
public RandomPostcode() {
}
public RandomPostcode outcode(String outcode) {
RandomPostcode.outcode = outcode;
return this;
}
public JSONObject asJson() throws IOException, UnirestException {
return Unirest.get(RANDOM_LOOKUP_URL).queryString("outcode", outcode).asJson().getBody().getObject();
}
}
......@@ -20,18 +20,12 @@ public class ReverseGeocoding {
private static final String LOOKUP_URL = "http://api.postcodes.io/postcodes?";
private static final String RANDOM_LOOKUP_URL = "https://api.postcodes.io/random/postcodes";
private static Double longitude;
private static Double latitude;
private static int limit;
private static String outcode;
private static boolean random;
private static int radius;
private static boolean wideSearch;
......@@ -43,11 +37,6 @@ public class ReverseGeocoding {
public ReverseGeocoding() {
}
public ReverseGeocoding(String outcode) {
ReverseGeocoding.outcode = outcode;
ReverseGeocoding.random = true;
}
public ReverseGeocoding(Double longitude, Double latitude) {
ReverseGeocoding.longitude = longitude;
ReverseGeocoding.latitude = latitude;
......@@ -88,9 +77,6 @@ public class ReverseGeocoding {
* @throws UnirestException
*/
public JSONObject asJson() throws IOException, UnirestException {
if (random) {
return Unirest.get(RANDOM_LOOKUP_URL).queryString("outcode", outcode).asJson().getBody().getObject();
}
String url = "";
url = url.concat("lon=").concat(String.valueOf(longitude));
url = url.concat("&lat=").concat(String.valueOf(latitude));
......
......@@ -98,7 +98,7 @@ public class PostcodeLookupTest {
JSONAssert.assertEquals(
Unirest.get("https://api.postcodes.io/random/postcodes").queryString("outcode", "bs347np").asJson()
.getBody().getObject(),
PostcodeLookup.randomPostcode("bs347np").asJson(), JSONCompareMode.LENIENT);
PostcodeLookup.randomPostcode().outcode("bs347np").asJson(), JSONCompareMode.LENIENT);
assertTrue(PostcodeLookup.randomPostcode().asJson().has("result"));
assertEquals(200, PostcodeLookup.randomPostcode().asJson().getInt("status"));
}
......
......@@ -28,8 +28,8 @@ public class PostcodeTest {
@Test
public void testPostcodeValidator() throws JSONException, UnirestException {
assertTrue(Postcode.validatePostcode("ST42EU"));
assertFalse(Postcode.validatePostcode("ST4"));
assertTrue(Postcode.isValid("ST42EU"));
assertFalse(Postcode.isValid("ST4"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment