Class: Worldfootball::League

Inherits:
Object
  • Object
show all
Defined in:
lib/webget-football/worldfootball/leagues.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, data) ⇒ League

Returns a new instance of League.



20
21
22
23
24
25
26
# File 'lib/webget-football/worldfootball/leagues.rb', line 20

def initialize( key, data )
  @key  = key
  ## @data = data

  @pages       = data[:pages]
  @season_proc = data[:season] || ->(season) { nil }
end

Instance Method Details

#fill_slug(slug, season:) ⇒ Object

helper method



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/webget-football/worldfootball/leagues.rb', line 68

def fill_slug( slug, season: )
  ## note: fill-in/check for place holders too
  slug = if slug.index( '{season}' )
           slug.sub( '{season}', season.to_path( :long ) )  ## e.g. 2010-2011
         elsif slug.index( '{end_year}' )
           slug.sub( '{end_year}', season.end_year.to_s )   ## e.g. 2011
         else
           ## assume convenience fallback - append regular season
           "#{slug}-#{season.to_path( :long )}"
        end

  puts "  slug=>#{slug}<"

  slug
end

#keyObject



28
# File 'lib/webget-football/worldfootball/leagues.rb', line 28

def key()   @key; end

#pages(season:) ⇒ Object



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
# File 'lib/webget-football/worldfootball/leagues.rb', line 30

def pages( season: )
  ## note: return for no stages / simple case - just a string
  ##   and for the stages case ALWAYS an array (even if it has only one page (with stage))

  if @pages.is_a?( String )
    # assume always "simple/regular" format w/o stages
    slug = @pages
    { slug: fill_slug( slug, season: season ) }
  else
     ## check for league format / stages
     ##   return array (of strings) or nil (for no stages - "simple" format)
     indices = @season_proc.call( season )
     if indices.nil?
       puts "!! ERROR - no configuration found for season >#{season}< for league >#{@key}< found; sorry"
       exit 1
     elsif indices.is_a?( Integer )  ## single number - single/regular format w/o stage
      # note: starting with 0 (always use idx-1) !!!
       slug = if @pages.is_a?( Array )
                @pages[indices-1]
              else ## assume hash (and key is page slug)
                @pages.keys[indices-1]
              end
       { slug: fill_slug( slug, season: season ) }
     else  ## assume regular case - array of integers
       recs = []
       indices.each do |idx|
          slug = key = @pages.keys[idx-1]
          recs << { slug:  fill_slug( slug, season: season ),
                    stage: @pages[key] }  ## note: include mapping for page to stage name!!
       end
       recs
    end
  end
end