Class: FootballToSqlite::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/football-to-sqlite.rb

Instance Method Summary collapse

Constructor Details

#initializeTool

Returns a new instance of Tool.



17
18
19
# File 'lib/football-to-sqlite.rb', line 17

def initialize
  LogUtils::Logger.root.level = :info   # set logging level to info
end

Instance Method Details

#connect(dbpath) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/football-to-sqlite.rb', line 22

def connect( dbpath )
  puts "working directory: #{Dir.pwd}"

  ## note:
  ##  File.dirname( 'test.db' ) returns '.'
  ##   thus, use expand_path first to get full path
  ##
  ## todo/check: move mkdir into connect (for (re)use) - why? why not?
  FileUtils.mkdir_p( File.dirname( File.expand_path( dbpath )))   ## make sure path exists

  SportDb.connect( adapter: 'sqlite3',
                   database: dbpath )

  SportDb.auto_migrate!

  LogDb.setup  # start logging to db (that is, save logs in logs table in db)
end

#run(args) ⇒ Object



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
# File 'lib/football-to-sqlite.rb', line 41

def run( args )

  opts = {}
  optparser = OptionParser.new do |parser|
    parser.banner = "Usage: football-to-sqlite [options] DATABASE PATHS..."
  end
  optparser.parse!( args )

  if args.empty?
    puts "!! ERROR - sqlite database name/path expected (e.g. sport.db)"
    puts optparser.help
    exit 1
  end

  dbpath = args.shift
  puts "dbpath: >#{dbpath}<"
  puts "args:"
  p args

  connect( dbpath )

  args.each do |arg|
    puts "reading #{arg}..."
    ## note: only support reading matchfiles for now (NOT zips & dirs or clubs & leagues etc.)
    SportDb.read_match( arg )
  end

  ## todo: check if stdin is always utf-8 or such?
  unless STDIN.tty?
    puts "reading STDIN..."
    ## assume/ read stdin as utf8 - possible?
    txt = STDIN.read
    puts "[------------->>>"
    puts txt
    puts "<<<-------------]"
    SportDb.parse_match( txt )
    puts "encoding: #{txt.encoding}"
  end


  ## print some stats
  SportDb.tables

  puts "Done."
end