class Robotwitter::Db

Constants

TABLENAME

Public Class Methods

new(name, path) click to toggle source
# File lib/robotwitter/db.rb, line 24
def initialize(name, path)
  create_db("#{path}/#{name}") unless exists? "#{path}/#{name}"
  @db = SQLite3::Database.new "#{path}/#{name}.db"
end

Public Instance Methods

create_db(name) click to toggle source

create database

# File lib/robotwitter/db.rb, line 11
    def create_db(name)
      db = SQLite3::Database.new("#{name}.db")

      sql = <<-SQL
            create table #{TABLENAME} (
              id  integer PRIMARY KEY AUTOINCREMENT,
              tweet_id string
            );
            SQL

      db.execute_batch(sql)
    end
exists?(name) click to toggle source

check if database exists

# File lib/robotwitter/db.rb, line 6
def exists?(name)
  File.exist?("#{name}.db")
end
retweeted?(result) click to toggle source

already retweeted this post

# File lib/robotwitter/db.rb, line 30
def retweeted? result
  sql = "select 1 from #{TABLENAME} where tweet_id = '#{result['id_str']}'"
  res = @db.get_first_row(sql)
  !res.nil?
end
save_retweet(result) click to toggle source

сохраняем ретвитт, чтобы не ретвиттить снова result - результат twitter.search

# File lib/robotwitter/db.rb, line 38
def save_retweet result
  id = result.attrs['id_str']
  sql = "insert into #{TABLENAME} (tweet_id) values (#{id})"
  @db.query(sql)
end