Last active
December 30, 2015 17:59
-
-
Save harlow/7864660 to your computer and use it in GitHub Desktop.
Refactoring ideas for https://github.com/JamesChevalier/hashtagged/blob/master/app.rb#L17-L27.
Extract an object. Encapsulate the interaction with the Twitter client.
Extract named methods for looping logic.
This file contains 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 'sinatra' | |
require 'twitter' | |
class App < Sinatra::Base | |
get '/' do | |
erb :index | |
end | |
post '/hashtags' do | |
@user_name = user_name | |
@hashtags = HashtagAggregator.new(user_name, twitter_client).hashtags | |
erb :hashtags | |
end | |
private | |
def user_name | |
params[:user_name] | |
end | |
def twitter_client | |
Twitter::REST::Client.new do |config| | |
config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] | |
config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] | |
config.access_token = ENV['TWITTER_ACCESS_TOKEN'] | |
config.access_token_secret = ENV['TWITTER_ACCESS_TOKEN_SECRET'] | |
end | |
end | |
end |
This file contains 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 HashtagAggregator | |
def initialize(user_name, client) | |
@user_name = user_name | |
@client = client | |
end | |
def hashtags | |
hashtag_list.uniq.map do |tag| | |
{ | |
text: tag, | |
weight: hashtag_list.count(tag), | |
link: { href: TwitterSearch.url_for(tag), target: '_blank', title: tag } | |
} | |
end | |
end | |
private | |
attr_reader :user_name, :client | |
def hashtag_list | |
@hashtag_list ||= tweets.map{ |tweet| tweet.hashtags.map(&:text) }.flatten | |
end | |
def tweets | |
client.user_timeline(user_name, { count: 200, include_entities: true }) | |
end | |
end |
This file contains 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
requrie 'spec_helper' | |
describe HastagAggregator, '#hashtags' do | |
let(:tag_text) { 'baxter' } | |
it 'returns an array of unique hashtags' do | |
user_name = 'RonBurgundy' | |
result = HastagAggregator.new(user_name, stubbed_client).hash_tags | |
expect(result).to eq( | |
[ | |
{ | |
text: tag_text, | |
weight: 1, | |
link: { href: TwitterSearch.url_for(tag_text), target: '_blank', title: tag_text } | |
} | |
] | |
) | |
end | |
def stubbed_client | |
hashtag = double(text: tag_text) | |
tweet = double(hashtags: [hashtag]) | |
client = double(user_timeline: [tweet]) | |
end | |
end |
This file contains 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 TwitterSearch | |
SEARCH_URL = 'https://twitter.com/search?q=%23' | |
def self.url_for(search_term) | |
"#{SEARCH_URL}#{search_term}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment