Skip to content

Instantly share code, notes, and snippets.

@pcon
Created July 10, 2018 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcon/4f65c2e8f3f128957744264189436c8f to your computer and use it in GitHub Desktop.
Save pcon/4f65c2e8f3f128957744264189436c8f to your computer and use it in GitHub Desktop.
public class FootballAPI {
public class GameWeather {
public String humidity {get;set;}
public String temp_celsius {get;set;}
public String temp_farenheit {get;set;}
public String wind_speed {get;set;}
public String description {get;set;}
}
public class GameTeam {
public String country {get;set;}
public String code {get;set;}
public Integer goals {get;set;}
public Integer penalties {get;set;}
}
public class GameEvent {
public String id {get;set;}
public String type_of_event {get;set;}
public String player {get;set;}
public String x_time {get;set;}
}
public class GamePlayer {
public String name {get;set;}
public Boolean captain {get;set;}
public Integer shirt_number {get;set;}
public String position {get;set;}
}
public class GameTeamStat {
public String country {get;set;}
public Integer attempts_on_goal {get;set;}
public Integer on_target {get;set;}
public Integer off_target {get;set;}
public Integer blocked {get;set;}
public Integer woodwork {get;set;}
public Integer corners {get;set;}
public Integer offsides {get;set;}
public Integer ball_possession {get;set;}
public Integer pass_accuracy {get;set;}
public Integer num_passes {get;set;}
public Integer passes_completed {get;set;}
public Integer distance_covered {get;set;}
public Integer balls_recovered {get;set;}
public Integer tackles {get;set;}
public Integer clearances {get;set;}
public Integer yellow_cards {get;set;}
public Integer red_cards {get;set;}
public Integer fouls_committed {get;set;}
public String tactics {get;set;}
public List<GamePlayer> starting_eleven {get;set;}
}
public class GameInfo {
public String venue {get;set;}
public String location {get;set;}
public String status {get;set;}
public String time_x {get;set;}
public String fifa_id {get;set;}
public GameWeather weather {get;set;}
public String attendance {get;set;}
public List<String> officials {get;set;}
public String stage_name {get;set;}
public String home_team_country {get;set;}
public String away_team_country {get;set;}
public String datetime_x {get;set;}
public String winner {get;set;}
public String winner_code {get;set;}
public GameTeam home_team {get;set;}
public GameTeam away_team {get;set;}
public List<GameEvent> home_team_events {get;set;}
public List<GameEvent> away_team_events {get;set;}
public GameTeamStat home_team_statistics {get;set;}
public GameTeamStat away_team_statistics {get;set;}
public String last_event_update_at {get;set;}
public String last_score_update_at {get;set;}
}
public class GameWrapper {
public List<GameInfo> games {get;set;}
}
private static String mogrifyJSON(String data) {
String regexFormat = '(?m)^\\s*"{0}"\\s*:';
String replacementFormat = '"{0}" :';
Map<String, String> replacements = new Map<String, String> {
'time' => 'time_x',
'datetime' => 'datetime_x'
};
String formattedJSON = JSON.serializePretty(JSON.deserializeUntyped(data));
for (String key : replacements.keySet()) {
String regex = String.format(regexFormat, new List<String> {key});
String replacement = String.format(replacementFormat, new List<String> {replacements.get(key)});
formattedJSON = formattedJSON.replaceAll(regex, replacement);
}
return formattedJSON;
}
private static Datetime parseDatetime(String datetime_x) {
return Datetime.now();
}
private static String getDayOfWeek(Datetime dt) {
String day = dt.format('E');
switch on dt.format('E').toLowerCase() {
when 'mon' {
day = 'Monday';
}
when 'tue' {
day = 'Tuesday';
}
when 'wed' {
day = 'Wednesday';
}
when 'thu' {
day = 'Thursday';
}
when 'fri' {
day = 'Friday';
}
when 'sat' {
day = 'Saturday';
}
when 'sun' {
day = 'Sunday';
}
}
return day;
}
public static String getGameStatus(String fifa_code) {
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://worldcup.sfg.io/matches/country?fifa_code=' + fifa_code + '&by_date=desc');
req.setMethod('GET');
HttpResponse res = h.send(req);
System.debug(System.LoggingLevel.ERROR, res.getBody());
if (res.getStatusCode() != 200) {
return res.getStatus();
}
GameWrapper wrapper = (GameWrapper) JSON.deserialize(mogrifyJSON('{"games":' + res.getBody() + '}'), GameWrapper.class);
for (GameInfo game : wrapper.games) {
GameTeam requestedTeam = (game.home_team.code == fifa_code) ? game.home_team : game.away_team;
GameTeam otherTeam = (game.home_team.code == fifa_code) ? game.away_team : game.home_team;
Datetime gameDate = parseDatetime(game.datetime_x);
if (game.status.equalsIgnoreCase('future')) {
return 'Hope ' + requestedTeam.country + ' beats ' + otherTeam.country + ' on ' + getDayOfWeek(gameDate);
}
if (requestedTeam.goals == otherTeam.goals) {
return requestedTeam.country + ' is all tied up with ' + otherTeam.country;
}
if (requestedTeam.goals < otherTeam.goals) {
String status = 'Can\'t believe ' + otherTeam.country + ' is winning! \n';
status += requestedTeam.country + ': ' + requestedTeam.goals + ' - ' + otherTeam.country + ': ' + otherTeam.goals;
status += '\nCome on ' + requestedTeam.country + '!';
return status;
}
String status = 'Glad to see ' + otherTeam.country + ' is winning.\n';
status += requestedTeam.country + ': ' + requestedTeam.goals + ' - ' + otherTeam.country + ': ' + otherTeam.goals;
return status;
}
return 'Could not find game.';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment