Module: Footty

Defined in:
lib/footty.rb,
lib/footty/dataset.rb,
lib/footty/version.rb

Defined Under Namespace

Classes: Dataset

Constant Summary collapse

VERSION =
'2024.9.27'

Class Method Summary collapse

Class Method Details



5
6
7
# File 'lib/footty/version.rb', line 5

def self.banner
  "footty/#{VERSION} on Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}] in (#{root})"
end

.main(args = ARGV) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/footty.rb', line 17

def self.main( args=ARGV )
  puts banner # say hello

   league = 'en'
   year   = nil

   leagues = Dataset.leagues  ## e.g. ['world','euro',
                              ##       'de','en','at']

   ## filter args for league or year
   args = args.select do |arg|
                           if arg =~ /^\d{4}$/
                               year = arg.to_i(10)
                               false  ## eat-up
                           elsif leagues.include?( arg )
                               league = arg
                               false
                           else
                               true
                           end
                       end


  what = args[0] || 'today'
  what = what.downcase


  # Dataset.new( league: 'euro', year: 2024 )
  dataset = Dataset.new( league: league, year: year )


  ## in the future make today "configurable" as param - why? why not?
  today = Date.today

  if ['yesterday', 'y', '-1'].include?( what )
    matches = dataset.yesterdays_matches
    if matches.empty?
       puts "** No matches played yesterday.\n"
    end
  elsif ['tomorrow', 't', '+1', '1'].include?( what )
    matches = dataset.tomorrows_matches
    if matches.empty?
       puts "** No matches scheduled tomorrow.\n"
    end
  elsif ['past', 'p', 'prev'].include?( what )
    matches = dataset.past_matches
    if matches.empty?
       puts "** No matches played yet.\n"
    end
  elsif ['upcoming', 'up', 'u', 'next', 'n'].include?( what )
    matches = dataset.upcoming_matches
    if matches.empty?
       puts "** No more matches scheduled.\n"
    end
  else
     matches = dataset.todays_matches

     ## no matches today
     if matches.empty?
        puts "** No matches scheduled today.\n"

        ## note: was world cup 2018 - end date -- Date.new( 2018, 7, 11 )
        ## note: was euro 2020 (in 2021) - end date -- Date.new( 2021, 7, 11 )
        if Date.today > dataset.end_date    ## tournament is over, look back
          puts "Past matches:"
          matches = dataset.past_matches
        else  ## world cup is upcoming /in-progress,look forward
          puts "Upcoming matches:"
          matches = dataset.upcoming_matches( limit: 18 )
        end
     end
  end

  print_matches( matches )
end


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/footty.rb', line 94

def self.print_matches( matches )
  ## print games

  today = Date.today

  matches.each do |match|
    print "   %5s" % "\##{match['num']} "   if match['num']

    date = Date.strptime( match['date'], '%Y-%m-%d' )
    print "#{date.strftime('%a %b/%d')} "      ## e.g. Thu Jun/14
    print "#{match['time']} "  if match['time']

    if date > today
       diff = (date - today).to_i
       print "%10s" % "(in #{diff}d) "
    end


    if match['team1'].is_a?( Hash )
      print "%22s" % "#{match['team1']['name']} (#{match['team1']['code']})"
    else
      print "%22s" % "#{match['team1']}"
    end


    if match['score'].is_a?( Hash ) &&
       match['score']['ft']
        if match['score']['ft']
           print " #{match['score']['ft'][0]}-#{match['score']['ft'][1]} "
        end
        if match['score']['et']
          print "aet #{match['score']['et'][0]}-#{match['score']['et'][1]} "
        end
        if match['score']['p']
          print "pen #{match['score']['p'][0]}-#{match['score']['p'][1]} "
        end
    elsif match['score1'] && match['score2']
      ## todo/fix: add support for knockout scores
      ##                 with score1et/score1p  (extra time and penalty)
      print " #{match['score1']}-#{match['score2']} "
      print "(#{match['score1i']}-#{match['score2i']}) "
    else
      print "    vs    "
    end

    if match['team2'].is_a?( Hash )
      print "%-22s" % "#{match['team2']['name']} (#{match['team2']['code']})"
    else
      print "%-22s" % "#{match['team2']}"
    end


    if match['group']
      print " #{match['group']} /"    ## group phase/stage
    end

    print " #{match['round']} "    ## knock out (k.o.) phase/stage

    if match['stadium']
      print " @ #{match['stadium']['name']}, #{match['city']}"
    end

    print "\n"


    if match['goals1'] && match['goals2']
      print "                     ["
      match['goals1'].each_with_index do |goal,i|
        print " "    if i > 0
        print "#{goal['name']}"
        print " #{goal['minute']}"
        print "+#{goal['offset']}"   if goal['offset']
        print "'"
        print " (o.g.)"    if goal['owngoal']
        print " (pen.)"    if goal['penalty']
      end
      match['goals2'].each_with_index do |goal,i|
        if i == 0
          print "; "
        else
         print " "
        end
        print "#{goal['name']}"
        print " #{goal['minute']}"
        print "+#{goal['offset']}"  if goal['offset']
        print "'"
        print " (o.g.)"  if goal['owngoal']
        print " (pen.)"  if goal['penalty']
      end
      print "]\n"
    end
  end
end

.rootObject



9
10
11
# File 'lib/footty/version.rb', line 9

def self.root
  File.expand_path( File.dirname(File.dirname(File.dirname(__FILE__))) )
end