Sha256: bff32d272977b96b4ef216f72c2fda93c4e9b5460f49590eef3f08b17a1ce722

Contents?: true

Size: 1.67 KB

Versions: 7

Compression:

Stored size: 1.67 KB

Contents

module Chatterbot

  #
  # routines for optionally interacting with a database for logging
  # tweets, and storing config data there. Uses Sequel to handle the
  # heavy lifing.
  module DB
    #
    # connect to the database, and generate any missing tables
    def db
      @_db ||= connect_and_validate
    end

    def display_db_config_notice
      puts "ERROR: You have specified a DB connection, but you need to install the sequel gem to use it"
    end

    protected

    #
    # get a DB object from Sequel
    def get_connection
      if ! has_sequel? && config.has_key?(:db_uri)
        display_db_config_notice
      elsif has_sequel?
        Sequel.connect(config[:db_uri])
      end
    end

    #
    # try and connect to the DB, and create tables that are missing.
    def connect_and_validate
      conn = get_connection
      return if conn.nil?

      if ! conn.tables.include?(:blacklist)
        conn.create_table :blacklist do
          String :user, :primary_key => true
          DateTime :created_at
        end
      end

      if ! conn.tables.include?(:tweets)
        conn.create_table :tweets do
          primary_key :id
          String :txt
          String :bot
          String :user
          String :source_id
          String :source_tweet

          DateTime :created_at
        end
      end

      if ! conn.tables.include?(:config)
        conn.create_table :config do
          String :id, :primary_key => true

          Bignum :since_id

          String :secret
          String :token
          String :consumer_secret
          String :consumer_key

          DateTime :created_at
          DateTime :updated_at
        end
      end

      conn
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
chatterbot-0.7.0 lib/chatterbot/db.rb
chatterbot-0.6.6 lib/chatterbot/db.rb
chatterbot-0.6.5 lib/chatterbot/db.rb
chatterbot-0.6.3 lib/chatterbot/db.rb
chatterbot-0.6.2 lib/chatterbot/db.rb
chatterbot-0.6.1 lib/chatterbot/db.rb
chatterbot-0.5.1 lib/chatterbot/db.rb