class BufferApi | |
BUFFER_ROOT = 'https://api.bufferapp.com/1/' | |
def initialize(options) | |
@token = options[:token] || ENV.fetch('BUFFER_TOKEN') | |
@profile_ids = options.fetch(:profile_ids) | |
@client = RestClient | |
end | |
def queue(content) | |
post_request('updates/create.json', content) | |
end | |
def send_now(content) | |
content.merge!(now: true) | |
post_request('updates/create.json', content) | |
end | |
private | |
def post_request(end_point, content) | |
@client.post url(end_point), default_params.merge(content) | |
end | |
def url(end_point) | |
BUFFER_ROOT + end_point | |
end | |
def default_params | |
{ profile_ids: @profile_ids, access_token: @token } | |
end | |
end |
class TwitterBot::Base | |
class << self | |
def send_tweet(text:, image_url: nil, queue: true) | |
new.send_tweet(text: text, image_url: image_url, queue: queue) | |
end | |
end | |
def send_tweet(text:, image_url: nil, queue: true) | |
return unless send_tweets? | |
leave_space = :one_attachment if image_url.present? | |
message = TwitterMessage.new(text, leave_space: leave_space) | |
media_hash = image_url.present? ? { photo: image_url } : nil | |
if queue | |
buffer_client.queue(text: message.to_tweet, media: media_hash) | |
else | |
buffer_client.send_now(text: message.to_tweet, media: media_hash) | |
end | |
end | |
private | |
def send_tweets? | |
Rails.configuration.x.send_tweets.present? | |
end | |
def buffer_client | |
@buffer_client ||= BufferApi.new(profile_ids: [buffer_account_id]) | |
end | |
def buffer_account_id | |
fail NotImplementedError, 'You must implement buffer_account_id in each kind of Twitter Bot' | |
end | |
end |
class TwitterBot::ProductHuntHi < TwitterBot::Base | |
private | |
def buffer_account_id | |
ENV.fetch('BUFFER_PRODUCT_HUNT_HI_ID') | |
end | |
end |
MESSAGE_TEMPLATES = [ | |
'Woohoo! %s was upvoted for the 100th time. 🙌', | |
'%s was upvoted 100 times. Mom & dad would be proud 😄', | |
# … many many more | |
] | |
MESSAGE_GIFS = %w( | |
batman-thumbs-up.gif | |
dance-and-shake.gif | |
# … many many more | |
) | |
tweet = ['@' + maker.twitter_username, | |
format(MESSAGE_TEMPLATES.sample, post.name), | |
Routes.post_url(post)] | |
TwitterBot::ProductHuntHi.send_tweet(text: tweet, image_url: S3Helper.image_url(MESSAGE_GIFS.sample), queue: false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment