Created
October 5, 2016 13:24
-
-
Save thiagoa/057881cb2314912d1cd641bad028a62c to your computer and use it in GitHub Desktop.
Twitter Timeline Hub final
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TwitterTimelineHub | |
Result = Struct.new(:status, :tweets) | |
def initialize(twitter_client = TwitterClientFactory.new.call) | |
@twitter_client = twitter_client | |
end | |
def call(user, count: 20) | |
tweets = fetch_user_timeline(user, count: count) | |
Result.new(:ok, tweets) | |
rescue Twitter::Error::NotFound | |
Result.new(:not_found, []) | |
rescue Twitter::Error::Unauthorized => e | |
raise if e.message =~ /Invalid or expired token/ | |
Result.new(:forbidden, []) | |
end | |
private | |
def fetch_user_timeline(*args) | |
@twitter_client.user_timeline(*args).map do |tweet| | |
filter_tweet(tweet) | |
end | |
end | |
def filter_tweet(tweet) | |
{ created_at: tweet.created_at, | |
screen_name: tweet.user.screen_name, | |
text: tweet.text, | |
mentions: extract_mentions(tweet) } | |
end | |
def extract_mentions(tweet) | |
tweet.user_mentions.map(&:screen_name) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment