Module: Lazybird::Db

Extended by:
Db
Included in:
Db
Defined in:
lib/lazybird/db.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.initObject

TODO: refactor this. This is a quick & dirty way to create the DB



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
# File 'lib/lazybird/db.rb', line 10

def self.init
  db.transaction do
    db.create_table :config do
      primary_key :id
      String :name, :unique => true, :null => false
      String :consumer_key, :null => false
      String :consumer_secret, :null => false
      String :access_token, :null => false
      String :access_token_secret, :null => false
    end

    db.create_table :tweets do
      primary_key :id
      String :text, :unique => true, :null => false
      DateTime :created_at
    end

    db.create_table :tasks do
      primary_key :id
      String :command, :unique => true, :null => false
      String :options
      DateTime :created_at
    end
  end
end

Instance Method Details

#configObject



47
48
49
# File 'lib/lazybird/db.rb', line 47

def config
  db[:config].first
end

#dbObject



80
81
82
# File 'lib/lazybird/db.rb', line 80

def db
  @_db ||= Sequel.amalgalite(Lazybird::Config.db_file)
end

#list_tasksObject



62
63
64
# File 'lib/lazybird/db.rb', line 62

def list_tasks
  db[:tasks].select(:command)
end

#rem_task!(task_command) ⇒ Object



58
59
60
# File 'lib/lazybird/db.rb', line 58

def rem_task!(task_command)
  db[:tasks].where('command = ?', task_command).delete
end

#save_config!(config) ⇒ Object

TODO: Ideally we should have different configs, perhaps per twitter user for now, let’s just delete every time we save a new one.



42
43
44
45
# File 'lib/lazybird/db.rb', line 42

def save_config!(config)
  db[:config].delete
  db[:config].insert(config.merge(name: 'default'))
end

#save_task!(task) ⇒ Object



51
52
53
54
55
56
# File 'lib/lazybird/db.rb', line 51

def save_task!(task)
  task_hash = {command: task.command,
               created_at: DateTime.now}
  task_hash.merge!(options: task.opts) if !task.opts.empty?
  db[:tasks].insert(Lazybird::Utils::Hash.compact(task_hash))
end

#save_tweet!(text:) ⇒ Object



36
37
38
# File 'lib/lazybird/db.rb', line 36

def save_tweet!(text:)
  db[:tweets].insert(text: text, created_at: DateTime.now)
end

#task_found(task_name, found) ⇒ Object



76
77
78
# File 'lib/lazybird/db.rb', line 76

def task_found(task_name, found)
  Object.const_get(found[task_name]['class'])
end

#tasksObject



66
67
68
69
70
71
72
73
74
# File 'lib/lazybird/db.rb', line 66

def tasks
  db[:tasks].all.map do |task|
    found = Lazybird::Utils::Hash.find_by_key(Lazybird::Config.tasks, task[:command])
    unless found.empty?
      task_found(task[:command], found).new(command: task[:command],
                                            options: found[:options])
    end
  end
end