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/views/home/index.html.erb --> | |
<div data-twitter-app> | |
<div data-tweets> | |
</div> | |
</div> |
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/features/twitter_timeline_spec.rb | |
require 'feature_helper' | |
RSpec.feature 'twitter timeline' do | |
scenario 'a user views a twitter timeline' do | |
pending | |
visit root_path | |
within '[data-twitter-app]' do |
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 stub_timeline(id:, returns:) | |
instance = instance_double(TwitterTimelineHub) | |
allow(instance).to receive(:call).with(id).once.and_return(returns) | |
allow(TwitterTimelineHub).to receive(:new).once.and_return(instance) | |
end | |
def stub_result(**attrs) | |
instance_double(TwitterTimelineHub::Result, **attrs) | |
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
# app/models/twitter_timeline_hub.rb | |
class TwitterTimelineHub | |
Result = Struct.new(:tweets, :status) | |
def call(id) | |
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
require 'rails_helper' | |
RSpec.describe TwitterTimelineController, type: :request do | |
def stub_timeline(id:, returns:) | |
instance = instance_double(TwitterTimelineHub) | |
allow(instance).to receive(:call).with(id).once.and_return(returns) | |
allow(TwitterTimelineHub).to receive(:new).once.and_return(instance) | |
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
result = stub_result( | |
status: :ok, | |
tweets: [ | |
‘screen_name’ => ‘joe’, | |
‘text’ => ‘Hi @bob, I am Joe!’, | |
‘mentions’ => [‘bob’], | |
‘created_at’ => Time.new(2016, 1, 1) | |
] | |
) |
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
twitter_timeline_hub = TwitterTimelineHub.new | |
# returns a struct with results, which include Tweets | |
result = twitter_timeline_hub.call(‘thiagoaraujos’) |
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
# config/routes.rb | |
Rails.application.routes.draw do | |
root to: 'home#index' | |
resources :twitter_timeline, only: %i(show) | |
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
# app/controllers/twitter_timeline_controller.rb | |
class TwitterTimelineController < ApplicationController | |
def show | |
twitter_timeline_hub = TwitterTimelineHub.new | |
result = twitter_timeline_hub.call(params[:id]) | |
render json: { tweets: result.tweets, status: result.status } | |
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
# spec/models/twitter_client_factory_spec.rb | |
require 'twitter' | |
require 'spec_helper' | |
require_relative '../../app/models/twitter_client_factory' | |
RSpec.describe TwitterClientFactory do | |
describe '#call' do | |
it 'builds a twitter client class with the passed config' do | |
instance = TwitterClientFactory.new.call( | |
'consumer_key' => 'ck', |