Created
October 5, 2016 13:03
-
-
Save thiagoa/15abeccd29b457a673858d428fb4a7c2 to your computer and use it in GitHub Desktop.
Twitter Timeline Hub first context refactored
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) | |
@twitter_client = twitter_client | |
end | |
def call(user, count: 20) | |
tweets = fetch_user_timeline(user, count: count) | |
Result.new(:ok, tweets) | |
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