我正在尝试让 YAJL 在我的应用程序中工作 - 一直在 Twitter API 和 Digg API 上对其进行测试,但我似乎无法让它工作。
我不知道我哪里错了。使用以下代码进入 Twitter 流 API:(删除用户名/密码)
max_allowed_errors = 1200
consecutive_errors = 0
while consecutive_errors < max_allowed_errors do
url = URI.parse("https://[username]:[password]<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b7a1b282f293e3a36752f2c322f2f3e2975383436" rel="noreferrer noopener nofollow">[email protected]</a>/1/statuses/sample.json")
begin
Yajl::HttpStream.get(url) do |status|
consecutive_errors = 0
# puts status.inspect
end
rescue Yajl::HttpStream::InvalidContentType
consecutive_errors += 1
end
sleep(0.25*consecutive_errors)
end
我收到错误:
Yajl::HttpStream::HttpError in DashboardsController#streamit
Code 200 expected got 0
错误指的是这一行:
Yajl::HttpStream.get(url) do |status|
我想避免使用特定于 API 的 gem,这样我就可以重用代码并利用其他 API(即 TweetStream)。
预先感谢您的帮助!如果您有任何疑问或者我是否可以澄清这一点,请告诉我。
哦!我会指出,如果您有建议,我愿意接受其他 gem 来管理流媒体。
最佳答案
我认为问题来自这样一个事实:当您发出 HTTPS 调用时,它需要协商 SSL。如果您查看源代码,您会看到:
socket = opts.delete(:socket) || TCPSocket.new(uri.host, uri.port)
request = "#{method} #{uri.path}#{uri.query ? "?"+uri.query : nil} HTTP/1.1\r\n"
来源在这里:https://github.com/brianmario/yajl-ruby/blob/master/lib/yajl/http_stream.rb
看起来它所做的只是打开一个普通的 TCP 套接字。
它可能应该使用 http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/SSL/SSLSocket.html对于 SSL
关于ruby-on-rails - 使用 YAJL 在 Rails 中拉取流式 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11017387/