Skip to content

Commit

Permalink
MailGenerator: 設定可能な生成器にする
Browse files Browse the repository at this point in the history
refs #71
  • Loading branch information
ochaochaocha3 committed Jan 8, 2018
1 parent 0a801e1 commit 1d7cb2e
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 55 deletions.
4 changes: 4 additions & 0 deletions lib/rgrb/plugin/server_connection_report/generator.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# vim: fileencoding=utf-8

require 'rgrb/plugin/configurable_generator'

module RGRB
module Plugin
# サーバーリレー監視プラグイン
module ServerConnectionReport
# ServerConnectionReport の出力テキスト生成器
class Generator
include ConfigurableGenerator

# サーバがネットワークに参加した際のメッセージを返す
# @param [String] server サーバ名
# @param [String] message メッセージ
Expand Down
57 changes: 43 additions & 14 deletions lib/rgrb/plugin/server_connection_report/mail_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# frozen_string_literal: true

require 'rgrb/version'
require 'rgrb/plugin/configurable_generator'

require 'stringio'
require 'mail'
Expand All @@ -13,6 +14,8 @@ module Plugin
module ServerConnectionReport
# メール生成を司るクラス
class MailGenerator
include ConfigurableGenerator

# メールテンプレートの読み込みに失敗した際に発生するエラー
class MailTemplateLoadError < StandardError; end

Expand All @@ -37,30 +40,48 @@ class MailTemplateLoadError < StandardError; end
# @return [String]
attr_reader :to

# 送信データを初期化する
# @param [Hash] config 設定
# @param [Object?] logger ロガー
# @option [String] to 送信先
# @option [Hash] smtp 送信に利用するSMTPサーバーの設定
def initialize(config, logger = nil)
# メール生成器を初期化する
def initialize(*)
super

@subject = ''
@body = ''

@irc_host = ''
@irc_nick = ''
@irc_network = ''

smtp_config = config['SMTP']
if smtp_config
@mail_config = symbolize_keys(smtp_config).
reject { |_, value| value.nil? }
@mail_config = {}
@to = 'root@localhost'

@logger =
Lumberjack::Logger.new($stdout, progname: self.class.to_s)
end

# 設定データを解釈してプラグインの設定を行う
# @param [Hash] config_data 設定データのハッシュ
# @return [self]
def configure(config_data)
mail_config = config_data['Mail']
if mail_config
smtp_config = mail_config['SMTP']
if smtp_config
@mail_config = symbolize_keys(smtp_config).
reject { |_, value| value.nil? }
end

to_config = mail_config['To']
if to_config
@to = to_config
end
end

@to = config['To'] || 'root@localhost'
logger = config_data[:logger]
if logger
@logger = logger
end

@logger = logger || Lumberjack::Logger.new(
$stdout, progname: self.class.to_s
)
self
end

# メールのテンプレートを読み込む
Expand Down Expand Up @@ -110,6 +131,14 @@ def load_mail_template_file(path)
self
end

# 指定された名前のメールテンプレートファイルを読み込む
# @param [String] name テンプレート名。これに .txt が付加されて読み込まれる。
# @return [self]
# @raise [MailTemplateLoadError] 読み込めなかった場合に発生する
def load_mail_template_by_name(name)
load_mail_template_file("#{@data_path}/#{name}.txt")
end

STATUS_1 = {
joined: 'に参加し',
disconnected: 'から切断され'
Expand Down
87 changes: 46 additions & 41 deletions spec/rgrb/plugin/server_connection_report/mail_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,26 @@
let(:template_cre_path) { test_data_file_path['cre.txt'] }

let(:null_logger) { Lumberjack::Logger.new('/dev/null') }
let(:smtp_config) {

let(:config_data) {
{
'SMTP' => {
'address' => 'localhost',
'port' => 25,
'domain' => 'smtp.example.net',
'authentication' => false,
'ssl' => false,
'enable_starttls_auto' => false
'Mail' => {
'To' => ['admin@example.net'],
'SMTP' => {
'address' => 'localhost',
'port' => 25,
'domain' => 'smtp.example.net',
'authentication' => false,
'ssl' => false,
'enable_starttls_auto' => false
}
}
}
}
let(:mail_generator) { described_class.new(smtp_config, null_logger) }

let(:mail_generator) {
described_class.new.configure(logger: null_logger)
}

describe '#initialize' do
it 'インスタンスを初期化することができる' do
Expand All @@ -54,68 +61,66 @@
expect(mail_generator.irc_network).to eq('')
end

it 'to を正しく設定する' do
expect(mail_generator.to).to eq('root@localhost')
end
end

describe '#configure' do
let(:configured_mail_generator) {
mail_generator.configure(config_data)
}

describe '@to' do
context '指定されていなかった場合' do
it 'to を正しい既定値に設定する' do
expect(mail_generator.to).to eq('root@localhost')
it 'to が設定されない' do
config_data['Mail']['To'] = nil
expect(configured_mail_generator.to).to eq('root@localhost')
end
end

context '指定されていた場合' do
let(:mail_address) { 'someone@example.net' }
let(:config) {
{ 'To' => mail_address }
}

let(:generator) { described_class.new(config, null_logger) }

it 'to を設定する' do
expect(generator.to).to eq(mail_address)
expect(configured_mail_generator.to).to eq(['admin@example.net'])
end
end
end

describe '@mail_config' do
let(:mail_config_hash_1) {
YAML.load(<<-YAML)
SMTP:
address: localhost
port: 25
YAML
}

let(:expected_1) {
let(:expected_symbol_keys) {
{
address: 'localhost',
port: 25
port: 25,
domain: 'smtp.example.net',
authentication: false,
ssl: false,
enable_starttls_auto: false
}
}

let(:mail_config_hash_2) {
let(:smtp_config_with_nil_value) {
YAML.load(<<-YAML)
SMTP:
authentication: false
# YAMLではnilではなくnull
invalid_key: null
authentication: false
# YAMLではnilではなくnull
invalid_key: null
YAML
}

let(:expected_2) {
let(:expected_rejected_nil_value) {
{
authentication: false
}
}

it 'キーを文字列からシンボルに変換する' do
mail_generator_2 = described_class.new(mail_config_hash_1, null_logger)
expect(mail_generator_2.instance_variable_get(:@mail_config)).
to eq(expected_1)
expect(configured_mail_generator.instance_variable_get(:@mail_config)).
to eq(expected_symbol_keys)
end

it 'nullの項目を除く' do
mail_generator_2 = described_class.new(mail_config_hash_2, null_logger)
expect(mail_generator_2.instance_variable_get(:@mail_config)).
to eq(expected_2)
config_data['Mail']['SMTP'] = smtp_config_with_nil_value
expect(configured_mail_generator.instance_variable_get(:@mail_config)).
to eq(expected_rejected_nil_value)
end
end
end
Expand Down

0 comments on commit 1d7cb2e

Please sign in to comment.