使用 Twitter 的热门话题 Gem

Trending Topics Using Twitter Gem

当我实施推特 gem 时,我不断收到:Twitter::REST::Trends:Module 的未定义方法“趋势”。

require "twitter"
class StaticPagesController < ApplicationController
    def home
        client = Twitter::REST::Trends
        @trends =client.trends(id=1, options={})
    end
end

我是 Rails 的新手,所以我可能遗漏了一些愚蠢的东西,但我似乎无法在网上的任何地方找到正确的解决方案。根据我的理解 client.trends(id=1, options={}) 在文档 http://www.rubydoc.info/gems/twitter/Twitter/REST/Trends 中应该是一个 Enumerable。知道它可能是什么吗?感谢大家的宝贵时间。

有同样的问题并找到了您的post。这对我有用。

class TwitterApi

def self.trending_tweets
client.trends(id=1, options = {})
end

def self.client
@client ||= Twitter::REST::Client.new do |config|
config.consumer_key        = ""
config.consumer_secret     = ""
end
end
end

用您的替换密钥和密码,然后在您的应用中调用 TwitterApi.trending_tweets.to_h。我认为它 returns 是一个字符串,所以我将它转换为散列以便访问它。