Class: Rsssf::Repo

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/rsssf/repo.rb

Instance Method Summary collapse

Methods included from Utils

#archive_dir_for_season, #year_from_file, #year_from_name

Constructor Details

#initialize(path, title: 'Your Title Here', patch: nil) ⇒ Repo

Returns a new instance of Repo.



10
11
12
13
14
15
# File 'lib/rsssf/repo.rb', line 10

def initialize( path, title: 'Your Title Here',    
                      patch: nil )   
  @repo_path = path
  @title     = title
  @patch     = patch
end

Instance Method Details

#each_page(code, seasons, &blk) ⇒ Object

use each table or such - why? why not?



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rsssf/repo.rb', line 56

def each_page( code, seasons, &blk )  ## use each table or such - why? why not?
  seasons.each do |season|
    url = Rsssf.table_url( code, season: season )
    url_path = URI.parse( url ).path
    puts "  url = >#{url}<"
    puts "  url_path = >#{url_path}<"
    basename = File.basename( url_path, File.extname( url_path ))

    path = "#{@repo_path}/tables/#{basename}.txt"
     page = Page.read_txt( path )

     ## add/pass along patcher if patcher
     if @patch 
       page.patch  = @patch
       page.url    = url
     end 

    season = Season( season )
    blk.call( season, page )
  end
end

#make_pages_summaryObject



79
80
81
82
83
84
85
# File 'lib/rsssf/repo.rb', line 79

def make_pages_summary
  files = Dir.glob( "#{@repo_path}/tables/*.txt" )
  report = PageReport.build( files, title: @title )    ## pass in title etc.  

  ### save report as README.md in tables/ folder in repo
  report.save( "#{@repo_path}/tables/README.md" )
end

#make_schedules_summaryObject



88
89
90
91
92
93
94
95
96
97
# File 'lib/rsssf/repo.rb', line 88

def make_schedules_summary
   ## find all match datafiles
   args = [@repo_path]
   files = SportDb::Parser::Opts.expand_args( args )
   pp files   
             
   report = ScheduleReport.build( files, title: @title,
                                         patch: @patch )   ## pass in title etc.
   report.save( "#{@repo_path}/README.md" )
end

#patch_dir(root, &blk) ⇒ Object



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
# File 'lib/rsssf/repo.rb', line 111

def patch_dir( root, &blk )
  files = Dir.glob( "#{root}/**/*.txt" )
  ## pp files

  ## sort files by year (latest first)
  files = files.sort do |l,r|
    lyear = year_from_file( l )
    ryear = year_from_file( r )
    
    ryear <=> lyear
  end

  files.each do |file|
    txt = read_text( file )    ## note: assumes already converted to utf-8

    basename = File.basename( file, '.txt' )  ## e.g. duit92.txt => duit92
    year     = year_from_name( basename )

    new_txt = blk.call( txt, basename, year )

    ## calculate hash to see if anything changed ?? why? why not??
    if txt != new_txt
      puts "  patching #{file}, text changed"
      write_text( file, new_txt )
    end
  end # each file
end

#patch_pages(patcher) ⇒ Object



102
103
104
105
106
107
108
# File 'lib/rsssf/repo.rb', line 102

def patch_pages( patcher )
  ## lets you run/use custom (repo/country-specific patches e.g. for adding/patching headings etc.)
  patch_dir( "#{@repo_path}/tables" ) do |txt, name, year|
    puts "patching #{year} (#{name}) (#{@repo_path})..."
    patcher.patch( txt, name, year )    ## note: must be last (that is, must return (patcher) t(e)xt)
  end
end

#prepare_pages(code, seasons) ⇒ Object

for now use single country repos - why? why not?

add support for all-in-one repos


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
# File 'lib/rsssf/repo.rb', line 24

def prepare_pages( code, seasons )
  seasons.each do |season|
    url = Rsssf.table_url( code, season: season )

    ## check if not in cache
    unless Webcache.cached?( url )
        ## download - if not cached
        Rsssf.download_table( code, season: season )
    end

    page = Page.read_cache( url )

    url_path = URI.parse( url ).path
    puts "  url = >#{url}<"
    puts "  url_path = >#{url_path}<"

    basename = File.basename( url_path, File.extname( url_path ))

    ###
    ## check for on_prepare (apply patches)
    if @patch && @patch.respond_to?(:on_prepare)
       year = year_from_name( basename )
       page.txt = @patch.on_prepare( page.txt, basename, year )    
    end


    path = "#{@repo_path}/tables/#{basename}.txt"
    page.save( path ) 
  end
end

#rootObject Also known as: root_dir

use/rename to path - why? why not?



18
# File 'lib/rsssf/repo.rb', line 18

def root() @repo_path; end