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_timeline_hub.rb
Last active December 12, 2016 16:41
Twitter Timeline Hub call method rescuing not found exception
def call(user, count: 20)
tweets = fetch_user_timeline(user, count: count)
Result.new(:ok, tweets)
rescue Twitter::Error::NotFound
Result.new(:not_found, [])
end
@thiagoa
thiagoa / when_the_timeline_is_forbidden.rb
Last active October 10, 2016 16:47
Twitter timeline hub spec when the timeline is forbidden
context 'when the timeline is forbidden' do
it 'returns a result with no tweets and forbidden status' do
twitter_client = double('twitter_client')
allow(twitter_client).to receive(:user_timeline)
.with('screen_name', count: 5)
.once { fail Twitter::Error::Unauthorized }
twitter_timeline_hub = TwitterTimelineHub.new(twitter_client)
result = twitter_timeline_hub.call('screen_name', count: 5)
@thiagoa
thiagoa / twitter_timeline_hub.rb
Created October 5, 2016 13:15
Twitter Timeline Hub call method rescuing forbidden exception
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
Result.new(:forbidden, [])
end
@thiagoa
thiagoa / when_unauthorized_errors_refers_to_invalid_token.rb
Created October 5, 2016 13:23
When unauthorized error refers to an invalid token
context 'when the timeline is unauthorized' do
# previous context here
context 'when the unauthorized error refers to an invalid token' do
it 'fails' do
twitter_client = instance_double(Twitter::REST::Client)
allow(twitter_client).to receive(:user_timeline)
.with('screen_name', count: 5)
.once {
fail Twitter::Error::Unauthorized, 'Invalid or expired token'
@thiagoa
thiagoa / twitter_timeline_hub.rb
Created October 5, 2016 13:24
Twitter Timeline Hub final
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)
@thiagoa
thiagoa / home_controller.rb
Created October 5, 2016 13:52
Home controller
# app/controllers/home_controller.rb
class HomeController < ApplicationController
end
@thiagoa
thiagoa / twitter_timeline_controller.rb
Created October 10, 2016 15:32
Empty Twitter Timeline Controller
# app/controllers/twitter_timeline_controller.rb
class TwitterTimelineController < ApplicationController
end
@thiagoa
thiagoa / outlineOfFrontendIntegrationSpec.js
Last active November 4, 2016 14:28
The outline of a frontend integration spec
import { expect } from 'chai';
import createFixture from '../support/createFixture';
describe('renderCartTotal', () => {
// Declare shared environment variables
let fixture;
beforeEach(() => {
// Setup the environment
fixture = createFixture({ html: '<div data-js-cart-total></div>' });
@thiagoa
thiagoa / renderCartTotal.js
Last active November 4, 2016 14:27
Example function to render a cart's total on the screen
import { expect } from 'chai';
import createFixture from '../support/createFixture';
function renderCartTotal({ domNode, itemPrices }) {
const sum = itemPrices.reduce((total, price) => total + price, 0);
const formattedPrice = `$${sum}`;
domNode.innerHTML = formattedPrice;
}
// spec/javascripts/support/createFixture.js
function createFixtureNode() {
const fixture = document.createElement('div');
fixture.id = 'fixture';
fixture.destroy = function destroy() {
this.parentNode.removeChild(fixture);
};