Added some preliminary database stuff; added a script to mock adding a post to the DB

This commit is contained in:
Gregory Ballantine 2022-12-07 17:19:22 -05:00
parent ae443a5ba3
commit 587cc2fb55
8 changed files with 88 additions and 0 deletions

2
.gitignore vendored
View File

@ -56,3 +56,5 @@ build-iPhoneSimulator/
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
# .rubocop-https?--*
# Local database storage
data/vulture.db

4
Gemfile Normal file
View File

@ -0,0 +1,4 @@
source 'https://rubygems.org/'
gem 'sequel', '~> 5.63'
gem 'sqlite3', '~> 1.5'

15
Gemfile.lock Normal file
View File

@ -0,0 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
sequel (5.63.0)
sqlite3 (1.5.4-x64-mingw-ucrt)
PLATFORMS
x64-mingw-ucrt
DEPENDENCIES
sequel (~> 5.63)
sqlite3 (~> 1.5)
BUNDLED WITH
2.3.7

23
add_post.rb Normal file
View File

@ -0,0 +1,23 @@
#!/usr/bin/env ruby
require 'sequel'
require 'sqlite3'
require_relative 'lib/config.rb'
# Load the Sequel timestamps plugin
Sequel::Model.plugin :timestamps
# Load configuration
conf = Config.new(File.join(__dir__, 'data/defaults.yaml'))
# Initialize Sequel gem for database actions
DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database'))
# Load models
require_relative 'lib/models/post.rb'
# Create new time object, three minutes in the future
t = Time.now + (3 * 60)
# Insert post
Post.create(text: ARGV[0], publish_at: t)

View File

@ -1,2 +1,6 @@
vulture:
run_interval: 1
database:
adapter: 'sqlite'
database: 'data/vulture.db'

View File

@ -0,0 +1,18 @@
Sequel.migration do
up do
create_table(:posts) do
primary_key :id
String :text, null: false
DateTime :publish_at
Boolean :published, default: false
DateTime :created_at
DateTime :updated_at
end
end
down do
drop_table(:posts)
end
end

5
lib/models/post.rb Normal file
View File

@ -0,0 +1,5 @@
class Post < Sequel::Model
end

View File

@ -1,6 +1,8 @@
#!/usr/bin/env ruby
require 'logger'
require 'sequel'
require 'sqlite3'
require_relative 'lib/config.rb'
@ -11,11 +13,26 @@ conf = Config.new(File.join(__dir__, 'data/defaults.yaml'))
logger = Logger.new(STDOUT)
logger.level = Logger::INFO
# Load the Sequel timestamps plugin
Sequel::Model.plugin :timestamps
# Initialize Sequel gem for database actions
DB = Sequel.connect(adapter: conf.get('database.adapter'), database: conf.get('database.database'))
# Load models
require_relative 'lib/models/post.rb'
# Main program loop
while true
# Let the user know something is going on
logger.info('Checking for new social media posts to make public...')
# Search for posts that are due to be published
to_post = Post.dataset.where(published: false).where{publish_at < Time.now}
# If we have posts to handle, let's do it!
to_post.each { |post|
puts "Posting tweet: #{post.text}"
post.update(published: true)
}
# Wait for the specified wait period
sleep(conf.get('vulture.run_interval') * 60)
end