使用Rails4的Twitter Streaming API第二部分
Ruby2.0,Rails4で確認
前回:ハッシュタグがyoutubeとなっているtweetを取得してlogに表示する
已经做到了这个地步。
今回は
目標:取得したtweetをMongoDBに入れて簡単なAPIを作ってみる
ところまでやってみます。
我在博客中详细总结了有关MongoDB安装方法等的信息。
Rails应用程序开发
Mongoid的初始设置。
mongoid gem追加
gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'
bundle install
./bin/bundle install
mongoid設定ファイルを生成
./bin/rails generate mongoid:config
-
mongoid gem追加
gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'
-
bundle install
./bin/bundle install
-
mongoid設定ファイルを生成
./bin/rails generate mongoid:config
由于希望同时使用ActiveRecord和MongoDB,我们参考了这篇文章并进行了相关设置。
-
defaultのormがActiveRecordになるように修正
module StreamingSample
class Application < Rails::Application
config.generators do |g|
g.orm :active_record
end
end
end
建立模型
tweet収集用のMongoモデル作成
./bin/rails g mongoid:model tweet_mongo
Mongoモデルとしてロードするclassの設定
development:
options:
preload_models:
- tweet_mongo
TweetMongoモデル更新
./bin/rails g mongoid:model tweet_mongo
development:
options:
preload_models:
- tweet_mongo
我們決定將從串流 API 中獲取的推文直接放入數據中。
class TweetMongo
include Mongoid::Document
field :data
end
推特收集脚本修正
ENV["RAILS_ENV"] ||= "development"
root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require File.join(root, "config", "environment")
daemon = TweetStream::Daemon.new('twitter_streaming',
:log_output => false,
:dir_mode => :script,
:dir => File.join(Rails.root, 'tmp', 'pids'),
)
daemon.on_inited do
ActiveRecord::Base.connection.disconnect!
daemon_logger = Logger.new(File.join(Rails.root, "log", "stream.log"))
Rails.logger = ActiveRecord::Base.logger = daemon_logger
Rails.logger.debug "Listening..."
end
daemon.on_error do |message|
Rails.logger.error message
end
daemon.track('#youtube') do |tweet|
TweetMongo.create!(data: tweet.to_json) # MongoDBに保存する
end
Controller作成
ENV["RAILS_ENV"] ||= "development"
root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require File.join(root, "config", "environment")
daemon = TweetStream::Daemon.new('twitter_streaming',
:log_output => false,
:dir_mode => :script,
:dir => File.join(Rails.root, 'tmp', 'pids'),
)
daemon.on_inited do
ActiveRecord::Base.connection.disconnect!
daemon_logger = Logger.new(File.join(Rails.root, "log", "stream.log"))
Rails.logger = ActiveRecord::Base.logger = daemon_logger
Rails.logger.debug "Listening..."
end
daemon.on_error do |message|
Rails.logger.error message
end
daemon.track('#youtube') do |tweet|
TweetMongo.create!(data: tweet.to_json) # MongoDBに保存する
end
我們將創建一個簡單的API,將獲取的推文作為json格式直接返回。
-
api controller作成
./bin/rails g controller api::tweet
-
routing設定
StreamingSample::Application.routes.draw do
namespace 'api', :module => 'api' do
get 'tweet' => 'tweet#index'
end
end
-
controller更新
class Api::TweetController < ApplicationController
def index
tweets = TweetMongo.desc(:_id).limit(100).map {|tweet| JSON.parse(tweet.data)}
render json: tweets
end
end
尝试从浏览器进行访问
http://本地主机:3000/api/推文
通过这个API,您现在可以展示最新的100条推文。
请提供更多的上下文,以便我可以为您提供适当的翻译。
Rails4でTwitterStreamingAPIを使う その2
RailsでActiveRecordとMongoidの共存のさせ方
GETTING STARTED RAILS 4 WITH MONGODB
Mongoid
Rails3 対応 MongoDB ORM、Mongoid 詳解―永続化
Rails: Store JSON in MongoDB
MacにmongoDBをインストールする。そしてちょっと使ってみる。
MongoDB のコマンド操作メモ
Daemons
RubyでDemonizeするときのオプションとか使い方とか
How to specify daemon’s log and pid directories?
RailsでActiveRecordとMongoidの共存のさせ方
GETTING STARTED RAILS 4 WITH MONGODB
Mongoid
Rails3 対応 MongoDB ORM、Mongoid 詳解―永続化
Rails: Store JSON in MongoDB
MacにmongoDBをインストールする。そしてちょっと使ってみる。
MongoDB のコマンド操作メモ
Daemons
RubyでDemonizeするときのオプションとか使い方とか
How to specify daemon’s log and pid directories?