yajl-ruby icon indicating copy to clipboard operation
yajl-ruby copied to clipboard

http_stream deprecation and examples

Open simon3z opened this issue 10 years ago • 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

simon3z avatar Feb 08 '15 13:02 simon3z

Wondering the same :+1:

summera avatar Feb 21 '15 20:02 summera

+1

myconode avatar Mar 24 '15 01:03 myconode

Same. +1

ACPK avatar Nov 25 '15 08:11 ACPK

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

harleyg321 avatar Dec 20 '15 00:12 harleyg321