footballdata 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 306876da856af0139c78807cc9cb6613bb7ee6be
4
- data.tar.gz: 3963a026833ed2ee76d3e810bb721c10e41ee8d0
3
+ metadata.gz: cee2ff276684851524daba751370189e186547ee
4
+ data.tar.gz: f12ee272a685ab22cb6789979dd5ae9c83129e19
5
5
  SHA512:
6
- metadata.gz: fe2393e1d7d571b505a4e65947bfcd94f05558bc5ced6f7794bdd60e94cae1f3bedc20af39d94a5d0790f9025cf06d8c51418951bd92367f78599d926446ac58
7
- data.tar.gz: b33cb0af68e7ee3adf5ff1f1d0ed02d87e0c35eeebfb0e04ce3ed6abaa36f9bc95b8a4533f5a7436c50b2e1c8d72475589d2d36d145051c3d63ec32806cdc007
6
+ metadata.gz: 6acc26ccdaf3ffb7f4fb1a10d2226f906c4154ba487e94b238cf2aab1130d6704a9f4c0a338a62d5ca880c6268f76b07063ce6955ff346eae4ab3439003ac39f
7
+ data.tar.gz: 60f6b3572e57034bf49b6de1fec87cb441587b835586ec6e09056fa6c24118348e34e7f0ecd5ff51fbc40c53bd037615389ef870a98dc88efdc76bb219e526f6
data/footballdata.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'footballdata'
5
- gem.version = '0.2'
5
+ gem.version = '0.3'
6
6
  gem.date = '2016-01-27'
7
7
  gem.authors = ["Joshua Jansen"]
8
8
  gem.email = ["joshuajansen88@gmail.com"]
data/lib/footballdata.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  require "json"
2
- require "open-uri"
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.leagues(season: current_season)
17
- open("#{BASE_URL}/soccerseasons?season=#{season}", "X-Auth-Token" => api_key) do |response|
18
- JSON.parse(response.read).map do |league|
19
- League.new(
20
- id: league["id"],
21
- name: league["caption"]
22
- )
23
- end
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.team(id)
28
- open("#{BASE_URL}/teams/#{id}", "X-Auth-Token" => api_key) do |response|
29
- team = JSON.parse(response.read)
30
- id = team["_links"]["fixtures"]["href"].split("/")[-2]
31
- Team.new(
32
- id: id,
33
- name: team["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
@@ -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
- open("#{BASE_URL}/soccerseasons/#{id}/teams", "X-Auth-Token" => api_key) do |response|
12
- JSON.parse(response.read)["teams"].map do |team|
13
- id = team["_links"]["fixtures"]["href"].split("/")[-2]
14
- Footballdata::Team.new(
15
- id: id,
16
- name: team["name"]
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
- open("#{BASE_URL}/soccerseasons/#{id}/fixtures", "X-Auth-Token" => api_key) do |response|
24
- JSON.parse(response.read)["fixtures"].map do |team|
25
- Footballdata::Fixture.new(
26
- id: team["id"],
27
- soccerseason_id: team["soccerseasonId"],
28
- date: team["date"],
29
- matchday: team["matchday"],
30
- home_team_name: team["homeTeamName"],
31
- home_team_id: team["homeTeamId"],
32
- away_team_name: team["awayTeamName"],
33
- away_team_id: team["awayTeamId"],
34
- goals_home_team: team["result"]["goalsHomeTeam"],
35
- goals_away_team: team["result"]["goalsAwayTeam"]
36
- )
37
- end
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
- open("#{BASE_URL}/teams/#{id}/fixtures", "X-Auth-Token" => api_key) do |response|
12
- JSON.parse(response.read)["fixtures"].map do |team|
13
- Footballdata::Fixture.new(
14
- id: team["id"],
15
- soccerseason_id: team["soccerseasonId"],
16
- date: team["date"],
17
- matchday: team["matchday"],
18
- home_team_name: team["homeTeamName"],
19
- home_team_id: team["homeTeamId"],
20
- away_team_name: team["awayTeamName"],
21
- away_team_id: team["awayTeamId"],
22
- goals_home_team: team["result"]["goalsHomeTeam"],
23
- goals_away_team: team["result"]["goalsAwayTeam"]
24
- )
25
- end
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: footballdata
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Jansen