Skip to content

Instantly share code, notes, and snippets.

@oguzcanhuner
Created August 19, 2015 14:28
Show Gist options
  • Save oguzcanhuner/bb9d1258ba095e2b3977 to your computer and use it in GitHub Desktop.
Save oguzcanhuner/bb9d1258ba095e2b3977 to your computer and use it in GitHub Desktop.
require 'dotenv'
Dotenv.load
require 'twitter'
require 'bitly'
class TwitterJob
def perform
stream = Twitter::Streaming::Client.new do |config|
config.consumer_key = ENV['CONSUMER_KEY']
config.consumer_secret = ENV['CONSUMER_SECRET']
config.access_token = ENV['ACCESS_TOKEN']
config.access_token_secret = ENV['ACCESS_TOKEN_SECRET']
end
client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['CONSUMER_KEY']
config.consumer_secret = ENV['CONSUMER_SECRET']
config.access_token = ENV['ACCESS_TOKEN']
config.access_token_secret = ENV['ACCESS_TOKEN_SECRET']
end
Bitly.use_api_version_3
Bitly.configure do |config|
config.api_version = 3
config.access_token = ENV['BITLY_KEY']
end
stream.filter(track: "#showmeastory") do |tweet|
user = tweet.user.screen_name
tweet_parts = tweet.text.split(" ")
if tweet_parts.include?("boy")
gender = "boy"
elsif tweet_parts.include?("girl")
gender = "girl"
end
# the name will always come after the word 'called'
if tweet_parts.include?("called")
i = tweet_parts.index("called")
name = tweet_parts[i+1]
if !gender.nil? && !name.nil?
url = "https://www.lostmy.name/products/lostmy-name?name=#{name}&gender=#{gender}&utm_source=twitter&utm_medium=social&utm_term=showmeastory&utm_campaign=madwizard"
url = Bitly.client.shorten(url).short_url
res = "@#{user} Blazzowooosh! Here’s your story all about a #{gender} called #{name.capitalize}: #{url}"
client.update(res, in_reply_to_status: tweet)
else
message = "@#{user} Ah, the spell exploded! Check the words and try again."
client.update(message, in_reply_to_status: tweet)
end
else
message = "@#{user} Ah, the spell exploded! Check the words and try again."
client.update(message, in_reply_to_status: tweet)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment