Class: Bookman::Bookman

Inherits:
Object
  • Object
show all
Defined in:
lib/bookman/beer.rb,
lib/bookman/world.rb,
lib/bookman/bookman.rb,
lib/bookman/football.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Bookman

todo: add logutils/logger here



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bookman/bookman.rb', line 9

def initialize( config )
  @config = config

  ## for debugging dump main config settings
  puts "dataset:"
  puts "  - collection: #{@config.collection}"
  puts "  - setup:      #{@config.setup}"
  ## todo: add db_path - why, why not???

  puts "datafile:"
  puts "  - datafile_url  (remote):  #{@config.datafile_url}"
  puts "  - datafile_dir  (local):   #{@config.datafile_dir}"
  puts "  - datafile_path (local):   #{@config.datafile_path}"

  puts "bookfile:"
  puts "  - bookfile_url  (remote):  #{@config.bookfile_url}"
  puts "  - bookfile_dir  (local):   #{@config.bookfile_dir}"
  puts "  - bookfile_path (local):   #{@config.bookfile_path}"
  ## todo: add bookfile_unzip_path - why, why not???
end

Class Method Details

.create_beer_book_for(setup, opts = {}) ⇒ Object



39
40
41
42
# File 'lib/bookman/beer.rb', line 39

def self.create_beer_book_for( setup, opts={} )
  config = BeerConfig.new( setup: setup )
  Bookman.new( config )
end

.create_football_book_for(setup, opts = {}) ⇒ Object



43
44
45
46
# File 'lib/bookman/football.rb', line 43

def self.create_football_book_for( setup, opts={} )
  config = FootballConfig.new( setup: setup )
  Bookman.new( config )
end

.create_world_book_for(setup, opts = {}) ⇒ Object



39
40
41
42
# File 'lib/bookman/world.rb', line 39

def self.create_world_book_for( setup, opts={} )
  config = WorldConfig.new( setup: setup )
  Bookman.new( config )
end

Instance Method Details

#buildObject



183
184
185
186
187
188
189
190
191
# File 'lib/bookman/bookman.rb', line 183

def build
  ## all-in-one; do everything; complete all steps
  dl_datasets
  dl_book_templates

  build_db
  build_book
  run_jekyll
end

#build_bookObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/bookman/bookman.rb', line 147

def build_book
  connect()

  bookfile_path = @config.bookfile_path

  bookfile = Bookfile::Bookfile.load_file( bookfile_path )
  bookfile.dump        ## for debugging

  ### fix:
  ###     assume WorldDb::Models already included ??
  ## - for now always include on prepare
  bookfile.prepare( @config.book_templates_unzip_dir )

  puts "  contintents: #{WorldDb::Model::Continent.count}"   ## for debugging

  bookfile.build( @config.book_templates_unzip_dir )

  puts 'Done.'
end

#build_dbObject



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
# File 'lib/bookman/bookman.rb', line 101

def build_db
  ## clean; remove db if exits

  db_path = @config.db_path
  FileUtils.rm( db_path )  if File.exists?( db_path )

  connect()
  @config.create_db!

  datafile_path = @config.datafile_path
  pp datafile_path


  ### hack/quick fix for at,de - "standalone quick test": todo
  ##   - find something better
  if datafile_path.end_with?( 'at.rb' ) ||
     datafile_path.end_with?( '/at/Datafile' )
    ## standalone austria for debugging add country
    WorldDb::Model::Country.create!( key: 'at',
                              name: 'Austria',
                              code: 'AUT',
                              pop: 0,
                              area: 0 )
  elsif datafile_path.end_with?( 'de.rb' ) ||
        datafile_path.end_with?( '/de/Datafile' )
    WorldDb::Model::Country.create!( key: 'de',
                              name: 'Germany',
                              code: 'GER',
                              pop: 0,
                              area: 0 )
  else
    # no special case; continue
    puts "[debug] - no special world archive case w/ start script; continue"
  end


  datafile = Datafile::Datafile.load_file( datafile_path )
  datafile.dump    ## for debugging

  ## set "global" logger to debug
  LogUtils::Logger.root.level = :debug

  datafile.read    ## datafile step 2 - read all datasets 
end

#connectObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/bookman/bookman.rb', line 79

def connect
  db_path = @config.db_path

  db_config = {
   adapter:  'sqlite3',
   database: db_path
  }

  pp db_config
  ActiveRecord::Base.establish_connection( db_config )

  c = ActiveRecord::Base.connection

  ## try to speed up sqlite
  ## see http://www.sqlite.org/pragma.html
  c.execute( 'PRAGMA synchronous=OFF;' )
  c.execute( 'PRAGMA journal_mode=OFF;' )
  ## c.execute( 'PRAGMA journal_mode=MEMORY;' )
  c.execute( 'PRAGMA temp_store=MEMORY;' )
end

#dl_book_templatesObject



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
# File 'lib/bookman/bookman.rb', line 51

def dl_book_templates
  ## fetch Bookfile
  bookfile_dir  = @config.bookfile_dir
  bookfile_path = @config.bookfile_path

  ## check if folders exists? if not create folder in path
  FileUtils.mkdir_p( bookfile_dir )  unless Dir.exists?( bookfile_dir )

  ## note: lets use http:// instead of https:// for now - lets us use person proxy (NOT working w/ https for now)
  src = @config.bookfile_url
  ## dest will be something like './Bookfile'

  fetch_bookfile( src, bookfile_path )


  bookfile = Bookfile::Bookfile.load_file( bookfile_path )

  bookfile.dump        ## for debugging
  bookfile.download    ## bookfile step 1 - download all packages/zips (defaults to ./tmp) 
  
  ## todo/check: already checked in unzip if folder exists???
  dest_unzip = @config.book_templates_unzip_dir
  FileUtils.mkdir_p( dest_unzip )  unless Dir.exists?( dest_unzip )

  bookfile.unzip( dest_unzip )   ## bookfile step 2 - unzip book templates 
end

#dl_datasetsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bookman/bookman.rb', line 31

def dl_datasets
  ## fetch Datafile
  datafile_dir  = @config.datafile_dir
  datafile_path = @config.datafile_path

  ## check if folders exists? if not create folder in path
  FileUtils.mkdir_p( datafile_dir )  unless Dir.exists?( datafile_dir )

  ## note: lets use http:// instead of https:// for now - lets us use person proxy (NOT working w/ https for now)
  src = @config.datafile_url
  ## dest will be something like './Datafile'

  fetch_datafile( src, datafile_path )

  datafile = Datafile::Datafile.load_file( datafile_path )
  datafile.dump        ## for debugging
  datafile.download    ## datafile step 1 - download all datasets/zips 
end

#run_jekyllObject



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bookman/bookman.rb', line 168

def run_jekyll
  # change cwd folder
  cwd = FileUtils.pwd
  puts "cwd (before): #{FileUtils.pwd}"
  FileUtils.cd( @config.book_templates_unzip_dir )
  puts "cwd (after): #{FileUtils.pwd}"

  ## use `cd #{book_dir}; jekyll build`  -- why, why not???
  puts `jekyll build`

  # restore cwd folder
  FileUtils.cd( cwd )
end