football_data 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e8bbe4b1bd4a6641d8ff67525542f49f5a75f617
4
+ data.tar.gz: 592420d5665dd0bd8a33214880f715ed79d159d5
5
+ SHA512:
6
+ metadata.gz: 5230e13746916e6a9b126e2214f8178d08d18dae3ed67259cc195c29fe4133e9d2860dc0a06421ccfb995ef7c9c08a3681210114491e0d9969de96cf7dbaa9f1
7
+ data.tar.gz: 8fbf36da3b2f8707c61ae44fcc38fcc0861924baa33301ecbed81cf498fc5d681f843eeec2de39a5edfbdca057b735019758714f7b49c1705e3c1668c4f22b28
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in football_data.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # FootballData
2
+
3
+ This gem intends to provide easy access to the REST API provided by [Football-Data](http://api.football-data.org)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'football_data'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install football_data
20
+
21
+ ## Usage
22
+
23
+ According to the API documentation, you can retrive the following model:
24
+
25
+ <image>
26
+
27
+ ```ruby
28
+ FootballData.all_seasons
29
+ ```
30
+ This method returns all the current seasons. A season is made of teams, their
31
+ fixtures and the respective league table.
32
+
33
+
34
+ TBA
35
+
36
+ ## Development
37
+
38
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake false` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
39
+
40
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "football_data"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'football_data/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "football_data"
8
+ spec.version = FootballData::VERSION
9
+ spec.authors = ["PedroGabriel"]
10
+ spec.email = ["pedrogabriel@protonmail.ch"]
11
+
12
+ spec.summary = %q{Quick access to the Football-Data API}
13
+ spec.description = %q{This gem intends to provide easy access to the REST API provided by Football-Data}
14
+ spec.homepage = "https://bitbucket.org/PedroGabriel/footballdatagem"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.10"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "json", '~> 1.8', '>= 1.8.3'
24
+ spec.add_development_dependency "rspec", '~> 3.3'
25
+ spec.add_development_dependency "rest-client", '~> 1.8'
26
+ end
@@ -0,0 +1,49 @@
1
+
2
+ require "json"
3
+ require "rest-client"
4
+
5
+ module FootballData
6
+
7
+ private
8
+
9
+ @@api_key
10
+
11
+ public
12
+
13
+ def self.set_api_key(api_key)
14
+ @@api_key = api_key
15
+ end
16
+
17
+ ##
18
+ #
19
+ #
20
+ def self.all_leagues
21
+ uri = 'http://api.football-data.org/v1/soccerseasons/'
22
+ leagues = []
23
+ begin
24
+ json = Utils::request uri
25
+ json.each do |league|
26
+ leagues.push League.new(league)
27
+ end
28
+ return leagues
29
+ rescue
30
+ return leagues
31
+ end
32
+ end
33
+
34
+ ##
35
+ # Each season is based upon a certain league.
36
+ # In order to select a certain season we must provide the league's
37
+ # name in order to select the most recent season.
38
+ #
39
+ # @param name_or_id League's name
40
+ #
41
+ def self.get_league(name_or_id)
42
+ return nil if name_or_id.nil?
43
+ leagues = self.all_leagues
44
+ leagues.each do |league|
45
+ return league if league.has_eql_name(name_or_id) or league.has_eql_id(name_or_id)
46
+ end
47
+ nil
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ require_relative './link'
2
+ class Fixture
3
+
4
+ attr_reader :date, :status, :match_day, :home_team_name, :away_team_name,
5
+ :result, :links, :id
6
+
7
+ def initialize(fixture_hash)
8
+ @date = fixture_hash['date']
9
+ @status = fixture_hash['status']
10
+ @match_day = fixture_hash['matchday']
11
+ @home_team_name = fixture_hash['homeTeamName']
12
+ @away_team_name = fixture_hash['awayTeamName']
13
+ @result = fixture_hash['result'] #gives an hash as well
14
+ @links = Link.new(fixture_hash['_links'])
15
+ @id = @links.self.split('/').pop
16
+ end
17
+
18
+ ##
19
+ # Gives back the amount of goals related
20
+ # to the home team.
21
+ #
22
+ def goals_home_team
23
+ @result['goalsHomeTeam']
24
+ end
25
+
26
+ ##
27
+ # Gives back the amount of goals related
28
+ # to the away team.
29
+ #
30
+ def goals_away_team
31
+ @result['goalsAwayTeam']
32
+ end
33
+
34
+ ##
35
+ # Printable version of Fixture instance.
36
+ #
37
+ def to_s
38
+ "(#{@id}) - Match Day #{@match_day} #{@home_team_name} vs #{@away_team_name} - #{@status}"
39
+ end
40
+
41
+ end
@@ -0,0 +1,120 @@
1
+ require_relative '../football_data/link'
2
+
3
+ require_relative '../football_data'
4
+ require_relative './utils'
5
+ extend Utils
6
+
7
+ require_relative './team'
8
+ require_relative './standing'
9
+ require 'rest-client'
10
+
11
+ ##
12
+ # DTO Class that represents the concept of a league.
13
+ #
14
+ class League
15
+ attr_reader :caption, :league, :year, :number_of_teams, :number_of_games,
16
+ :links, :id
17
+
18
+
19
+ def initialize(season_hash)
20
+ raise ArgumentError.new('JSON object is invalid') if season_hash.nil?
21
+
22
+ @caption = season_hash['caption'] || nil #TODO rename
23
+ @league = season_hash['league'] || nil #TODO rename
24
+ @year = season_hash['year'] || nil
25
+ @number_of_teams = season_hash['numberOfTeams'] || nil
26
+ @number_of_games = season_hash['numberOfGames'] || nil
27
+ @links = Link.new(season_hash['_links'])
28
+ @id = @links.self.split('/').pop
29
+
30
+ @teams = []
31
+ end
32
+
33
+ ##
34
+ # Returns an array of Team that represents all the teams inside a certain
35
+ # season. It uses an internal "buffer" that gets returned if any other call
36
+ # to this method was made previously. This may happen since for an entire
37
+ # season the set of teams is constant.
38
+ #
39
+ def get_teams
40
+ return @teams unless @teams.empty?
41
+ season_hash_teams = Utils.request(@links.teams)
42
+ season_hash_teams['teams'].each do |team|
43
+ @teams.push Team.new(team)
44
+ end
45
+ @teams
46
+ end
47
+
48
+ ##
49
+ #
50
+ #
51
+ def get_team(name)
52
+ get_teams.each { |team| return team if team.name.eql?(name) or team.short_name.eql?(name) }
53
+ nil
54
+ end
55
+
56
+ ##
57
+ # Printable version of League instance.
58
+ #
59
+ def to_s
60
+ "(#{@id})#{@league} - #{@caption} - #{@year}"
61
+ end
62
+
63
+ ##
64
+ # Verifies if the target team has the same name as
65
+ # the parameter. You can pass the name or the short name.
66
+ #
67
+ def has_eql_name(name)
68
+ @caption.eql? name or @league.eql? name
69
+ end
70
+
71
+ ##
72
+ # Verifies if the target team has the same id
73
+ # as the id passed as parameter.
74
+ #
75
+ def has_eql_id(id)
76
+ @id.eql? id
77
+ end
78
+
79
+ ##
80
+ # Returns an array with instances of Fixture. It does not use an internal
81
+ # array since the API does not hold all the fixtures since the beginning of the
82
+ # season. It keep adding as time goes by which makes every call return a new
83
+ # value every time.
84
+ #
85
+ def all_fixtures
86
+ fixtures = []
87
+ league_hash_fixtures = Utils.request(@links.fixtures)
88
+ league_hash_fixtures['fixtures'].each do |fix|
89
+ fixtures.push Fixture.new(fix)
90
+ end
91
+ fixtures
92
+ end
93
+
94
+ ##
95
+ # It tries to return a given matchday. Instead of returning all the fixtures,
96
+ # it filters and returns only one if the parameter equals to the id present
97
+ # in any of the matchdays.
98
+ def get_matchday(matchday_number)
99
+ throw ArgumentError if matchday_number < 1
100
+ fixtures_from_matchday = []
101
+ self.all_fixtures.each do |matchday|
102
+ fixtures_from_matchday.push matchday if matchday.match_day == matchday_number
103
+ end
104
+ fixtures_from_matchday
105
+ end
106
+
107
+ ##
108
+ # Tries to acquire the resource given by the the URI "leaguetable"
109
+ # present in the main resource (all seasons).
110
+ # It does not use a buffer mechanism since after every week all the
111
+ # league tables might change according to the results of each fixture.
112
+ def get_leaguetable
113
+ leaguetable = []
114
+ league_hash_leaguetable = Utils.request(@links.league_table)
115
+ league_hash_leaguetable['standing'].each do |standing_hash|
116
+ leaguetable.push Standing.new(standing_hash)
117
+ end
118
+ leaguetable
119
+ end
120
+ end
@@ -0,0 +1,17 @@
1
+
2
+ class Link
3
+
4
+ attr_reader :self, :teams, :fixtures, :league_table
5
+ attr_reader :team
6
+
7
+ def initialize(link_hash)
8
+ raise ArgumentError.new('JSON object is invalid') if link_hash.nil?
9
+
10
+ @self = link_hash['self']['href'] unless link_hash['self'].nil?
11
+ @team = link_hash['team']['href'] unless link_hash['team'].nil?
12
+ @teams = link_hash['teams']['href'] unless link_hash['teams'].nil?
13
+ @fixtures = link_hash['fixtures']['href'] unless link_hash['fixtures'].nil?
14
+ @league_table = link_hash['leagueTable']['href'] unless link_hash['leagueTable'].nil?
15
+ end
16
+
17
+ end
@@ -0,0 +1,25 @@
1
+
2
+ class Standing
3
+
4
+ attr_reader :links, :position, :team_name, :points, :played_games
5
+ attr_reader :goals, :goals_against, :goal_difference, :wins, :draws, :losses
6
+
7
+ def initialize(standing_hash)
8
+ @links = Link.new(standing_hash['_links'])
9
+ @position = standing_hash['position']
10
+ @team_name = standing_hash['teamName']
11
+ @played_games = standing_hash['playedGames']
12
+ @points = standing_hash['points']
13
+ @goals = standing_hash['goals']
14
+ @goals_against = standing_hash['goalsAgainst']
15
+ @goal_difference = standing_hash['goalDifference']
16
+ @wins = standing_hash['wins']
17
+ @draws = standing_hash['draws']
18
+ @losses = standing_hash['losses']
19
+ end
20
+
21
+ def to_s
22
+ "#{@position} - #{@team_name} - #{@points} Points"
23
+ end
24
+
25
+ end
@@ -0,0 +1,22 @@
1
+ require_relative './link'
2
+
3
+ class Team
4
+ attr_reader :name, :market_value, :code, :short_name, :crest_url, :links, :id
5
+
6
+ def initialize(team_hash)
7
+ @name = team_hash['name']
8
+ @code = team_hash['code']
9
+ @short_name = team_hash['shortName']
10
+ @crest_url = team_hash['crestUrl']
11
+ @market_value = team_hash['squadMarketValue']
12
+ @links = Link.new(team_hash['_links'])
13
+ @id = @links.self.split('/').pop
14
+ end
15
+
16
+
17
+ def to_s
18
+ rep = "#{@name}"
19
+ rep = " - (#{@code}) " unless @code.nil?
20
+ rep + " - #{@short_name}"
21
+ end
22
+ end
@@ -0,0 +1,12 @@
1
+ module Utils
2
+ def self.request(uri)
3
+ begin
4
+ api_key = FootballData.class_variable_get(:@@api_key)
5
+ response = RestClient.get uri, {'x-auth-token' => api_key}
6
+ return JSON.parse(response.body) if response.code == 200
7
+ nil
8
+ rescue
9
+ return nil
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module FootballData
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: football_data
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - PedroGabriel
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.8'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 1.8.3
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '1.8'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.8.3
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.3'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.3'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rest-client
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '1.8'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '1.8'
89
+ description: This gem intends to provide easy access to the REST API provided by Football-Data
90
+ email:
91
+ - pedrogabriel@protonmail.ch
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - ".gitignore"
97
+ - ".travis.yml"
98
+ - CODE_OF_CONDUCT.md
99
+ - Gemfile
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - football_data.gemspec
105
+ - lib/football_data.rb
106
+ - lib/football_data/fixture.rb
107
+ - lib/football_data/league.rb
108
+ - lib/football_data/link.rb
109
+ - lib/football_data/standing.rb
110
+ - lib/football_data/team.rb
111
+ - lib/football_data/utils.rb
112
+ - lib/football_data/version.rb
113
+ homepage: https://bitbucket.org/PedroGabriel/footballdatagem
114
+ licenses: []
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.5.1
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Quick access to the Football-Data API
136
+ test_files: []