brianmario/yajl-ruby

http_stream deprecation and examples

simon3z opened this issue · 4 comments

If http_stream is going to be deprecated in 2.0 (generating a warning) what's the preferred implementation of the current:

require 'uri'
require 'yajl/http_stream'

uri = URI.parse("http://#{username}:#{password}@stream.twitter.com/spritzer.json")
Yajl::HttpStream.get(uri, :symbolize_keys => true) do |hash|
  puts hash.inspect
end

Would it be possible to update the README with the new code? Thanks

Wondering the same 👍

ACPK commented

Same. +1

The following would be a way to do streaming with Net::HTTP, although twitter now requires oauth for authentication so the twitter example wouldn't work as it currently stands anyway.

require 'yajl'
require 'net/http'

def object_parsed(obj)
    puts obj.inspect
end

uri = URI("http://#{username}:#{password}@stream.twitter.com/spritzer.json")
request = Net::HTTP::Get.new(uri.request_uri)

Net::HTTP.start(uri.host, uri.port) do |http|
    http.request request do |response|
        parser = Yajl::Parser.new()
        parser.on_parse_complete = method(:object_parsed)
        response.read_body do |chunk|
            parser << chunk
        end
    end
end