-
Notifications
You must be signed in to change notification settings - Fork 7
/
twitter.rb
58 lines (49 loc) · 1.53 KB
/
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
require 'twitter'
class FakeTwitter
def initialize(logger)
@logger = logger
end
def update(str)
@logger.info "[FakeTwitter]: #{str}"
end
end
class TwitterClient
def initialize(logger)
@logger = logger
@twitter = if ENV['ENVIRONMENT'] != 'test' && env_keys_exist?
Twitter::REST::Client.new do |config|
config.consumer_key = ENV['TWITTER_CONSUMER_KEY']
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET']
config.access_token = ENV['TWITTER_ACCESS_TOKEN']
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
else
FakeTwitter.new(logger)
end
end
def env_keys_exist?
ENV['TWITTER_CONSUMER_KEY'] &&
ENV['TWITTER_CONSUMER_SECRET'] &&
ENV['TWITTER_ACCESS_TOKEN'] &&
ENV['TWITTER_ACCESS_TOKEN_SECRET']
end
def tweet(clinic)
@logger.info "[TwitterClient] Sending tweet for #{clinic.title} (#{clinic.new_appointments} new appointments)"
text = clinic.twitter_text
if text.is_a?(Array)
text.each { |t| @twitter.update(t) }
else
@twitter.update(text)
end
rescue => e
@logger.error "[TwitterClient] error: #{e}"
raise e unless ENV['ENVIRONMENT'] == 'production' || ENV['ENVIRONMENT'] == 'staging'
Sentry.capture_exception(e)
end
def post(clinics)
clinics.filter(&:should_tweet?).each do |clinic|
tweet(clinic)
clinic.save_tweet_time
end
end
end