Skip to content

Instantly share code, notes, and snippets.

@kalleth
Last active August 29, 2015 14:25
Show Gist options
  • Save kalleth/996eac7b6b6e89f4704a to your computer and use it in GitHub Desktop.
Save kalleth/996eac7b6b6e89f4704a to your computer and use it in GitHub Desktop.
require 'date'
require 'twitter'
require 'dotenv'
Dotenv.load
class TwitterSearch
def fetch(query)
client.search("#{query} since:#{Date.today}").collect do |tweet|
Tweet.new(tweet)
end
end
private
def client
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
end
end
class Tweet
def initialize(tweet)
@tweet = tweet
end
def id
@tweet.id
end
def to_s
"(#{@tweet.id}) #{@tweet.user.screen_name}: #{@tweet.text}"
end
end
class PrizeDraw
def initialize(tweets)
@tweets = tweets
end
def pick_winner
winning_tweet = tweets.sample
puts "TWEETS:\n"
puts tweets
puts "Winning tweet out of #{tweets.length}:\n"
puts winning_tweet
puts "https://twitter.com/statuses/#{winning_tweet.id}"
end
private
attr_reader :tweets
end
tweets = TwitterSearch.new.fetch('#ubxdwinner')
PrizeDraw.new(tweets).pick_winner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment