2

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.

matthewvb
  • 932
  • 2
  • 11
  • 21

1 Answers1

5

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

Michael Papile
  • 6,836
  • 30
  • 30
  • Thanks. I'm looking into this and alternatives to handle streaming. – matthewvb Jun 13 '12 at 16:27
  • You can thank me by hitting that little up arrow for providing you a helpful answer :) – Michael Papile Jun 13 '12 at 20:18
  • 1
    +1 for you. I haven't gotten SSL to work using YAJL, but I tried switching to EventMachine and got the SSL connection to take - just need a way to manage connections. Thanks for your help. – matthewvb Jun 13 '12 at 20:20
  • Not a problem and thanks. You may want to bring this up to the YAJL developers as it seems like a pretty simple fix for them. – Michael Papile Jun 13 '12 at 20:22