0

私は grackle gem を使用して Rails 3 でツイートを取得しています。ツイートを取得してビューに表示することができます。次に、この gem を使用してハッシュタグを検索します。出来ますか。はいの場合、方法を教えてください。私はすでに Twitter API に登録してシークレット トークンを取得しており、ハッシュタグを取得するためにコードを変更しようとしています。すでに Twitter API を利用しており、出力を取得する方法ですが、作業コードでそれを変更/実装する方法。

私のつぶやき.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 に答える