Using YAJL to pull a streaming API in Rails

I'm trying to get YAJL working in my app - been testing it off the Twitter API and the Digg API and I can't seem to get it working.

I'm not sure where I'm going wrong. Tapping into the Twitter streaming API using this code: (username/password removed)

max_allowed_errors = 1200
consecutive_errors = 0
while consecutive_errors < max_allowed_errors do
  url = URI.parse("https://[username]:[password]!@stream.twitter.com/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

I get an error:

Yajl::HttpStream::HttpError in DashboardsController#streamit

Code 200 expected got 0

The error refers to this line:

Yajl::HttpStream.get(url) do |status|

I'd like to avoid API-specific gems so I can reuse code and tap into other APIs down the road (i.e. TweetStream).

Thanks in advance for the help! Let me know if you have any questions or if I can clarify this at all.

Oh! I'll note I'm open to other gems to manage streaming if you have recommendations.

Answers


I think the issue comes from the fact that when you are placing an HTTPS call, it needs to negotiate SSL. If you look at the source you see:

    socket = opts.delete(:socket) || TCPSocket.new(uri.host, uri.port)
    request = "#{method} #{uri.path}#{uri.query ? "?"+uri.query : nil} HTTP/1.1\r\n"

The source is here: https://github.com/brianmario/yajl-ruby/blob/master/lib/yajl/http_stream.rb

It seems that all it does is open a plain TCP socket.

It probably should use http://www.ruby-doc.org/stdlib-1.9.3/libdoc/openssl/rdoc/OpenSSL/SSL/SSLSocket.html for ssl


Need Your Help

mysql Query Return Code

mysql sql

I am Writing a MYSQL Stored Procedure. In that i know using HANDLER we can check the condition for SELECT STATEMENT.

How to refresh table row until db condition?

jquery ruby-on-rails ajax

I have table that prepends with new row via ajax.