2016-10-16 27 views
0

所以即时通过使用ruby twitter gem和api与omniauth一起,通过简单的rails应用程序登录到twitter,并返回用户的前20名追随者。代码来做到这一点的主要部分是在一个lib文件的方法,指出:红宝石Twitter的宝石追随者方法重复20次,而不是显示前20名追随者

def followers 
    client.followers.take(20) 
end 

出于某种原因,应用程序完美的作品在本地,但之后被部署到Heroku的,它显示我的第一个追随者,20次重复,而不是前20名追随者。任何帮助,将不胜感激。这里是我的代码:

我有一个基本的叽叽喳喳api应用程序在rails中,它在本地完美地工作,但是当我推到Heroku它不起作用,并检查日志时有一个错误说uninitialized constant WelcomeController::TwitterApi。我无法找出如何纠正这一点。非常感谢。

的lib/twitter_api.rb

class TwitterApi 
    def initialize(user) 
    @user = user 
    end 

    def our_public_tweets 
    client.user_timeline(user, count: 1, exclude_replies: true, include_rts: false) 
    end 

    def followers 
    client.followers.take(20) 
    end 

    private 

    attr_reader :user 

    def client 
    @client ||= Twitter::REST::Client.new do |config| 
     config.consumer_key = Rails.application.secrets.twitter_api_key 
     config.consumer_secret = Rails.application.secrets.twitter_api_secret 
     config.access_token = user.token 
     config.access_token_secret = user.secret 
    end 
    end 
end 

application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery with: :exception 

    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    end 

    # to enable the current_user variable to be used in the view file 
    helper_method :current_user 

end 

welcome_controller.rb

class WelcomeController < ApplicationController 

require 'twitter_api' 

    def index 
    @twitter_api = TwitterApi.new(current_user) 
    end 
end 

的意见/首页/ index.html.erb

<div class="wrapper"> 
    <h1>OMNIAUTH AND TWITTER API</h1> 

    <!-- <%= link_to "Sign in with Twitter", "/auth/twitter" %> --> 
    <% if current_user %> 
    <div id="sign_in_wrapper"> 
     <p id="sign_in">Signed in as <span><%= current_user.name %></span> </p> 
     <%= image_tag current_user.profile_image, class: "profile_image" %> 
     <p><%= link_to "Sign out", signout_path, id: "sign_out" %></p> 
    </div> 

    <div class="public_tweets"> 
     <p>Latest tweet from <%= current_user.name %>:</p> 
     <% @twitter_api.our_public_tweets.each do |tweet| %> 
     <% cache('our_public_tweets', expires_in: 6.hours) do %> 
      <%= parsed_tweet(tweet) %> 
     <% end %> 
     <% end %> 
    </div> 

    <ul class="followers"> 
     <p>First 20 followers for <%= current_user.name %>:</p> 
     <% @twitter_api.followers.each do |follower| %> 
     <% cache('followers', expires_in: 6.hours) do %> 
      <li><%= follower.name %></li> 
      <hr> 
     <% end %> 
     <% end %> 
    </ul> 

    <% else %> 

    <%= link_to "/auth/twitter", id: "link_button" do %> 
     <i class="fa fa-twitter fa-3x"></i> 
    <% end %> 
    <p class="date">Click the twitter icon to sign in and view followers</p> 

    <% end %> 
</div> 

型号/ user.rb

class User < ApplicationRecord 
    def self.from_omniauth(auth_hash) 
    #Look up the user or create them using keys in the auth hash 
    user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create 
    user.update(
     name: auth_hash.info.name, 
     profile_image: auth_hash.info.image, 
     twitter_user_name: auth_hash.info.nickname, 
     token: auth_hash.credentials.token, 
     secret: auth_hash.credentials.secret 
    ) 
    user 
    end 

    # token and secret is what came back from omniauth and this was saved to the user database. 
end 

application_helper.rb

module ApplicationHelper 

    def parsed_tweet(tweet) 
    _parsed_tweet = tweet.text.dup 

    tweet.urls.each do |entity| 
     html_link = link_to(entity.display_url.to_s, entity.expanded_url.to_s, target: 'blank') 
     _parsed_tweet.sub!(entity.url.to_s, html_link) 
    end 

    tweet.media.each do |entity| 
     html_link = link_to(entity.display_url.to_s, entity.expanded_url.to_s, target: 'blank') 
     _parsed_tweet.sub!(entity.url.to_s, html_link) 
    end 

    _parsed_tweet.html_safe 
    end 
end 

回答

1

还有就是你的问题,不正确的缓存。

<% cache('followers', expires_in: 6.hours) do %> 
     <li><%= follower.name %></li> 
     <hr> 
    <% end %> 

当缓存为空时,您找不到关键“关注者”的任何内容。你带第一个跟随者并用该密钥缓存它。当你显示第二个追随者时,已经有一个可用的缓存条目,所以你使用缓存而不是第二个追随者的数据。

与您的其他街区相同的问题。我认为你的意思是缓存整个循环,而不是单个元素。

出于某种原因,应用程序完美的作品在当地

因为本地您有:null cache_store,我想。

+0

多数民众赞成在伟大的 - 谢谢你!出于兴趣,为什么它会改变为heroku而不是本地 - 缓存只能在生产中工作呢? – sdawes

+0

@sdawes:本地你可能没有设置缓存(空存储) –