Commit 5c15e23d authored by Deepak's avatar Deepak

Postcode Auto complete feature added

parent d5d430d0
package com.postcode.io.initializers;
import org.json.JSONObject;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
/**
* @author Deepak
*
*/
public class LimitPostcode {
private static final String AUTOCOMPLETE_URL = "https://api.postcodes.io/postcodes/";
private static String postcode;
private static int limit;
public LimitPostcode(String postcode) {
LimitPostcode.postcode = postcode;
}
/**
* Limits number of postcodes matches to return. Defaults to 10. Needs to be less than 100.
*
* @param limit
* @return
*/
public LimitPostcode limit(int limit) {
LimitPostcode.limit = limit;
return this;
}
public JSONObject asJson() throws UnirestException {
return Unirest.get(AUTOCOMPLETE_URL.concat(postcode).concat("/autocomplete"))
.queryString("limit", limit != 0 ? limit : 10).asJson().getBody().getObject();
}
}
......@@ -95,4 +95,8 @@ public class PostcodeLookup {
return new NearestPostcode(postcode);
}
public static LimitPostcode autocomplete(String postcode) {
return new LimitPostcode(postcode);
}
}
......@@ -119,4 +119,12 @@ public class PostcodeLookupTest {
.length());
}
@Test
public void testAutoComplete() throws Exception {
assertEquals(200, PostcodeLookup.autocomplete("ST4").asJson().get("status"));
assertEquals("ST4 1AA", PostcodeLookup.autocomplete("ST4").asJson().getJSONArray("result").get(0));
assertEquals(10, PostcodeLookup.autocomplete("ST4").asJson().getJSONArray("result").length());
assertEquals(20, PostcodeLookup.autocomplete("ST4").limit(20).asJson().getJSONArray("result").length());
}
}
\ No newline at end of file
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