import axios from 'axios' ; import { pick } from 'lodash' ; import * as moment from 'moment' ; import { WORLDCUP_2018_ID } from '../config' ; import { Competition , Fixture , IFixtureJson , IStandingJson , Standing , Status } from '../models' ; const FOOTBALL_DATA_API_URL = 'https://api.football-data.org/v1' ; const COMPETITION_ENDPOINT = `/competitions/${ WORLDCUP_2018_ID }` ; const LEAGUE_TABLE_ENDPOINT = `/competitions/${ WORLDCUP_2018_ID }/leagueTable` ; const FIXTURES_ENDPOINT = `/competitions/${ WORLDCUP_2018_ID }/fixtures` ; export default { async getCompetition ( ) { const { data } = await axios . get ( `${ FOOTBALL_DATA_API_URL }${ COMPETITION_ENDPOINT }` ) ; return pick ( data , [ 'caption' , 'league' , 'year' , 'numberOfTeams' , 'numberOfGames' ] ) ; } , async getAllStandings ( ) { const { data : { standings : standingsJson } } = await axios . get ( `${ FOOTBALL_DATA_API_URL }${ LEAGUE_TABLE_ENDPOINT }` ) ; const result = Object . keys ( standingsJson ) . reduce ( ( acc , table ) => { const standings = standingsJson [ table ] . map ( ( standing ) => new Standing ( standing ) ) ; acc . set ( table , standings ) ; return acc ; } , new Map ( ) ) ; return result ; } , async getStandings ( tableName ) { const normalizedTableName = tableName . toUpperCase ( ) ; const standings = await this . getAllStandings ( ) ; const result = standings . get ( normalizedTableName ) || [ ] ; return result ; } , async getFixtures ( ) { const { data : { fixtures : fixturesJson } } = await axios . get ( `${ FOOTBALL_DATA_API_URL }${ FIXTURES_ENDPOINT }` ) ; const fixtures = fixturesJson . map ( ( fixture ) => new Fixture ( fixture ) ) ; return fixtures ; } , async getNextRecentFixtures ( ) { const fixtures = await this . getFixtures ( ) ; const result = fixtures . filter ( ( fixture ) => moment ( ) . isBefore ( fixture . date ) && moment ( ) . add ( 24 , 'hour' ) . isAfter ( fixture . date ) ) ; return result ; } , async getLastRecentFixtures ( ) { const fixtures = await this . getFixtures ( ) ; const result = fixtures . filter ( ( fixture ) => moment ( ) . isAfter ( fixture . date ) && moment ( ) . add ( - 24 , 'hour' ) . isBefore ( fixture . date ) ) ; return result ; } , async getPlayingFixtures ( ) { const fixtures = await this . getFixtures ( ) ; const result = fixtures . filter ( ( fixture ) => fixture . status === Status . Playing ) ; return result || [ ] ; } } ;