我刚开始学习红宝石和mongodb的概念。这是我试图运行的脚本
require 'rubygems'
require 'tweetstream'
require 'mongo'
TweetStream.configure do |config|
config.consumer_key = '<key>'
config.consumer_secret = '<secret>'
config.oauth_token = '<token>'
config.oauth_token_secret = '<token_secret'
config.auth_method = :oauth
end
connection = Mongo::Connection.new
db = connection.db("tweetsDB")
tweets = db.collection("tweets")
client = TweetStream::Client.new
client.on_error do |message|
puts message
end
client.follow(<user_id>,<user_id>) do |status|
id = tweets.insert(status, :safe => true)
end
注意:我已在上面的脚本中删除了此帖子中的所有静态私有值。
Mongo,bson,bson_ext的版本 - 1.7.0
错误消息
NoMethodError: undefined method `has_key?' for #<Twitter::Tweet:0x7f21cd14cf08>
/var/lib/gems/1.8/gems/bson-1.7.0/lib/bson/types/object_id.rb:93:in `create_pk'
/var/lib/gems/1.8/gems/mongo-1.7.0/lib/mongo/collection.rb:360:in `insert'
/var/lib/gems/1.8/gems/mongo-1.7.0/lib/mongo/collection.rb:360:in `collect!'
/var/lib/gems/1.8/gems/mongo-1.7.0/lib/mongo/collection.rb:360:in `insert'
tracker.rb:28
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:525:in `call'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:525:in `invoke_callback'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:533:in `yield_message_to'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:471:in `respond_to'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:411:in `connect'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:296:in `call'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:296:in `invoke_callback'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:143:in `handle_stream'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:193:in `on_body'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:192:in `each'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:192:in `on_body'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:74:in `<<'
/var/lib/gems/1.8/gems/em-twitter-0.2.1/lib/em-twitter/connection.rb:74:in `receive_data'
/var/lib/gems/1.8/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
/var/lib/gems/1.8/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:385:in `start'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:128:in `filter'
/var/lib/gems/1.8/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:106:in `follow'
tracker.rb:27
答案 0 :(得分:2)
如果您想盲目地将所有内容转储到MongoDB中,那么您可以执行以下操作:
client.follow(<user_id>,<user_id>) do |status|
data = status.to_hash
id = tweets.insert(data)
end
如果您想要更具选择性,那么您可以尝试以下方式:
# only add the following fields to the database
ONLY = %w{created_at text geo coordinate id_str}
client.follow(<user_id>,<user_id>) do |status|
data = status.to_hash.select{|k,v| ONLY.include?(k.to_s)}
id = tweets.insert(data)
end
或者:
# add everything except the following fields to the database
EXCEPT = %w{entities}
client.follow(<user_id>,<user_id>) do |status|
data = status.to_hash.reject{|k,v| EXCEPT.include?(k.to_s)}
id = tweets.insert(data)
end
答案 1 :(得分:0)
答案 2 :(得分:0)
对象的has_key?是Hash超类中可用的方法。它试图做的是将一个json字符串传递给Mongo。除了你传递一个字符串,has_key?不是String类的一部分。只需将其转换为哈希值,你就应该好了。
client.follow(<user_id>,<user_id>) do |status|
id = tweets.insert({:status => status.text}, :safe => true)
end