footballdata 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/footballdata.gemspec +1 -1
- data/lib/footballdata.rb +40 -20
- data/lib/models/fixture.rb +3 -0
- data/lib/models/league.rb +26 -26
- data/lib/models/team.rb +15 -15
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cee2ff276684851524daba751370189e186547ee
|
|
4
|
+
data.tar.gz: f12ee272a685ab22cb6789979dd5ae9c83129e19
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6acc26ccdaf3ffb7f4fb1a10d2226f906c4154ba487e94b238cf2aab1130d6704a9f4c0a338a62d5ca880c6268f76b07063ce6955ff346eae4ab3439003ac39f
|
|
7
|
+
data.tar.gz: 60f6b3572e57034bf49b6de1fec87cb441587b835586ec6e09056fa6c24118348e34e7f0ecd5ff51fbc40c53bd037615389ef870a98dc88efdc76bb219e526f6
|
data/footballdata.gemspec
CHANGED
data/lib/footballdata.rb
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
require "json"
|
|
2
|
-
require
|
|
2
|
+
require 'net/http'
|
|
3
3
|
|
|
4
4
|
require 'models/fixture'
|
|
5
5
|
require 'models/team'
|
|
@@ -8,33 +8,53 @@ require 'configuration'
|
|
|
8
8
|
|
|
9
9
|
BASE_URL = "http://api.football-data.org/v1"
|
|
10
10
|
|
|
11
|
-
def api_key
|
|
12
|
-
Footballdata.configuration.api_key
|
|
13
|
-
end
|
|
14
|
-
|
|
15
11
|
module Footballdata
|
|
16
|
-
def self.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
12
|
+
def self.api_key
|
|
13
|
+
Footballdata.configuration.api_key
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.api_request(path)
|
|
17
|
+
uri = URI([BASE_URL, path].join)
|
|
18
|
+
req = Net::HTTP::Get.new(uri)
|
|
19
|
+
req['X-Auth-Token'] = api_key
|
|
20
|
+
|
|
21
|
+
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
|
|
22
|
+
http.request(req)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
case response
|
|
26
|
+
when Net::HTTPSuccess then
|
|
27
|
+
response
|
|
28
|
+
when Net::HTTPTooManyRequests then
|
|
29
|
+
raise RuntimeError, "API limit reached"
|
|
30
|
+
else
|
|
31
|
+
response
|
|
24
32
|
end
|
|
25
33
|
end
|
|
26
34
|
|
|
27
|
-
def self.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
id: id,
|
|
33
|
-
name:
|
|
35
|
+
def self.leagues(season: current_season)
|
|
36
|
+
response = Footballdata::api_request("/soccerseasons?season=#{season}")
|
|
37
|
+
|
|
38
|
+
JSON.parse(response.body).map do |league|
|
|
39
|
+
League.new(
|
|
40
|
+
id: league["id"],
|
|
41
|
+
name: league["caption"],
|
|
42
|
+
number_of_teams: league["numberOfTeams"]
|
|
34
43
|
)
|
|
35
44
|
end
|
|
36
45
|
end
|
|
37
46
|
|
|
47
|
+
def self.team(id)
|
|
48
|
+
response = Footballdata::api_request("/teams/#{id}")
|
|
49
|
+
|
|
50
|
+
team = JSON.parse(response.body)
|
|
51
|
+
id = team["_links"]["fixtures"]["href"].split("/")[-2]
|
|
52
|
+
Team.new(
|
|
53
|
+
id: id,
|
|
54
|
+
name: team["name"]
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
38
58
|
def self.current_season
|
|
39
59
|
Time.now.month >= 7 ? Time.now.year : Time.now.year - 1
|
|
40
60
|
end
|
data/lib/models/fixture.rb
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
module Footballdata
|
|
2
2
|
class Fixture
|
|
3
|
+
attr_reader :id, :soccerseason_id, :date, :matchday, :home_team_name, :home_team_id,
|
|
4
|
+
:away_team_name, :away_team_id, :goals_home_team, :goals_away_team
|
|
5
|
+
|
|
3
6
|
def initialize(id:, soccerseason_id:, date:, matchday:, home_team_name:, home_team_id:,
|
|
4
7
|
away_team_name:, away_team_id:, goals_home_team:, goals_away_team:)
|
|
5
8
|
@id = id
|
data/lib/models/league.rb
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
1
|
module Footballdata
|
|
2
2
|
class League
|
|
3
|
-
attr_reader :id, :name
|
|
3
|
+
attr_reader :id, :name, :number_of_teams
|
|
4
4
|
|
|
5
|
-
def initialize(id:, name:)
|
|
5
|
+
def initialize(id:, name:, number_of_teams:)
|
|
6
6
|
@id = id
|
|
7
7
|
@name = name
|
|
8
|
+
@number_of_teams = number_of_teams
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
def teams
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
end
|
|
12
|
+
response = Footballdata::api_request("/soccerseasons/#{id}/teams")
|
|
13
|
+
JSON.parse(response.body)["teams"].map do |team|
|
|
14
|
+
id = team["_links"]["fixtures"]["href"].split("/")[-2]
|
|
15
|
+
Footballdata::Team.new(
|
|
16
|
+
id: id,
|
|
17
|
+
name: team["name"]
|
|
18
|
+
)
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
|
|
22
|
-
def fixtures
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
22
|
+
def fixtures(timeframe: "n#{self.number_of_teams}")
|
|
23
|
+
response = Footballdata::api_request("/soccerseasons/#{id}/fixtures?timeFrame=#{timeframe}")
|
|
24
|
+
|
|
25
|
+
JSON.parse(response.body)["fixtures"].map do |team|
|
|
26
|
+
Footballdata::Fixture.new(
|
|
27
|
+
id: team["id"],
|
|
28
|
+
soccerseason_id: team["soccerseasonId"],
|
|
29
|
+
date: team["date"],
|
|
30
|
+
matchday: team["matchday"],
|
|
31
|
+
home_team_name: team["homeTeamName"],
|
|
32
|
+
home_team_id: team["homeTeamId"],
|
|
33
|
+
away_team_name: team["awayTeamName"],
|
|
34
|
+
away_team_id: team["awayTeamId"],
|
|
35
|
+
goals_home_team: team["result"]["goalsHomeTeam"],
|
|
36
|
+
goals_away_team: team["result"]["goalsAwayTeam"]
|
|
37
|
+
)
|
|
38
38
|
end
|
|
39
39
|
end
|
|
40
40
|
end
|
data/lib/models/team.rb
CHANGED
|
@@ -8,21 +8,21 @@ module Footballdata
|
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def fixtures
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
11
|
+
response = Footballdata::api_request("/teams/#{id}/fixtures")
|
|
12
|
+
|
|
13
|
+
JSON.parse(response.body)["fixtures"].map do |team|
|
|
14
|
+
Footballdata::Fixture.new(
|
|
15
|
+
id: team["id"],
|
|
16
|
+
soccerseason_id: team["soccerseasonId"],
|
|
17
|
+
date: team["date"],
|
|
18
|
+
matchday: team["matchday"],
|
|
19
|
+
home_team_name: team["homeTeamName"],
|
|
20
|
+
home_team_id: team["homeTeamId"],
|
|
21
|
+
away_team_name: team["awayTeamName"],
|
|
22
|
+
away_team_id: team["awayTeamId"],
|
|
23
|
+
goals_home_team: team["result"]["goalsHomeTeam"],
|
|
24
|
+
goals_away_team: team["result"]["goalsAwayTeam"]
|
|
25
|
+
)
|
|
26
26
|
end
|
|
27
27
|
end
|
|
28
28
|
end
|