Created
April 4, 2018 16:07
-
-
Save kopylovvlad/b7310a1d4039b55ea8bafc1590e2c467 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogHandler | |
attr_reader :successor | |
def initialize(successor = nil) | |
@successor = successor | |
end | |
def process(log_item) | |
if accept(log_item) | |
return true | |
elsif @successor | |
@successor.process(log_item) | |
else | |
fail(log_item) | |
end | |
end | |
private | |
def fail(log_item) | |
msg = "The log-item '#{log_item}' could not be handled." | |
save_to_file(msg, 'LogHandler_erros.log') | |
end | |
def accept(log_item) | |
raise '#accept_request method must be implemented.' | |
end | |
def save_to_file(log_item, file_name) | |
File.open(file_name, 'a') do |f| | |
f.write(log_item) | |
f.write("\n") | |
end | |
end | |
end | |
class StandartLogHandler < LogHandler | |
def accept(log_item) | |
if log_item[:http_status] =~ /^2\d\d$/ | |
puts 'StandartLogHandler\'s accept' | |
save_to_file(log_item, 'production.log') | |
return true | |
else | |
return false | |
end | |
end | |
end | |
class PayLogHandler < LogHandler | |
def accept(log_item) | |
if valid_item?(log_item) | |
puts 'PayLogHandler\'s accept' | |
save_to_file(log_item, 'production_payments.log') | |
return true | |
else | |
return false | |
end | |
end | |
private | |
def valid_item?(log_item) | |
(log_item[:http_status] =~ /^2\d\d$/ && | |
log_item[:http_method]=='POST' && | |
log_item[:processing][:by]=='PayController#create') | |
end | |
end | |
class ClientErrorLogHandler < LogHandler | |
def accept(log_item) | |
if log_item[:http_status] =~ /^4\d\d$/ | |
puts 'ClientErrorLogHandler\'s accept' | |
save_to_file(log_item, 'production4xx.log') | |
send_to_telegram(log_item) | |
return true | |
else | |
return false | |
end | |
end | |
private | |
def send_to_telegram(log_item) | |
# ::TelegramSender.notify_about4xx(log_item) | |
end | |
end | |
class ServerErrorLogHandler < LogHandler | |
def accept(log_item) | |
if log_item[:http_status] =~ /^5\d\d$/ | |
puts 'ServerErrorLogHandler\'s accept' | |
save_to_file(log_item, 'production5xx.log') | |
send_to_slack(log_item) | |
return true | |
else | |
return false | |
end | |
end | |
private | |
def send_to_slack(log_item) | |
# ::SlackSender.notify_about5xx(log_item) | |
end | |
end | |
chain_of_responsibility = ServerErrorLogHandler.new( | |
ClientErrorLogHandler.new( | |
PayLogHandler.new( | |
StandartLogHandler.new | |
) | |
) | |
) | |
chain_of_responsibility.process(example_200) | |
# StandartLogHandler's accept | |
chain_of_responsibility.process(example_payment) | |
# PayLogHandler's accept | |
chain_of_responsibility.process(example_404) | |
# ClientErrorLogHandler's accept | |
chain_of_responsibility.process(example_500) | |
# ServerErrorLogHandler's accept |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment