diff options
-rw-r--r-- | Gemfile | 1 | ||||
-rw-r--r-- | Gemfile.lock | 6 | ||||
-rw-r--r-- | app/controllers/sessions_controller.rb | 8 | ||||
-rw-r--r-- | app/models/account.rb | 21 | ||||
-rw-r--r-- | app/models/user.rb | 36 | ||||
-rw-r--r-- | spec/models/account_spec.rb | 54 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 13 | ||||
-rw-r--r-- | spec/spec_helper.rb | 1 |
8 files changed, 51 insertions, 89 deletions
@@ -32,5 +32,6 @@ group :development, :test do gem 'rspec' gem 'rspec-rails' gem 'factory_girl_rails'#, :require => 'factory_girl' + gem 'webmock' end diff --git a/Gemfile.lock b/Gemfile.lock index c10eab9..9c15a2d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -33,6 +33,7 @@ GEM multi_json (~> 1.3) thread_safe (~> 0.1) tzinfo (~> 0.3.33) + addressable (2.3.3) after_commit_action (0.1.3) activerecord (>= 3.0.0) arel (4.0.0.beta2) @@ -49,6 +50,7 @@ GEM coffee-script-source (1.6.2) counter_culture (0.1.11) after_commit_action (~> 0.1.3) + crack (0.3.2) daemon-spawn (0.4.2) daemons (1.1.9) dalli (2.6.2) @@ -182,6 +184,9 @@ GEM kgio (~> 2.6) rack raindrops (~> 0.7) + webmock (1.11.0) + addressable (>= 2.2.7) + crack (>= 0.3.2) yajl-ruby (1.1.0) PLATFORMS @@ -211,4 +216,5 @@ DEPENDENCIES twitter uglifier (>= 1.0.3) unicorn + webmock yajl-ruby diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 6ea6aaf..74577ec 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -4,10 +4,10 @@ class SessionsController < ApplicationController def callback auth = request.env["omniauth.auth"] - account = Account.register_or_update(user_id: auth["uid"], - oauth_token: auth["credentials"]["token"], - oauth_token_secret: auth["credentials"]["secret"], - consumer_version: Settings.consumer_version) + account = Account.create_or_update(user_id: auth["uid"], + oauth_token: auth["credentials"]["token"], + oauth_token_secret: auth["credentials"]["secret"], + consumer_version: Settings.consumer_version) session[:account] = account session[:user_id] = account.user_id diff --git a/app/models/account.rb b/app/models/account.rb index faeaadc..8a7fb9d 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,5 +1,5 @@ class Account < ActiveRecord::Base - def self.register_or_update(hash) + def self.create_or_update(hash) account = where(user_id: hash[:user_id]).first_or_initialize account.oauth_token = hash[:oauth_token] account.oauth_token_secret = hash[:oauth_token_secret] @@ -20,13 +20,6 @@ class Account < ActiveRecord::Base oauth_token_secret: oauth_token_secret) end - def twitter_user(uid = nil) - uid ||= user_id - Rails.cache.fetch("twitter_user/#{uid}", expires_in: 1.hour) do - client.user(uid) rescue nil - end - end - def import_favorites(id) result = client.status_activity(id) @@ -43,16 +36,4 @@ class Account < ActiveRecord::Base Retweet.from_tweet_object(status) end end - - def stats_api - return {} unless twitter_user - { - favorites_count: twitter_user.favourites_count, - listed_count: twitter_user.listed_count, - followers_count: twitter_user.followers_count, - tweets_count: twitter_user.statuses_count, - friends_count: twitter_user.friends_count, - bio: twitter_user.description - } - end end diff --git a/app/models/user.rb b/app/models/user.rb index 262a7e5..d01dec8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -86,22 +86,36 @@ class User < ActiveRecord::Base end end - def stats - @stats_cache ||= ->{ + def stats(include_stats_api = false) + @stats_cache ||= begin raise Aclog::Exceptions::UserNotRegistered unless account - tweets.inject( - {favorites_count: favorites.count, - retweets_count: retweets.count, - tweets_count: tweets.length, # cache: tweets.inject calls "SELECT `tweets`.*" - favorited_count: 0, - retweeted_count: 0, - stats_api: account.stats_api} - ) do |hash, m| + hash = {favorites_count: favorites.count, + retweets_count: retweets.count, + tweets_count: tweets.length, # cache: tweets.inject calls "SELECT `tweets`.*" + favorited_count: 0, + retweeted_count: 0} + + if include_stats_api + twitter_user = account.client.user + if twitter_user + h = { + favorites_count: twitter_user.favourites_count, + listed_count: twitter_user.listed_count, + followers_count: twitter_user.followers_count, + tweets_count: twitter_user.statuses_count, + friends_count: twitter_user.friends_count, + bio: twitter_user.description + } + end + hash[:stats_api] = h + end + + tweets.inject(hash) do |hash, m| hash[:favorited_count] += m.favorites_count hash[:retweeted_count] += m.retweets_count hash end - }.call + end end end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index a86cbf9..4984b94 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -2,10 +2,10 @@ require 'spec_helper' describe Account do - describe ".register_or_update" do + describe ".create_or_update" do context "when not recorded" do let(:account) { FactoryGirl.build(:account_1) } - subject { Account.register_or_update(account.attributes.symbolize_keys) } + subject { Account.create_or_update(account.attributes.symbolize_keys) } its(:user_id) { should be account.user_id } its(:oauth_token) { should eq account.oauth_token } its(:oauth_token_secret) { should eq account.oauth_token_secret } @@ -15,7 +15,7 @@ describe Account do context "when already recorded" do let(:old_account) { FactoryGirl.create(:account_1) } let(:new_account) { FactoryGirl.build(:account_2) } - subject { Account.register_or_update(new_account.attributes.symbolize_keys) } + subject { Account.create_or_update(new_account.attributes.symbolize_keys) } its(:id) { should be old_account.id } its(:user_id) { should be old_account.user_id } its(:user_id) { should be new_account.user_id } @@ -46,55 +46,7 @@ describe Account do it { should be_a Twitter::Client } end - describe "#twitter_user" do - let(:account) { FactoryGirl.create(:account_1) } - let(:user) { FactoryGirl.create(user_fixture) } - subject { account.twitter_user(user.id) } - - context "when exist" do - let(:user_fixture) { :user_exists } - its(:id) { should be user.id } - its(:screen_name) { should eq user.screen_name } - end - - context "when not exist" do - let(:user_fixture) { :user_not_exists } - it { should be nil } - end - - context "when suspended" do - let(:user_fixture) { :user_suspended } - it { should be nil } - end - - context "when no parameter" do - let(:user_fixture) { :user_1 } - before { user } - subject { account.twitter_user } - its(:id) { should be user.id } - its(:screen_name) { should eq user.screen_name } - end - end - describe "#import_favorites" do # TODO end - - describe "#stats_api" do - let(:account) { FactoryGirl.create(:account_1) } - let(:tweet) { OpenStruct.new(favourites_count: 10, - listed_count: 12, - followers_count: 14, - statuses_count: 16, - friends_count: 18, - description: "") } - before { account.stub!(:twitter_user).and_return(tweet) } - subject { OpenStruct.new(account.stats_api) } - its(:favorites_count) { should be tweet.favourites_count } - its(:listed_count) { should be tweet.listed_count } - its(:followers_count) { should be tweet.followers_count } - its(:tweets_count) { should be tweet.statuses_count } - its(:friends_count) { should be tweet.friends_count } - its(:bio) { should eq tweet.description } - end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 748ad97..f595fea 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -129,10 +129,17 @@ describe User do FactoryGirl.create(:favorite, tweet: tweet_1, user: user_3) FactoryGirl.create(:retweet, tweet: tweet_2, user: user_2) - account.stub!(:stats_api).and_return(stats_api) - user.stub!(:account).and_return(account) + stub_request(:get, "https://api.twitter.com/1.1/account/verify_credentials.json") + .to_return(status: 200, body: {id: user.id, + favourites_count: 10, + listed_count: 12, + followers_count: 14, + statuses_count: 16, + friends_count: 18, + description: "abc"}.to_json) end - subject { OpenStruct.new(user.stats) } + + subject { OpenStruct.new(user.stats(true)) } its(:stats_api) { should eq stats_api } its(:favorites_count) { should be 0 } its(:retweets_count) { should be 0 } diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b32a16e..eaab06d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ ENV["RAILS_ENV"] ||= 'test' require File.expand_path("../../config/environment", __FILE__) require 'rspec/rails' require 'rspec/autorun' +require "webmock/rspec" # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. |