Class: FootballCat::Tool
- Inherits:
-
Object
- Object
- FootballCat::Tool
- Defined in:
- lib/football-cat/tool.rb
Instance Method Summary collapse
-
#csv_encode(values) ⇒ Object
helpers.
-
#initialize ⇒ Tool
constructor
A new instance of Tool.
- #run(args) ⇒ Object
Constructor Details
#initialize ⇒ Tool
Returns a new instance of Tool.
6 7 |
# File 'lib/football-cat/tool.rb', line 6 def initialize end |
Instance Method Details
#csv_encode(values) ⇒ Object
helpers
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/football-cat/tool.rb', line 95 def csv_encode( values ) ## quote values that incl. a comma values.map do |value| if value.index(',') puts "** rec with field with comma:" pp values %Q{"#{value}"} else value end end.join( ',' ) end |
#run(args) ⇒ Object
10 11 12 13 14 15 16 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/football-cat/tool.rb', line 10 def run( args ) = {} optparser = OptionParser.new do |parser| parser. = "Usage: football-cat [options] OUTPATH INPATHS..." end optparser.parse!( args ) if args.empty? puts "!! ERROR - football.csv name/path expected (e.g. league.csv)" puts optparser.help exit 1 end outpath = args.shift puts "outpath: >#{outpath}<" puts "args:" p args headers = nil args.each do |arg| inpath = File.( arg ) puts "reading #{inpath}..." if File.directory?( inpath ) pack = SportDb::DirPackage.new( inpath ) pack.each_csv do |entry| puts " #{entry.name}" basename = File.basename( entry.name, File.extname( entry.name ) ) ## get basename WITHOUT extension ## note: upcase for now and remove dots e.g. ## es.1 => ES1 ## de.cup => DECUP - why? why not? league_key = basename.upcase.gsub( '.', '' ) ## todo/fix: check if season_key is proper season - e.g. matches pattern !!!! season_q = File.basename( File.dirname( entry.name )) season = Season.parse( season_q ) ## normalize season season_key = season.key puts " league: #{league_key}, season: #{season_key}" txt = entry.read ## matches = SportDb::CsvMatchParser.parse( txt ) rows = CsvHash.parse( txt ) puts " addinging #{rows.size} match(es)..." # rows = rows.map do |row| # { 'League' => league_key, # 'Season' => season_key }.merge( row ) # end # pp rows[0..2] if headers.nil? ## first time - create file with headers headers = ['League', 'Season'] + rows[0].keys puts "headers:" pp headers FileUtils.mkdir_p( File.dirname( outpath )) ## make sure path exists File.open( outpath, 'w:utf-8' ) do |f| f.write( headers.join(',') ) f.write( "\n" ) end end File.open( outpath, 'a:utf-8' ) do |f| rows.each do |row| f.write( csv_encode( [ league_key, season_key ] + row.values )) f.write( "\n") end end end else ## assume "free-standing" single datatfile puts "!! WARN - skipping datafile >#{inpath}< - sorry - for now only directories / packages work" end end puts "Done." end |