Class: StatsCombiner::Combiner

Inherits:
Object
  • Object
show all
Defined in:
lib/stats_combiner.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Combiner

Usage:

s = StatsCombiner::Combiner.new({
     :api_key => 'your_key',
     :host => 'talkingpointsmemo.com',
     :ttl => 3600,
     :flat_file => '/var/www/html/topten.html'
   })


19
20
21
22
23
24
25
26
# File 'lib/stats_combiner.rb', line 19

def initialize(opts = {})
  @init_options = {
    :ttl => 3600, #one hour by default
    :story_count => 10,
    :flat_file => 'topten.html',
  }.merge!(opts)
  @db_file = "#{@init_options[:host]}_stats_db.sqlite3"
end

Instance Method Details

#run(opts = {}) ⇒ Object

check where we are in the cycle and run the necessary functions call this after initializing StatsCombiner

Usage:

s.run({
 :filters => e.filters (result of a StatsCombiner::Filterer filters hash)
 :verbose => true
})


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

def run(opts = {})
  { :filters => nil,
    :verbose => false
  }.merge!(opts)
  
  @filters = opts[:filters]
  
  now = Time.now
  if File::exists?(@db_file)
    @db = Sequel.sqlite(@db_file)
    @table_create_time = self.table_create_time
    @table_destroy_time = @table_create_time + @init_options[:ttl]
    @ttl = @table_destroy_time.to_i - Time.now.to_i
    
    if now.to_i < @table_destroy_time.to_i 
     self.combine
     if opts[:verbose]
      puts "Combining. DB has #{@ttl} seconds to live"
     end
    else
     self.report_and_cleanup
     if opts[:verbose]
      puts "ttl expired. reporting and cleaning up"
      end
    end
  else
    self.setup
    if opts[:verbose]
      puts "No DB detected. I set one up"
    end
  end
end