-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalert_in_ua_2_twitter.rb
62 lines (50 loc) · 1.73 KB
/
alert_in_ua_2_twitter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require './alert_in_ua_2_twitter/version'
require './alert_in_ua_2_twitter/db'
require './alert_in_ua_2_twitter/alert_retriever'
require './alert_in_ua_2_twitter/alert_image_retriever'
require './alert_in_ua_2_twitter/translation'
require './alert_in_ua_2_twitter/tweeter'
API_TOKEN = ENV['ALERTS_IN_UA_TOKEN']
TWITTER_CONSUMER_KEY = ENV['TWITTER_CONSUMER_KEY']
TWITTER_CONSUMER_SECRET = ENV['TWITTER_CONSUMER_SECRET']
TWITTER_ACCESS_TOKEN = ENV['TWITTER_ACCESS_TOKEN']
TWITTER_ACCESS_SECRET = ENV['TWITTER_ACCESS_SECRET']
SNITCH_URL = ENV['SNITCH_URL']
class AlertInUa2Twitter
def initialize(lang = :ja)
MiniI18n.locale = lang
end
def alert
started_alerts, active_alerts, terminated_alerts = AlertInUa2Twitter::AlertRetriever.new.get
image_path = AlertInUa2Twitter::AlertImageRetriever.new.get if started_alerts.any? || terminated_alerts.any?
format_and_notify(started_alerts, :started, image_path) if started_alerts.any?
format_and_notify(terminated_alerts, :finished) if terminated_alerts.any?
end
private
def format_and_notify(alerts, kind, image = nil)
m = []
now = Time.now.getlocal('+09:00')
alerts.each do |a|
m << (MiniI18n.t(a.location_title.sub('.', '')) || a.location_title)
end
message = [
MiniI18n.t(kind),
m.sort.join(MiniI18n.t(:comma)),
MiniI18n.t(:period),
MiniI18n.l(now, format: :short)
].join
tweet_message(message, image)
end
def tweeter
@tweeter ||= AlertInUa2Twitter::Tweeter.new(
TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET,
TWITTER_ACCESS_TOKEN,
TWITTER_ACCESS_SECRET
)
end
def tweet_message(message, image_path = nil)
puts "Tweeting: #{message}"
tweeter.create_tweet(message, image_path)
end
end