Class: Pupil::Stream
- Inherits:
-
Object
- Object
- Pupil::Stream
- Defined in:
- lib/pupil/stream/base.rb
Defined Under Namespace
Classes: Shash, Status, StreamError
Constant Summary
- STREAM_APIS =
{ :userstream => "https://userstream.twitter.com/2/user.json", :filter => "https://stream.twitter.com/1/statuses/filter.json%s" }
Instance Attribute Summary (collapse)
-
- (Object) screen_name
readonly
Returns the value of attribute screen_name.
Instance Method Summary (collapse)
- - (Object) guess_event(status)
-
- (Stream) initialize(key)
constructor
A new instance of Stream.
- - (Object) run_get_stream(type, param = nil, &block)
-
- (Pupil::Stream::Shash, Pupil::Stream::Status) start(type, param = nil, &block)
Event variable supported :status, :retweeted, :favorite, :friends and :delete.
Constructor Details
- (Stream) initialize(key)
A new instance of Stream
10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pupil/stream/base.rb', line 10 def initialize key @screen_name = key[:screen_name] @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 Attribute Details
- (Object) screen_name (readonly)
Returns the value of attribute screen_name
3 4 5 |
# File 'lib/pupil/stream/base.rb', line 3 def screen_name @screen_name end |
Instance Method Details
- (Object) guess_event(status)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pupil/stream/base.rb', line 70 def guess_event status if status["delete"] return Shash.new(:delete, status["delete"]["status"]) elsif status["friends"] return Shash.new(:friends, status["friends"]) elsif status["event"] == "favorite" return Shash.new(:favorite, status) elsif status["retweeted_status"] return Status.new(status, :retweeted) elsif status["text"] return Status.new(status) else return Shash.new(:unknown, status) end end |
- (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 68 |
# 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 event = self.guess_event status block.call event end end end end rescue => vars raise StreamError, "StreamError: #{vars}" end end end |
- (Pupil::Stream::Shash, Pupil::Stream::Status) start(type, param = nil, &block)
Event variable supported :status, :retweeted, :favorite, :friends and :delete
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 |