在Ruby中使用cURL命令?

时间:2014-04-06 17:54:36

标签: ruby api twitter

想要通过Twitter API抓一堆推文,作为输出我得到cURL命令,就像那样

curl --get 'https://api.twitter.com/1.1/search/tweets.json' --data 'q=football' --header      'Authorization: OAuth oauth_consumer_key="**hidden**", oauth_nonce="**hidden**", oauth_signature="**hidden**", oauth_signature_method="HMAC-SHA1", oauth_timestamp="**hidden**", oauth_token="**hidden**", oauth_version="1.0"' --verbose

我的问题是,有没有办法在Ruby脚本中使用此命令来删除推文?

3 个答案:

答案 0 :(得分:1)

使用这里提供的Twitter gem http://rdoc.info/gems/twitter和以下代码,您可以从ruby脚本中获取所有推文。

require 'twitter'

client = Twitter::REST::Client.new do |config|
    config.consumer_key        ="hidden"
    config.consumer_secret     ="hidden"
    config.access_token        ="hidden"
    config.access_token_secret ="hidden"
end

client.search("football").collect do |tweet|
    puts tweet.text
end

答案 1 :(得分:0)

你可以将它包装在反引号中,并从任何unix(如)命令

中获取输出

script.rb

cmd=`echo 'hello world'`
puts cmd

ouptput:你好世界

答案 2 :(得分:0)

最好使用@Hunter McMillen建议的现有API,但如果您想自己执行http请求,可以使用net/http lib。示例如下:

require 'net/http'
uri = URI('http://example.com/index.html')
params = { :limit => 10, :page => 3 }
uri.query = URI.encode_www_form(params)

res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)

Here is the info on how to set headers