Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 ロガー出力を concern などとともに実装した
Browse files Browse the repository at this point in the history
nikukyugamer committed Jun 6, 2021
1 parent 05b170c commit d5172c3
Showing 6 changed files with 68 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ TWEET_STORAGE_PASSWORD=

BUGSNAG_API_KEY=
ROLLBAR_TOKEN=
APP_NAME_FOR_LOGGER=

RECOMMENDED_QUOTES_WORKSHEET_ID=
RECOMMENDED_QUOTES_SHEET_NAME=
Empty file removed app/controllers/concerns/.keep
Empty file.
40 changes: 40 additions & 0 deletions app/controllers/concerns/logger_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# TODO: 命名を再考
module LoggerMethods
extend ActiveSupport::Concern

module ModuleMethods
def convert_hash_to_json(options={})
{
app_name: ENV['APP_NAME_FOR_LOGGER'] || options[:app_name] || '',
message: options[:message] || '',
time: Time.zone.now.to_s
}.merge(options).to_json
end

def convert_tweet_objects_to_array(tweets)
converted_array_from_tweet_objects = []

# 長すぎると Papertrail が記録してくれない
tweets.each do |tweet|
inserted_hash = {
id_number: tweet.id_number,
# user_name: tweet.user.name,
screen_name: tweet.user.screen_name,
# full_text: tweet.full_text,
# is_retweet: tweet.is_retweet,
url: tweet.url,
tweeted_at: tweet.tweeted_at,
# media_exists: tweet.has_assets?,
# is_public: tweet.is_public?,
# is_mentioned_to_gensosenkyo_admin: tweet.is_mentioned_to_gensosenkyo_admin?
}

converted_array_from_tweet_objects << inserted_hash
end

converted_array_from_tweet_objects
end
end

extend ModuleMethods
end
18 changes: 15 additions & 3 deletions app/lib/google_sheet_api/write_to_response_api_sheet_by_hashtag.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@

module GoogleSheetApi
class WriteToResponseApiSheetByHashtag
include LoggerMethods

def initialize(spreadsheet_id: nil, sheet_name: nil)
@client = GoogleSheetApi::Client.new.create
@spreadsheet_id = spreadsheet_id || ENV['RECOMMENDED_QUOTES_WORKSHEET_ID']
@@ -16,10 +18,20 @@ def initialize(spreadsheet_id: nil, sheet_name: nil)
end

def execute(hashtag, options={})
update_data(target_tweets(hashtag, options))
update_target_tweets = target_tweets(hashtag, options)

Rails.logger.info(
LoggerMethods.convert_hash_to_json(
hashtag: hashtag,
message: 'update_target_tweets',
tweets: LoggerMethods.convert_tweet_objects_to_array(update_target_tweets)
)
)

update_data(update_target_tweets)
rescue StandardError => e
Rails.logger.fatal 'FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute'
Rails.logger.fatal e if e.present?
Rails.logger.fatal(LoggerMethods.convert_hash_to_json(message: 'FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute'))
Rails.logger.fatal(LoggerMethods.convert_hash_to_json(message: e)) if e.present?

bugsnag_error_message = "FATAL エラーです: GoogleSheetApi::GoogleSheetApi#execute / #{e}"
Bugsnag.notify(bugsnag_error_message)
10 changes: 10 additions & 0 deletions app/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -81,4 +81,14 @@ def has_assets?
def has_in_tweet_urls?
in_tweet_urls.present?
end

def is_mentioned_to_gensosenkyo_admin?
# gensosenkyo: 1471724029, sub_gensosenkyo: 1388758231825018881
gensosenkyo_admin_user_id_numbers = {
'gensosenkyo': 1471724029,
'sub_gensosenkyo': 1388758231825018881
}

mentions.any? { |mention| mention.user_id_number.in?(gensosenkyo_admin_user_id_numbers.values) }
end
end
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -29,5 +29,7 @@ class Application < Rails::Application
config.load_defaults 6.1
config.time_zone = 'Tokyo'
config.api_only = true

config.logger = RemoteSyslogLogger.new('logs.papertrailapp.com', 15515)
end
end

0 comments on commit d5172c3

Please sign in to comment.