0

我正在使用 grackle gem 来获取 rails 3 上的推文。我能够获取推文并在视图中显示它们。现在我想使用这个 gem 搜索主题标签。是否可以。如果是,那么请告诉我如何,因为我已经注册到 twitter API 并获得了秘密令牌,我仍在尝试修改代码以获取主题标签,我已经使用了 twitter API 并且我能够获取输出,但如何在我的工作代码中修改/实现它。

我的 tweet.rb 文件

  Class Tweet < ActiveRecord::Base

  require 'grackle'
  require 'twitter'
  require "rubygems"
   MY_APPLICATION_NAME = "mike1011"


attr_accessible :content, :created


 ####Connect to the Twitter API and pull down the latest tweets"""
  def self.get_latest 
   ##the call to twitter api works but how to modify this further to use the search            api**
    tweets = client.search.home_timeline? :screen_name => MY_APPLICATION_NAME # hit the API

    ##this works on twitter console    
    ##https://api.twitter.com/1.1/search/tweets.json?q=%23haiku


  end

  private
  def self.client
    Grackle::Client.new(:auth=>{
      :type=>:oauth,
      :consumer_key=>'xxxxxxxxxxxxxxx',
      :consumer_secret=>'xxxxxxxxxxxxxxx',
      :token=>"2227985911-xxxxxxxxxxxxxxxxxx",
      :token_secret=>"xxxxxxxxxxxxxxxxxxx"
    })

  end   
end

我希望用户使用搜索按钮和文本框中填写的值来搜索主题标签的视图文件

<fieldset style="height:auto;width:auto">
<%= text_field_tag 'Search', 'Enter your hashtags'%>
<%= button_tag(type: 'button') do
  content_tag(:strong, 'Search in twitter!',:id=>"search_button")
end
%>

</fieldset>
4

1 回答 1

0

是的,有可能。如果您不打算将推文保存到数据库,最好创建一个 api,以便您可以从任何地方调用它或将其全部放在控制器中。

这样做的好处是您可以使用参数。

看看我的代码:https ://github.com/eflores1975/stock-dashboard/blob/master/app/controllers/tweets_controller.rb

基本上,它将推文加载到一个数组中,然后使用 ActiveSupport::JSON.decode 将其转换为可消耗对象并开始对其进行按摩以满足我的需要。

最后,我可以将其用作: http : //gentle-tor-3942.herokuapp.com/tweets/get_tweets?symbol= $aapl

其中 symbol=$aapl 是我正在寻找的哈希标签。

于 2014-06-19T18:27:48.683 回答