Skip to content

Commit

Permalink
Configurable によって外部から環境変数を注入可能にした
Browse files Browse the repository at this point in the history
  • Loading branch information
maeda-m committed Feb 26, 2023
1 parent c0624a0 commit ca54718
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 34 deletions.
15 changes: 3 additions & 12 deletions app/models/discord/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

module Discord
class Server
TOKEN_PREFIX_SIZE = 4
include ActiveSupport::Configurable
config_accessor :guild_id, :authorize_token, instance_accessor: false

class << self
class_attribute :guild_id, :authorize_token, instance_reader: false

def create_text_channel(name:, parent: nil)
guild = Discord::Server.find_by(id: guild_id, token: authorize_token)
return nil if guild.blank?
Expand All @@ -18,14 +17,6 @@ def create_text_channel(name:, parent: nil)
nil
end

def guild_id
@guild_id || ENV['DISCORD_GUILD_ID'].presence
end

def authorize_token
@authorize_token || "Bot #{ENV['DISCORD_BOT_TOKEN']}"
end

def find_by(id:, token:)
return nil unless enabled?

Expand All @@ -40,7 +31,7 @@ def find_by(id:, token:)
end

def enabled?
guild_id && authorize_token.size > TOKEN_PREFIX_SIZE
guild_id && authorize_token
end

private
Expand Down
7 changes: 2 additions & 5 deletions app/models/discord/times_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

module Discord
class TimesChannel
class_attribute :category_id, instance_reader: false
include ActiveSupport::Configurable
config_accessor :category_id, instance_accessor: false

class << self
def to_channel_name(username)
username.downcase
end

def category_id
@category_id || ENV['DISCORD_TIMES_CHANNEL_CATEGORY_ID'].presence
end
end

def initialize(username)
Expand Down
9 changes: 9 additions & 0 deletions config/initializers/discord.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Rails.application.reloader.to_prepare do
Discord::Server.configure do |config|
config.guild_id = ENV['DISCORD_GUILD_ID'].presence

bot_token = ENV['DISCORD_BOT_TOKEN'].presence
config.authorize_token = "Bot #{bot_token}" if bot_token
end
Discord::TimesChannel.config.category_id = ENV['DISCORD_TIMES_CHANNEL_CATEGORY_ID'].presence
end
12 changes: 0 additions & 12 deletions test/models/discord/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,14 @@ class ServerTest < ActiveSupport::TestCase
test '.enabled?' do
Discord::Server.guild_id = '1234567890123456789'
Discord::Server.authorize_token = 'Bot valid token'
ENV['DISCORD_GUILD_ID'] = nil
ENV['DISCORD_BOT_TOKEN'] = nil
assert Discord::Server.enabled?

Discord::Server.guild_id = nil
Discord::Server.authorize_token = nil
ENV['DISCORD_GUILD_ID'] = '1234567890123456789'
ENV['DISCORD_BOT_TOKEN'] = 'Bot valid token'
assert Discord::Server.enabled?

Discord::Server.guild_id = nil
Discord::Server.authorize_token = nil
ENV['DISCORD_GUILD_ID'] = nil
ENV['DISCORD_BOT_TOKEN'] = nil
assert_not Discord::Server.enabled?

Discord::Server.guild_id = nil
Discord::Server.authorize_token = 'skip'
ENV['DISCORD_GUILD_ID'] = nil
ENV['DISCORD_BOT_TOKEN'] = nil
assert_not Discord::Server.enabled?
end
end
Expand Down
6 changes: 3 additions & 3 deletions test/models/discord/times_channel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
module Discord
class TimesChannelTest < ActiveSupport::TestCase
setup do
@category_id = ENV['DISCORD_TIMES_CHANNEL_CATEGORY_ID']
ENV['DISCORD_TIMES_CHANNEL_CATEGORY_ID'] = nil
@category_id = Discord::TimesChannel.category_id
Discord::TimesChannel.category_id = nil

@stub_create_text_channel = lambda { |name:, parent:|
Discordrb::Channel.new({
Expand All @@ -18,7 +18,7 @@ class TimesChannelTest < ActiveSupport::TestCase
end

teardown do
ENV['DISCORD_TIMES_CHANNEL_CATEGORY_ID'] = @category_id
Discord::TimesChannel.category_id = @category_id
end

test '#save return true' do
Expand Down
2 changes: 1 addition & 1 deletion test/system/notification/signed_up_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Notification::SignedUpTest < ApplicationSystemTestCase
AbstractNotifier.delivery_mode = :normal

@bot_token = Discord::Server.authorize_token
Discord::Server.authorize_token = 'skip'
Discord::Server.authorize_token = nil
end

teardown do
Expand Down
2 changes: 1 addition & 1 deletion test/system/sign_up_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class SignUpTest < ApplicationSystemTestCase
setup do
@bot_token = Discord::Server.authorize_token
Discord::Server.authorize_token = 'skip'
Discord::Server.authorize_token = nil
end

teardown do
Expand Down

0 comments on commit ca54718

Please sign in to comment.