class Twimock::Database

Constants

ADAPTER
DB_DIRECTORY
DEFAULT_DB_NAME
TABLE_NAMES

Attributes

connection[R]
name[R]

Public Class Methods

new(name=nil) click to toggle source
# File lib/twimock/database.rb, line 14
def initialize(name=nil)
  @name = DEFAULT_DB_NAME
  connect
  create_tables
end

Public Instance Methods

clear() click to toggle source
# File lib/twimock/database.rb, line 42
def clear
  drop_tables
  create_tables
end
connect() click to toggle source
# File lib/twimock/database.rb, line 20
def connect
  @connection = SQLite3::Database.new filepath
  @state = :connected
  @connection
end
connected?() click to toggle source
# File lib/twimock/database.rb, line 32
def connected?
  @state == :connected
end
create_tables() click to toggle source
# File lib/twimock/database.rb, line 47
def create_tables
  TABLE_NAMES.each do |table_name|
    self.send "create_#{table_name}_table" unless table_exists?(table_name)
  end
  true
end
disconnect!() click to toggle source
# File lib/twimock/database.rb, line 26
def disconnect!
  @connection.close
  @state = :disconnected
  nil
end
drop() click to toggle source
# File lib/twimock/database.rb, line 36
def drop
  disconnect!
  File.delete(filepath) if File.exist?(filepath)
  nil
end
drop_table(table_name) click to toggle source
# File lib/twimock/database.rb, line 54
def drop_table(table_name)
  return false unless File.exist?(filepath) && table_exists?(table_name)
  @connection.execute "drop table #{table_name};"
  true
end
drop_tables() click to toggle source
# File lib/twimock/database.rb, line 60
def drop_tables
  return false unless File.exist?(filepath)
  TABLE_NAMES.each{|table_name| drop_table(table_name) }
  true
end
filepath() click to toggle source
# File lib/twimock/database.rb, line 66
def filepath
  name ||= @name
  File.join(DB_DIRECTORY, "#{@name}.#{ADAPTER}")
end
table_exists?(table_name) click to toggle source
# File lib/twimock/database.rb, line 71
def table_exists?(table_name)
  tables = @connection.execute "select * from sqlite_master"
  tables.each do |table|
    return true if table[1].to_s == table_name.to_s
  end
  false
end

Private Instance Methods

create_access_tokens_table() click to toggle source
# File lib/twimock/database.rb, line 106
    def create_access_tokens_table
      @connection.execute <<-SQL
        CREATE TABLE access_tokens (
          id              INTEGER   PRIMARY KEY AUTOINCREMENT,
          string          TEXT      NOT NULL,
          secret          TEXT      NOT NULL,
          application_id  INTEGER,
          user_id         INTEGER   NOT NULL,
          created_at      DATETIME  NOT NULL,
          UNIQUE(string, secret));
      SQL
    end
create_applications_table() click to toggle source
# File lib/twimock/database.rb, line 81
    def create_applications_table
      @connection.execute <<-SQL
        CREATE TABLE applications (
          id          INTEGER   PRIMARY KEY AUTOINCREMENT,
          api_key     TEXT      NOT NULL,
          api_secret  TEXT      NOT NULL,
          created_at  DATETIME  NOT NULL,
          UNIQUE(api_secret)
        );
      SQL
    end
create_request_tokens_table() click to toggle source
# File lib/twimock/database.rb, line 119
    def create_request_tokens_table
      @connection.execute <<-SQL
        CREATE TABLE request_tokens (
          id              INTEGER   PRIMARY KEY AUTOINCREMENT,
          string          TEXT      NOT NULL,
          secret          TEXT      NOT NULL,
          verifier        TEXT      NOT NULL,
          application_id  INTEGER   NOT NULL,
          user_id         INTEGER,
          created_at      DATETIME  NOT NULL,
          UNIQUE(string, secret, verifier));
      SQL
    end
create_users_table() click to toggle source
# File lib/twimock/database.rb, line 93
    def create_users_table
      @connection.execute <<-SQL
        CREATE TABLE users (
          id                   INTEGER   PRIMARY KEY AUTOINCREMENT,
          name                 TEXT      NOT NULL,
          twitter_id           TEXT      NOT NULL,
          email                TEXT      NOT NULL,
          password             TEXT      NOT NULL,
          created_at           DATETIME  NOT NULL,
          UNIQUE(twitter_id, email));
      SQL
    end