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
# app/models/twitter_client_factory.rb | |
class TwitterClientFactory | |
def call(params) | |
Twitter::REST::Client.new do |config| | |
config.consumer_key = params['consumer_key'] | |
config.consumer_secret = params['consumer_secret'] | |
config.access_token = params['access_token'] | |
config.access_token_secret = params['access_token_secret'] | |
end | |
end |
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
context 'when config is not passed' do | |
before do | |
@consumer_key_backup = ENV['TWITTER_CONSUMER_KEY'] | |
@consumer_secret_backup = ENV['TWITTER_CONSUMER_SECRET'] | |
@access_token_backup = ENV['TWITTER_ACCESS_TOKEN'] | |
@access_token_secret_backup = ENV['TWITTER_ACCESS_TOKEN_SECRET'] | |
ENV['TWITTER_CONSUMER_KEY'] = 'ck' | |
ENV['TWITTER_CONSUMER_SECRET'] = 'cs' | |
ENV['TWITTER_ACCESS_TOKEN'] = 'at' |
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
# app/models/twitter_client_factory.rb | |
class TwitterClientFactory | |
def call(params = {}) | |
params = default_params.merge(params) | |
Twitter::REST::Client.new do |config| | |
config.consumer_key = params['consumer_key'] | |
config.consumer_secret = params['consumer_secret'] | |
config.access_token = params['access_token'] | |
config.access_token_secret = params['access_token_secret'] |
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
# spec/models/twitter_timeline_hub_spec.rb | |
require 'spec_helper' | |
require 'twitter' | |
require_relative '../../app/models/twitter_timeline_hub' | |
RSpec.describe TwitterTimelineHub do | |
describe '#call' do | |
it 'collaborates with twitter_client to get a timeline and delivers a result struct' do | |
twitter_client = instance_double(Twitter::REST::Client) | |
allow(twitter_client).to receive(:user_timeline).and_return([]) |
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 = @twitter_client.user_timeline(user, count: count) | |
Result.new(:ok, tweets) |
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
def build_tweet_double(user_name:, mention_name:, text:, created_at:) | |
user = instance_double(Twitter::User, screen_name: user_name) | |
mention = instance_double( | |
Twitter::Entity::UserMention, | |
screen_name: mention_name | |
) | |
instance_double( | |
Twitter::Tweet, | |
user: user, | |
text: text, |
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
context 'when the timeline is found' do | |
it 'returns a result object with tweets and an ok status' do | |
twitter_client = instance_double(Twitter::REST::Client) | |
tweet = build_tweet_double( | |
user_name: 'thiagoaraujos', | |
text: 'Foo @bar', | |
mention_name: 'bar', | |
created_at: Date.new(2016, 1, 1) | |
) | |
allow(twitter_client).to receive(:user_timeline).and_return([tweet]) |
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 = @twitter_client.user_timeline(user, count: count).map do |tweet| | |
{ created_at: tweet.created_at, |
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) |
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
context 'when the timeline is not found' do | |
it 'returns a result with no tweets and not_found status' do | |
twitter_client = instance_double(Twitter::REST::Client) | |
allow(twitter_client).to receive(:user_timeline) | |
.with('screen_name', count: 5) | |
.once { fail Twitter::Error::NotFound } | |
twitter_hub = TwitterTimelineHub.new(twitter_client) | |
result = twitter_hub.call('screen_name', count: 5) |