Class: Footballdata::Metal

Inherits:
Object
  • Object
show all
Defined in:
lib/footballdata/download.rb

Overview

plumbing metal “helpers”

Constant Summary collapse

BASE_URL =
'http://api.football-data.org/v4'

Class Method Summary collapse

Class Method Details

.areasObject



126
127
128
# File 'lib/footballdata/download.rb', line 126

def self.areas
   get( "#{BASE_URL}/areas" )
end

.competition(code) ⇒ Object

more



110
111
112
# File 'lib/footballdata/download.rb', line 110

def self.competition( code )
  get( "#{BASE_URL}/competitions/#{code}" )
end

.competition_matches_url(code, year) ⇒ Object

just use matches_url - why? why not?



83
# File 'lib/footballdata/download.rb', line 83

def self.competition_matches_url( code, year )   "#{BASE_URL}/competitions/#{code}/matches?season=#{year}";   end

.competition_scorers_url(code, year) ⇒ Object



86
# File 'lib/footballdata/download.rb', line 86

def self.competition_scorers_url( code, year )   "#{BASE_URL}/competitions/#{code}/scorers?season=#{year}"; end

.competition_standings_url(code, year) ⇒ Object



85
# File 'lib/footballdata/download.rb', line 85

def self.competition_standings_url( code, year ) "#{BASE_URL}/competitions/#{code}/standings?season=#{year}"; end

.competition_teams_url(code, year) ⇒ Object



84
# File 'lib/footballdata/download.rb', line 84

def self.competition_teams_url( code, year )     "#{BASE_URL}/competitions/#{code}/teams?season=#{year}";     end

.competitions(auth: false) ⇒ Object



77
78
79
# File 'lib/footballdata/download.rb', line 77

def self.competitions( auth: false )
  get( competitions_url, auth: auth )
end

.competitions_urlObject



73
74
75
# File 'lib/footballdata/download.rb', line 73

def self.competitions_url
  "#{BASE_URL}/competitions"
end

.get(url, auth: true, headers: {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/footballdata/download.rb', line 41

def self.get( url,
                auth:    true,
                headers: {} )

  token = ENV['FOOTBALLDATA']
  ## note: because of public workflow log - do NOT output token
  ## puts token

  request_headers = {}
  request_headers['X-Auth-Token'] = token    if auth && token
  request_headers['User-Agent']   = 'ruby'
  request_headers['Accept']       = '*/*'

  request_headers = request_headers.merge( headers )   unless headers.empty?


  ## note: add format: 'json' for pretty printing json (before) save in cache
  response = Webget.call( url, headers: request_headers )

  ## for debugging print pretty printed json first 400 chars
  puts response.json.pretty_inspect[0..400]

  exit 1  if response.status.nok?   # e.g. HTTP status code != 200

  response.json
end

.match(id) ⇒ Object



118
119
120
# File 'lib/footballdata/download.rb', line 118

def self.match( id )
  get( "#{BASE_URL}/matches/#{id}" )
end

.matches(code, year, headers: {}) ⇒ Object



88
89
90
91
92
# File 'lib/footballdata/download.rb', line 88

def self.matches( code, year,
                  headers: {} )
    get( competition_matches_url( code, year ),
         headers: headers )
end

.person(id) ⇒ Object



122
123
124
# File 'lib/footballdata/download.rb', line 122

def self.person( id )
 get( "#{BASE_URL}/persons/#{id}" )
end

.scorers(code, year) ⇒ Object



106
# File 'lib/footballdata/download.rb', line 106

def self.scorers( code, year ) get( competition_scorers_url( code, year )); end

.standings(code, year) ⇒ Object



105
# File 'lib/footballdata/download.rb', line 105

def self.standings( code, year ) get( competition_standings_url( code, year )); end

.team(id) ⇒ Object



114
115
116
# File 'lib/footballdata/download.rb', line 114

def self.team( id )
  get( "#{BASE_URL}/teams/#{id}" )
end

.teams(code, year) ⇒ Object



104
# File 'lib/footballdata/download.rb', line 104

def self.teams( code, year )    get( competition_teams_url( code, year )); end

.todays_matches(date = Date.today) ⇒ Object

use/rename to matches_today or such - why? why not?



99
100
101
# File 'lib/footballdata/download.rb', line 99

def self.todays_matches( date=Date.today )    ## use/rename to matches_today or such - why? why not?
    get( todays_matches_url( date ) )
end

.todays_matches_url(date = Date.today) ⇒ Object



94
95
96
97
98
# File 'lib/footballdata/download.rb', line 94

def self.todays_matches_url( date=Date.today )
  "#{BASE_URL}/matches?"+
  "dateFrom=#{date.strftime('%Y-%m-%d')}&"+
  "dateTo=#{(date+1).strftime('%Y-%m-%d')}"
end