A simple Ruby server using Sinatra that serves Bluesky custom feeds

check if hostname & publisher_did are set

+48
+6
lib/blue_factory/configuration.rb
··· 1 1 require_relative 'modules/configurable' 2 2 require_relative 'modules/interactions' 3 3 require_relative 'modules/feeds' 4 + require_relative 'errors' 4 5 5 6 module BlueFactory 6 7 extend Configurable ··· 10 11 # 11 12 # The server's `did:web:` service DID built from the configured hostname. 12 13 # @return [String] 14 + # @raise [ConfigurationError] if the hostname is not set 13 15 # 14 16 def self.service_did 17 + if hostname.nil? 18 + raise ConfigurationError, "The `hostname` property is not set. Set it with: BlueFactory.set(:hostname, 'example.com')" 19 + end 20 + 15 21 'did:web:' + hostname 16 22 end 17 23
+7
lib/blue_factory/errors.rb
··· 26 26 end 27 27 28 28 # 29 + # Raised when some required configuration is missing. 30 + # 31 + 32 + class ConfigurationError < StandardError 33 + end 34 + 35 + # 29 36 # Raised when configuring the available feeds if a provided feed key is invalid. 30 37 # 31 38
+35
lib/blue_factory/server.rb
··· 21 21 # 22 22 23 23 class Server < Sinatra::Base 24 + 25 + # @private 26 + @@config_checked = false 27 + 28 + # Run the Sinatra app as a self-hosted server. 29 + # See {https://www.rubydoc.info/gems/sinatra/Sinatra/Base#run!-class_method Sinatra::Base.run!}. 30 + # @raise [ConfigurationError] if the {BlueFactory.hostname} or {BlueFactory.publisher_did} is not set 31 + 32 + def self.run!(options = {}, &block) 33 + verify_config 34 + super 35 + end 36 + 37 + before do 38 + Server.send(:verify_config) unless @@config_checked 39 + end 40 + 24 41 configure do 25 42 disable :static 26 43 enable :quiet ··· 140 157 json_error('MethodNotImplemented', 'Method Not Implemented', status: 501) 141 158 end 142 159 end 160 + 161 + 162 + private 163 + 164 + def self.verify_config 165 + if BlueFactory.hostname.nil? 166 + raise ConfigurationError, "The `hostname` property is not set. Set it with: BlueFactory.set(:hostname, 'example.com')" 167 + end 168 + 169 + if BlueFactory.publisher_did.nil? 170 + raise ConfigurationError, 171 + "The `publisher_did` property is not set. Set it with: BlueFactory.set(:publisher_did, 'did:plc:qweqweqwe')" 172 + end 173 + 174 + @@config_checked = true 175 + end 176 + 177 + private_class_method :verify_config 143 178 end 144 179 end