Browse Source

Refactor config

tags/v0.2.0
aeris 1 year ago
parent
commit
6881206ca1
3 changed files with 89 additions and 40 deletions
  1. 66
    28
      lib/cross-post/config.rb
  2. 17
    8
      lib/cross-post/mastodon.rb
  3. 6
    4
      lib/cross-post/twitter.rb

+ 66
- 28
lib/cross-post/config.rb View File

@@ -2,42 +2,80 @@ require 'yaml'

class CrossPost
class Config
DEFAULT_CONFIG = File.join Dir.home, '.config/cross-post/config.yml'
DEFAULT_CONFIG_FOLDER = File.join Dir.home, '.config/cross-post'
DEFAULT_CONFIG_FILE = 'config.yml'

def initialize
@file = ENV.fetch 'CROSS_POST_CONFIG', DEFAULT_CONFIG
raise 'Unable to find config file' unless File.readable? @file
File.open(@file) { |f| @config = YAML.safe_load f }
end
class SubConfig
def initialize(file)
@file = file
@config = if File.readable? @file
File.open(@file) { |f| YAML.safe_load f }
else
{}
end
end

def [](key)
current = @config
key.split(/\./).each do |k|
current = current[k]
return nil if current.nil?
end
current
end

def [](key)
current = @config
key.split(/\./).each do |k|
current = current[k]
return nil if current.nil?
def fetch(key, default = nil)
self[key] || default
end
current
end

def []=(key, value)
*key, last = key.split(/\./)
current = @config
key.each do |k|
next_ = current[k]
case next_
when nil
next_ = current[k] = {}
when Hash
else
raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
def []=(key, value)
*key, last = key.split(/\./)
current = @config
key.each do |k|
next_ = current[k]
case next_
when nil
next_ = current[k] = {}
when Hash
else
raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
end
current = next_
end
current = next_
current[last] = value
end

def put(key, value, save: false)
self[key] = value
self.save if save
end

def save
LOGGER.debug "Saving #{@file}"
yaml = YAML.dump @config
File.write @file, yaml
end
current[last] = value
end

def save
File.write @file, YAML.dump(@config)
def initialize
@config = {}
@dir = ENV.fetch 'CONFIG_FOLDER', DEFAULT_CONFIG_FOLDER
file = ENV.fetch 'CONFIG_FILE', DEFAULT_CONFIG_FILE
self.load :settings, file
self.load :posts
self.load :users
end

def [](name)
settings = @config[name]
return settings if settings
self.load name
end

def load(name, file = nil)
file ||= "#{name}.yml"
file = File.join @dir, file
@config[name] = SubConfig.new file
end
end
end

+ 17
- 8
lib/cross-post/mastodon.rb View File

@@ -5,12 +5,21 @@ require 'awesome_print'
class CrossPost
class Mastodon
def initialize(config)
url = config['mastodon.url']
token = config['mastodon.token']
user = config['mastodon.user']
@user_url = URI.join(url, "/@#{user}").to_s
@client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token
@stream = ::Mastodon::Streaming::Client.new base_url: url, bearer_token: token
settings = config[:settings]

url = settings['mastodon.url']
token = settings['mastodon.token']
user = settings['mastodon.user']

LOGGER.debug "Mastodon base URL: #{url}"
@client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token

stream_url = settings.fetch 'mastodon.stream_url', url
LOGGER.debug "Mastodon stream URL: #{stream_url}"
@stream = ::Mastodon::Streaming::Client.new base_url: stream_url, bearer_token: token

@user_url = URI.join(ENV.fetch('BASE_USER_URL', url), "/@#{user}").to_s
LOGGER.debug "Mastodon user URL: #{@user_url}"
end

def feed(twitter)
@@ -18,9 +27,9 @@ class CrossPost
begin
case object
when ::Mastodon::Status
next if reject? object
LOGGER.info { 'Receiving status' }
LOGGER.debug { status.ap }
LOGGER.debug { object.ai }
next if reject? object
twitter.post_status object
end
rescue => e

+ 6
- 4
lib/cross-post/twitter.rb View File

@@ -6,11 +6,13 @@ require 'cgi'
class CrossPost
class Twitter
def initialize(config)
settings = config[:settings]

config = {
consumer_key: config['twitter.consumer.key'],
consumer_secret: config['twitter.consumer.secret'],
access_token: config['twitter.access.token'],
access_token_secret: config['twitter.access.secret']
consumer_key: settings['twitter.consumer.key'],
consumer_secret: settings['twitter.consumer.secret'],
access_token: settings['twitter.access.token'],
access_token_secret: settings['twitter.access.secret']
}
@client = ::Twitter::REST::Client.new config
@stream = ::Twitter::Streaming::Client.new config

Loading…
Cancel
Save