module Footballdata

Public Class Methods

api_key() click to toggle source
# File lib/footballdata.rb, line 12
def self.api_key
  Footballdata.configuration.api_key
end
api_request(path) click to toggle source
# File lib/footballdata.rb, line 16
def self.api_request(path)
  uri = URI([BASE_URL, path].join)
  req = Net::HTTP::Get.new(uri)
  req['X-Auth-Token'] = api_key

  response = Net::HTTP.start(uri.hostname, uri.port) do |http|
    http.request(req)
  end

  case response
  when Net::HTTPSuccess then
    response
  when Net::HTTPTooManyRequests then
    raise RuntimeError, "API limit reached"
  else
    response
  end
end
configuration() click to toggle source
# File lib/configuration.rb, line 10
def self.configuration
  @configuration ||= Configuration.new
end
configuration=(config) click to toggle source
# File lib/configuration.rb, line 14
def self.configuration=(config)
  @configuration = config
end
configure() { |configuration| ... } click to toggle source
# File lib/configuration.rb, line 18
def self.configure
  yield configuration
end
current_season() click to toggle source
# File lib/footballdata.rb, line 59
def self.current_season
  Time.now.month >= 7 ? Time.now.year : Time.now.year - 1
end
leagues(season: current_season) click to toggle source
# File lib/footballdata.rb, line 35
def self.leagues(season: current_season)
  response = Footballdata::api_request("/soccerseasons?season=#{season}")

  JSON.parse(response.body).map do |league|
    League.new(
      id: league["id"],
      name: league["caption"],
      number_of_teams: league["numberOfTeams"]
    )
  end
end
team(id) click to toggle source
# File lib/footballdata.rb, line 47
def self.team(id)
  response = Footballdata::api_request("/teams/#{id}")

  team = JSON.parse(response.body)
  id = team["_links"]["fixtures"]["href"].split("/")[-2]
  Team.new(
    id: id,
    name: team["name"],
    logo_url: team["crestUrl"],
  )
end