Added some preliminary database stuff; added a script to mock adding a post to the DB
This commit is contained in:
parent
ae443a5ba3
commit
587cc2fb55
2
.gitignore
vendored
2
.gitignore
vendored
@ -56,3 +56,5 @@ build-iPhoneSimulator/
|
|||||||
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
|
||||||
# .rubocop-https?--*
|
# .rubocop-https?--*
|
||||||
|
|
||||||
|
# Local database storage
|
||||||
|
data/vulture.db
|
||||||
|
4
Gemfile
Normal file
4
Gemfile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
source 'https://rubygems.org/'
|
||||||
|
|
||||||
|
gem 'sequel', '~> 5.63'
|
||||||
|
gem 'sqlite3', '~> 1.5'
|
15
Gemfile.lock
Normal file
15
Gemfile.lock
Normal 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
23
add_post.rb
Normal 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)
|
@ -1,2 +1,6 @@
|
|||||||
vulture:
|
vulture:
|
||||||
run_interval: 1
|
run_interval: 1
|
||||||
|
|
||||||
|
database:
|
||||||
|
adapter: 'sqlite'
|
||||||
|
database: 'data/vulture.db'
|
||||||
|
18
db/migrations/0001_add_posts_table.rb
Normal file
18
db/migrations/0001_add_posts_table.rb
Normal 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
5
lib/models/post.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
class Post < Sequel::Model
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
end
|
17
vulture.rb
17
vulture.rb
@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
require 'logger'
|
require 'logger'
|
||||||
|
require 'sequel'
|
||||||
|
require 'sqlite3'
|
||||||
|
|
||||||
require_relative 'lib/config.rb'
|
require_relative 'lib/config.rb'
|
||||||
|
|
||||||
@ -11,11 +13,26 @@ conf = Config.new(File.join(__dir__, 'data/defaults.yaml'))
|
|||||||
logger = Logger.new(STDOUT)
|
logger = Logger.new(STDOUT)
|
||||||
logger.level = Logger::INFO
|
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
|
# Main program loop
|
||||||
while true
|
while true
|
||||||
# Let the user know something is going on
|
# Let the user know something is going on
|
||||||
logger.info('Checking for new social media posts to make public...')
|
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
|
# Wait for the specified wait period
|
||||||
sleep(conf.get('vulture.run_interval') * 60)
|
sleep(conf.get('vulture.run_interval') * 60)
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user