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'
2 2
 
3 3
 class CrossPost
4 4
 	class Config
5
-		DEFAULT_CONFIG = File.join Dir.home, '.config/cross-post/config.yml'
5
+		DEFAULT_CONFIG_FOLDER = File.join Dir.home, '.config/cross-post'
6
+		DEFAULT_CONFIG_FILE   = 'config.yml'
6 7
 
7
-		def initialize
8
-			@file = ENV.fetch 'CROSS_POST_CONFIG', DEFAULT_CONFIG
9
-			raise 'Unable to find config file' unless File.readable? @file
10
-			File.open(@file) { |f| @config = YAML.safe_load f }
11
-		end
8
+		class SubConfig
9
+			def initialize(file)
10
+				@file = file
11
+				@config = if File.readable? @file
12
+							  File.open(@file) { |f| YAML.safe_load f }
13
+						  else
14
+							  {}
15
+						  end
16
+			end
17
+
18
+			def [](key)
19
+				current = @config
20
+				key.split(/\./).each do |k|
21
+					current = current[k]
22
+					return nil if current.nil?
23
+				end
24
+				current
25
+			end
12 26
 
13
-		def [](key)
14
-			current = @config
15
-			key.split(/\./).each do |k|
16
-				current = current[k]
17
-				return nil if current.nil?
27
+			def fetch(key, default = nil)
28
+				self[key] || default
18 29
 			end
19
-			current
20
-		end
21 30
 
22
-		def []=(key, value)
23
-			*key, last = key.split(/\./)
24
-			current    = @config
25
-			key.each do |k|
26
-				next_ = current[k]
27
-				case next_
28
-				when nil
29
-					next_ = current[k] = {}
30
-				when Hash
31
-				else
32
-					raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
31
+			def []=(key, value)
32
+				*key, last = key.split(/\./)
33
+				current    = @config
34
+				key.each do |k|
35
+					next_ = current[k]
36
+					case next_
37
+					when nil
38
+						next_ = current[k] = {}
39
+					when Hash
40
+					else
41
+						raise "Invalid entry, Hash expected, had #{next_.class} (#{next_})"
42
+					end
43
+					current = next_
33 44
 				end
34
-				current = next_
45
+				current[last] = value
46
+			end
47
+
48
+			def put(key, value, save: false)
49
+				self[key] = value
50
+				self.save if save
51
+			end
52
+
53
+			def save
54
+					LOGGER.debug "Saving #{@file}"
55
+					yaml = YAML.dump @config
56
+					File.write @file, yaml
35 57
 			end
36
-			current[last] = value
37 58
 		end
38 59
 
39
-		def save
40
-			File.write @file, YAML.dump(@config)
60
+		def initialize
61
+			@config = {}
62
+			@dir    = ENV.fetch 'CONFIG_FOLDER', DEFAULT_CONFIG_FOLDER
63
+			file    = ENV.fetch 'CONFIG_FILE', DEFAULT_CONFIG_FILE
64
+			self.load :settings, file
65
+			self.load :posts
66
+			self.load :users
67
+		end
68
+
69
+		def [](name)
70
+			settings = @config[name]
71
+			return settings if settings
72
+			self.load name
73
+		end
74
+
75
+		def load(name, file = nil)
76
+			file ||= "#{name}.yml"
77
+			file = File.join @dir, file
78
+			@config[name] = SubConfig.new file
41 79
 		end
42 80
 	end
43 81
 end

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

@@ -5,12 +5,21 @@ require 'awesome_print'
5 5
 class CrossPost
6 6
 	class Mastodon
7 7
 		def initialize(config)
8
-			url       = config['mastodon.url']
9
-			token     = config['mastodon.token']
10
-			user      = config['mastodon.user']
11
-			@user_url = URI.join(url, "/@#{user}").to_s
12
-			@client   = ::Mastodon::REST::Client.new base_url: url, bearer_token: token
13
-			@stream   = ::Mastodon::Streaming::Client.new base_url: url, bearer_token: token
8
+			settings = config[:settings]
9
+
10
+			url   = settings['mastodon.url']
11
+			token = settings['mastodon.token']
12
+			user  = settings['mastodon.user']
13
+
14
+			LOGGER.debug "Mastodon base URL: #{url}"
15
+			@client = ::Mastodon::REST::Client.new base_url: url, bearer_token: token
16
+
17
+			stream_url = settings.fetch 'mastodon.stream_url', url
18
+			LOGGER.debug "Mastodon stream URL: #{stream_url}"
19
+			@stream = ::Mastodon::Streaming::Client.new base_url: stream_url, bearer_token: token
20
+
21
+			@user_url = URI.join(ENV.fetch('BASE_USER_URL', url), "/@#{user}").to_s
22
+			LOGGER.debug "Mastodon user URL: #{@user_url}"
14 23
 		end
15 24
 
16 25
 		def feed(twitter)
@@ -18,9 +27,9 @@ class CrossPost
18 27
 				begin
19 28
 					case object
20 29
 					when ::Mastodon::Status
21
-						next if reject? object
22 30
 						LOGGER.info { 'Receiving status' }
23
-						LOGGER.debug { status.ap }
31
+						LOGGER.debug { object.ai }
32
+						next if reject? object
24 33
 						twitter.post_status object
25 34
 					end
26 35
 				rescue => e

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

@@ -6,11 +6,13 @@ require 'cgi'
6 6
 class CrossPost
7 7
 	class Twitter
8 8
 		def initialize(config)
9
+			settings = config[:settings]
10
+
9 11
 			config  = {
10
-					consumer_key:        config['twitter.consumer.key'],
11
-					consumer_secret:     config['twitter.consumer.secret'],
12
-					access_token:        config['twitter.access.token'],
13
-					access_token_secret: config['twitter.access.secret']
12
+					consumer_key:        settings['twitter.consumer.key'],
13
+					consumer_secret:     settings['twitter.consumer.secret'],
14
+					access_token:        settings['twitter.access.token'],
15
+					access_token_secret: settings['twitter.access.secret']
14 16
 			}
15 17
 			@client = ::Twitter::REST::Client.new config
16 18
 			@stream = ::Twitter::Streaming::Client.new config

Loading…
Cancel
Save