Skip to content

Commit

Permalink
Add standings
Browse files Browse the repository at this point in the history
  • Loading branch information
olerom committed Mar 13, 2017
1 parent c60a956 commit b4f17d2
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 14 deletions.
19 changes: 12 additions & 7 deletions console/src/main/java/com/olerom/formula/console/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ private void run() {
Ergast ergast = new Ergast(2016, 30, 0);

try {
List<Circuit> circuits = ergast.getCircuits();
List<Driver> drivers = ergast.getDrivers();
List<Constructor> constructors = ergast.getConstructors();
List<Season> seasons = ergast.getSeasons();
List<Schedule> schedules = ergast.getSchedules();
List<RaceResult> results = ergast.getRaceResults(2);
List<Qualification> qualifications = ergast.getQualificationResults(2);
// ergast.getCircuits().forEach(System.out::println);
// ergast.getDrivers().forEach(System.out::println);
//
// ergast.getConstructors().forEach(System.out::println);
// ergast.getSeasons().forEach(System.out::println);
// ergast.getSchedules().forEach(System.out::println);
//
// ergast.getRaceResults(2).forEach(System.out::println);
// ergast.getQualificationResults(2).forEach(System.out::println);

// ergast.getDriverStandings(10).forEach(System.out::println);
ergast.getConstructorStandings(10).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
54 changes: 53 additions & 1 deletion core/src/main/java/com/olerom/formula/core/Ergast.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Ergast {
private final static String RESULTS_REQ = "http://ergast.com/api/{SERIES}/{SEASON}/{ROUND}/results.json";
private final static String QUALIFYING_REQ = "http://ergast.com/api/{SERIES}/{SEASON}/{ROUND}/qualifying.json";
private final static String DRIVER_STANDINGS_REQ = "http://ergast.com/api/{SERIES}/{SEASON}/{ROUND}/driverStandings.json";
private final static String CONSTRUCTOR_STANDINGS_REQ = "http://ergast.com/api/{SERIES}/{SEASON}/{ROUND}/constructorStandings.json";

private String series;
private int season;
Expand Down Expand Up @@ -134,10 +135,29 @@ public List<Qualification> getQualificationResults(int round) throws IOException
* @return list of driver standings that satisfy your query.
*/
public List<DriverStandings> getDriverStandings(int round) throws IOException {
if (this.season == -1) {
throw new SeasonException("Driver standing requires season to be mentioned");
}

String url = getUrl(DRIVER_STANDINGS_REQ);
url = getResultsUrl(url, round);
String json = getJson(url);
return parse(json, new String[]{"RaceTable", "Races", "QualifyingResults"}, DriverStandings.class);
return parse(json, new String[]{"StandingsTable", "StandingsLists", "DriverStandings"}, DriverStandings.class);
}

/**
* @param round is a round which you want to get.
* @return list of constructor standings that satisfy your query.
*/
public List<ConstructorStandings> getConstructorStandings(int round) throws IOException {
if (this.season == -1) {
throw new SeasonException("Constructor standing requires season to be mentioned");
}

String url = getUrl(CONSTRUCTOR_STANDINGS_REQ);
url = getResultsUrl(url, round);
String json = getJson(url);
return parse(json, new String[]{"StandingsTable", "StandingsLists", "ConstructorStandings"}, ConstructorStandings.class);
}

private String getResultsUrl(String url, int round) {
Expand Down Expand Up @@ -172,4 +192,36 @@ private String getJson(String url) throws IOException {

return response.toString();
}

public String getSeries() {
return series;
}

public int getSeason() {
return season;
}

public int getLimit() {
return limit;
}

public int getOffset() {
return offset;
}

public void setSeries(String series) {
this.series = series;
}

public void setSeason(int season) {
this.season = season;
}

public void setLimit(int limit) {
this.limit = limit;
}

public void setOffset(int offset) {
this.offset = offset;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@
* @author olerom
*/
public class ConstructorStandings {
private int position;
private String positionText;
private int points;
private int wins;
private Constructor constructor;

public ConstructorStandings(int position, String positionText, int points, int wins, Constructor constructor) {
this.position = position;
this.positionText = positionText;
this.points = points;
this.wins = wins;
this.constructor = constructor;
}

@Override
public String toString() {
return "ConstructorStandings{" +
"position=" + position +
", positionText='" + positionText + '\'' +
", points=" + points +
", wins=" + wins +
", constructor=" + constructor +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,24 @@ public class DriverStandings {
private Driver driver;
private List<Constructor> constructors;

public DriverStandings(int position, String positionText, int points, int wins, Driver driver, List<Constructor> constructors) {
this.position = position;
this.positionText = positionText;
this.points = points;
this.wins = wins;
this.driver = driver;
this.constructors = constructors;
}

@Override
public String toString() {
return "DriverStandings{" +
"position=" + position +
", positionText='" + positionText + '\'' +
", points=" + points +
", wins=" + wins +
", driver=" + driver +
", constructors=" + constructors +
'}';
}
}
10 changes: 4 additions & 6 deletions core/src/main/java/com/olerom/formula/core/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ public static <T> List<T> parse(String json, String[] jsonObjects, Class<T> type
entities.add(gson.fromJson(jarray.get(i).getAsJsonObject(), type));
}

for (T entitySout : entities) {
System.out.println(entitySout);
}

return entities;
}

Expand All @@ -37,7 +33,8 @@ private static JsonArray getJsonArray(String json, String[] jsonObjects, Type ty
JsonObject jobject = jelement.getAsJsonObject();
jobject = jobject.getAsJsonObject("MRData");

if (type == RaceResult.class || type == Qualification.class) {
if (type == RaceResult.class || type == Qualification.class
|| type == DriverStandings.class || type == ConstructorStandings.class) {
for (int i = 0; i < jsonObjects.length - 2; i++) {
jobject = jobject.getAsJsonObject(jsonObjects[i]);
}
Expand All @@ -62,7 +59,8 @@ private static String fixJson(String json, Type type) {
replace("\"FastestLap\"", "\"fastestLap\"").
replace("\"Q1\"", "\"q1\"").
replace("\"Q2\"", "\"q2\"").
replace("\"Q3\"", "\"q3\"");
replace("\"Q3\"", "\"q3\"").
replace("\"Constructors\"", "\"constructors\"");
}

// TODO: optimize
Expand Down

0 comments on commit b4f17d2

Please sign in to comment.