Class: Pupil::Stream
- Inherits:
-
Object
- Object
- Pupil::Stream
- Defined in:
- lib/pupil/stream/base.rb
Defined Under Namespace
Classes: StreamError, StreamEvent
Constant Summary
- STREAM_APIS =
{ :userstream => "https://userstream.twitter.com/2/user.json", :filter => "https://stream.twitter.com/1/statuses/filter.json%s" }
Instance Method Summary (collapse)
-
- (Stream) initialize(key)
constructor
A new instance of Stream.
- - (Object) run_get_stream(type, param = nil, &block)
- - (Object) start(type, param = nil, &block)
Constructor Details
- (Stream) initialize(key)
A new instance of Stream
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pupil/stream/base.rb', line 9 def initialize key @screen_name = key[:screen_name] @client = nil @config = nil @consumer = OAuth::Consumer.new( key[:consumer_key], key[:consumer_secret], :site => TWITTER_API_URL ) @access_token = OAuth::AccessToken.new( @consumer, key[:access_token], key[:access_token_secret] ) end |
Instance Method Details
- (Object) run_get_stream(type, param = nil, &block)
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/pupil/stream/base.rb', line 32 def run_get_stream(type, param=nil, &block) uri = URI.parse(STREAM_APIS[type] % Pupil.param_serializer(param)) https = Net::HTTP.new(uri.host, uri.port) https.use_ssl = true https.verify_mode = OpenSSL::SSL::VERIFY_NONE https.verify_depth = 5 while true do begin https.start do |https| request = Net::HTTP::Get.new(uri.request_uri) request["User-Agent"] = "Ruby/#{RUBY_VERSION} Pupil::Stream" request.oauth!(https, @consumer, @access_token) buf = "" https.request(request) do |response| response.read_body do |chunk| buf << chunk while (line = buf[/.+?(\r\n)+/m]) != nil begin buf.sub!(line,"") line.strip! status = JSON.parse(line) rescue break end block.call StreamEvent.new(status) end end end end rescue => vars raise StreamError, "StreamError: #{vars}" end end end |
- (Object) start(type, param = nil, &block)
26 27 28 29 30 |
# File 'lib/pupil/stream/base.rb', line 26 def start(type, param=nil, &block) raise ArgumentError unless block_given? run_get_stream type, param, &block end |