football_cli 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'football_cli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'football_cli'
8
+ spec.version = FootballCli::VERSION
9
+ spec.platform = Gem::Platform::RUBY
10
+ spec.author = ['Said Kaldybaev']
11
+ spec.email = ['said.kaldybaev@gmail.com']
12
+
13
+ spec.summary = %q{A command line interface}
14
+ spec.description = %q{A command line interface for all the football data feeds in Ruby}
15
+ spec.homepage = 'https://github.com/Saidbek/football_cli'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'bin'
22
+ spec.executables << 'football_cli'
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_dependency('football_ruby', '~> 0')
26
+ spec.add_dependency('rainbow', '~> 0')
27
+ spec.add_development_dependency('rake', '~> 0')
28
+ spec.add_development_dependency('aruba', '~> 0')
29
+ spec.add_development_dependency('terminal-table', '~> 0')
30
+ spec.add_runtime_dependency('gli','2.16.0', '~> 0')
31
+ end
@@ -0,0 +1,53 @@
1
+ module FootballCli
2
+ module Format
3
+ class Base
4
+ attr_reader :title, :response, :columns, :rows, :file_name
5
+
6
+ def initialize(opts={})
7
+ @response = opts[:response]
8
+ @title = opts[:title]
9
+ @columns = opts[:columns]
10
+ @file_name = opts[:file_name]
11
+ @qualification = opts[:qualification]
12
+
13
+ @rows = []
14
+ end
15
+
16
+ def generate
17
+ raise NotImplementedError, 'must implement generate() method in subclass'
18
+ end
19
+
20
+ def output
21
+ raise NotImplementedError, 'must implement output() method in subclass'
22
+ end
23
+
24
+ def write_to_file(output)
25
+ File.write(file_name, output) if file_name
26
+ end
27
+
28
+ def goal_columns
29
+ %i(goalsHomeTeam goalsAwayTeam).freeze
30
+ end
31
+
32
+ def pretty_table(data, column)
33
+ if qualification
34
+ color = case data[:position]
35
+ when qualification[:cl] then :green
36
+ when qualification[:el] then :yellow
37
+ when qualification[:rl] then :red
38
+ else
39
+ :aqua
40
+ end
41
+
42
+ data[column].to_s.color(color)
43
+ else
44
+ data[column]
45
+ end
46
+ end
47
+
48
+ def qualification
49
+ Hash[@qualification.collect {|k, v| [ k, v.each_cons(2).map {|from, to| from..to }.max ] }] if @qualification
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,30 @@
1
+ require 'csv'
2
+ require_relative 'base'
3
+
4
+ module FootballCli
5
+ module Format
6
+ class Csv < Base
7
+ def generate
8
+ @generate ||= CSV.generate do |csv|
9
+ csv << columns
10
+
11
+ response.each do |data|
12
+ csv << columns.collect {|c|
13
+ if goal_columns.include?(c) && data[:result]
14
+ data[:result][c]
15
+ else
16
+ data[c]
17
+ end
18
+ }
19
+ end
20
+ end
21
+ end
22
+
23
+ def output
24
+ puts generate
25
+
26
+ write_to_file(generate)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,19 @@
1
+ require_relative 'table'
2
+ require_relative 'json'
3
+ require_relative 'csv'
4
+
5
+ module FootballCli
6
+ module Format
7
+ class FormatFactory
8
+ def self.build(format, *args)
9
+ case format
10
+ when 'table' then Table.new(*args)
11
+ when 'json' then Json.new(*args)
12
+ when 'csv' then Csv.new(*args)
13
+ else
14
+ raise 'Invalid format type. Available options (table, json, csv)'
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'json'
2
+ require_relative 'base'
3
+
4
+ module FootballCli
5
+ module Format
6
+ class Json < Base
7
+ def generate
8
+ response.each do |data|
9
+ rows.push(
10
+ Hash[columns.collect {|c|
11
+ if goal_columns.include?(c)
12
+ [c, data[:result][c]]
13
+ else
14
+ [c, data[c]]
15
+ end
16
+ }]
17
+ )
18
+ end
19
+
20
+ @generate ||= JSON.pretty_generate(rows)
21
+ end
22
+
23
+ def output
24
+ puts generate
25
+
26
+ write_to_file(generate)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ require 'rainbow/ext/string'
2
+ require_relative 'base'
3
+
4
+ module FootballCli
5
+ module Format
6
+ class Table < Base
7
+ def generate
8
+ response.each do |data|
9
+ rows.push(
10
+ columns.collect {|c|
11
+ if goal_columns.include?(c) && data[:result] # when requesting team's fixtures
12
+ data[:result][c]
13
+ else
14
+ pretty_table(data, c)
15
+ end
16
+ }
17
+ )
18
+ end
19
+
20
+ Terminal::Table.new do |t|
21
+ t.title = title
22
+ t.headings = columns.map(&:capitalize)
23
+ t.rows = rows
24
+ end
25
+ end
26
+
27
+ def output
28
+ puts generate
29
+
30
+ write_to_file(generate)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,128 @@
1
+ require 'football_cli'
2
+ require 'football_ruby'
3
+ require 'terminal-table'
4
+
5
+ require_relative 'mapper'
6
+ require_relative 'format/format_factory'
7
+
8
+ module FootballCli
9
+ class Handler
10
+ include FootballCli::Mapper
11
+
12
+ def initialize(command, options={})
13
+ @command = command
14
+ @league = options[:league]
15
+ @match_day = options[:match_day]
16
+ @players = options[:players]
17
+ @fixtures = options[:fixtures]
18
+ @team = options[:team]
19
+ @format = options[:format] || 'table'
20
+ @file = options[:file]
21
+ end
22
+
23
+ def run
24
+ case command
25
+ when :live
26
+ live_scores
27
+ when :show
28
+ case
29
+ when league, match_day
30
+ league_table
31
+ when team && players, fixtures
32
+ if players
33
+ team_players
34
+ elsif fixtures
35
+ team_fixtures
36
+ end
37
+ else
38
+ raise 'Invalid option'
39
+ end
40
+ else
41
+ raise 'Invalid command'
42
+ end
43
+ end
44
+
45
+ def league_table
46
+ response = client.league_table(league_id, match_day: match_day)
47
+
48
+ display(
49
+ title: response[:leagueCaption],
50
+ response: response[:standing],
51
+ columns: %i(position teamName playedGames goalDifference points),
52
+ qualification: qualification
53
+ )
54
+ end
55
+
56
+ def team_players
57
+ response = client.team_players(team_id)
58
+ team_response = client.team(team_id)
59
+
60
+ display(
61
+ title: "#{team_response[:name]} players. Total #{response[:count]}",
62
+ response: response[:players],
63
+ columns: %i(jerseyNumber name position nationality dateOfBirth)
64
+ )
65
+ end
66
+
67
+ def team_fixtures
68
+ response = client.team_fixtures(team_id)
69
+ team_response = client.team(team_id)
70
+
71
+ display(
72
+ title: "#{team_response[:name]} players. Total #{response[:count]}",
73
+ response: response[:fixtures],
74
+ columns: %i(matchday homeTeamName goalsHomeTeam goalsAwayTeam awayTeamName status date)
75
+ )
76
+ end
77
+
78
+ def live_scores
79
+ response = client.live_scores
80
+
81
+ display(
82
+ title: 'Live scores',
83
+ response: response[:games],
84
+ columns: %i(league homeTeamName goalsHomeTeam goalsAwayTeam awayTeamName time)
85
+ )
86
+ end
87
+
88
+ private
89
+
90
+ attr_reader :client, :command, :league, :match_day, :players, :fixtures, :team, :format, :file
91
+
92
+ def league_id
93
+ get_league_id(league)
94
+ end
95
+
96
+ def team_id
97
+ get_team_id(team)
98
+ end
99
+
100
+ def qualification
101
+ get_qualification(league)
102
+ end
103
+
104
+ def display(opts = {})
105
+ response = opts[:response]
106
+ title = opts[:title]
107
+ columns = opts[:columns]
108
+ qualification = opts[:qualification]
109
+
110
+ factory = FootballCli::Format::FormatFactory.build(
111
+ format,
112
+ {
113
+ response: response,
114
+ title: title,
115
+ columns: columns,
116
+ file_name: file,
117
+ qualification: qualification
118
+ }
119
+ )
120
+
121
+ factory.output
122
+ end
123
+
124
+ def client
125
+ @client ||= FootballRuby::Client.new
126
+ end
127
+ end
128
+ end
@@ -0,0 +1,39 @@
1
+ require 'json'
2
+
3
+ module FootballCli
4
+ module Mapper
5
+ def get_league_id(code)
6
+ league_info(code).dig(:id)
7
+ end
8
+
9
+ def get_team_id(code)
10
+ team_info(code).dig(:id)
11
+ end
12
+
13
+ def get_qualification(code)
14
+ league_info(code).dig(:qualification)
15
+ end
16
+
17
+ private
18
+
19
+ def league_info(code)
20
+ @league_info ||= parse_json(leagues_path).find {|league| code == league[:league]} || {}
21
+ end
22
+
23
+ def team_info(code)
24
+ @team_info ||= parse_json(teams_path).find {|team| code == team[:code]} || {}
25
+ end
26
+
27
+ def parse_json(path)
28
+ @parse_json ||= JSON.parse(File.read(path), symbolize_names: true)
29
+ end
30
+
31
+ def leagues_path
32
+ File.join 'config', 'leagues.json'
33
+ end
34
+
35
+ def teams_path
36
+ File.join 'config', 'teams.json'
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module FootballCli
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'football_cli/handler'
2
+ require 'football_cli/version'
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: football_cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Said Kaldybaev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: football_ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rainbow
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: aruba
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-table
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: gli
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: 2.16.0
90
+ - - "~>"
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '='
98
+ - !ruby/object:Gem::Version
99
+ version: 2.16.0
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ description: A command line interface for all the football data feeds in Ruby
104
+ email:
105
+ - said.kaldybaev@gmail.com
106
+ executables:
107
+ - football_cli
108
+ extensions: []
109
+ extra_rdoc_files: []
110
+ files:
111
+ - ".gitignore"
112
+ - ".ruby-version"
113
+ - Gemfile
114
+ - Gemfile.lock
115
+ - README.MD
116
+ - Rakefile
117
+ - bin/football_cli
118
+ - config/leagues.json
119
+ - config/teams.json
120
+ - football_cli.gemspec
121
+ - lib/football_cli.rb
122
+ - lib/football_cli/format/base.rb
123
+ - lib/football_cli/format/csv.rb
124
+ - lib/football_cli/format/format_factory.rb
125
+ - lib/football_cli/format/json.rb
126
+ - lib/football_cli/format/table.rb
127
+ - lib/football_cli/handler.rb
128
+ - lib/football_cli/mapper.rb
129
+ - lib/football_cli/version.rb
130
+ homepage: https://github.com/Saidbek/football_cli
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.5.1
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: A command line interface
154
+ test_files: []