Skip to content

Instantly share code, notes, and snippets.

View thiagoa's full-sized avatar

Thiago Araújo Silva thiagoa

  • thoughtbot
  • Natal / RN - Brazil
View GitHub Profile
@thiagoa
thiagoa / twitter_client_factory.rb
Last active October 4, 2016 11:28
First twitter client factory
# 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
@thiagoa
thiagoa / twitter_client_factory_spec.rb
Last active November 7, 2016 20:36
Twitter client factory second spec
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'
@thiagoa
thiagoa / twitter_client_factory.rb
Last active October 10, 2016 16:11
Twitter client factory
# 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']
@thiagoa
thiagoa / twitter_timeline_hub_spec.rb
Last active October 10, 2016 16:32
First version of twitter timeline hub spec
# 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([])
@thiagoa
thiagoa / twitter_timeline_hub.rb
Created October 5, 2016 12:18
Twitter timeline hub
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)
@thiagoa
thiagoa / build_tweet_double.rb
Created October 5, 2016 12:39
Build tweet double
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,
@thiagoa
thiagoa / when_the_timeline_is_found.rb
Created October 5, 2016 12:54
TwitterTimelineHub - When the timeline is found
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])
@thiagoa
thiagoa / twitter_timeline_hub.rb
Created October 5, 2016 13:00
TwitterTimelineHub first context
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,
@thiagoa
thiagoa / twitter_timeline_hub.rb
Created October 5, 2016 13:03
Twitter Timeline Hub first context refactored
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)
@thiagoa
thiagoa / twitter_timeline_hub_spec.rb
Created October 5, 2016 13:09
When the timeline is not found
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)