Class: Pupil::Stream
- Inherits:
-
Object
- Object
- Pupil::Stream
- Includes:
- Essentials
- Defined in:
- lib/pupil/stream/base.rb
Defined Under Namespace
Classes: Array, Hash, Status, User
Constant Summary collapse
- STREAM_APIS =
{ :userstream => "https://userstream.twitter.com/2/user.json", :search => "https://stream.twitter.com/1/statuses/filter.json%s" }
Instance Attribute Summary collapse
-
#screen_name ⇒ Object
readonly
Returns the value of attribute screen_name.
Instance Method Summary collapse
- #guess_event(status) ⇒ Object
-
#initialize(key) ⇒ Stream
constructor
A new instance of Stream.
- #run_get_stream(type, param = nil, &block) ⇒ Object
-
#start(type, param = nil, &block) ⇒ Pupil::Stream::Shash, Pupil::Stream::Status
(also: #run)
Event variable supported :status, :retweeted, :favorite, :friends and :delete.
Methods included from Essentials
#get, #guess_parameter, #post, #serialize_parameter
Constructor Details
#initialize(key) ⇒ Stream
Returns a new instance of Stream.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/pupil/stream/base.rb', line 11 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
#screen_name ⇒ Object (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
#guess_event(status) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pupil/stream/base.rb', line 68 def guess_event status if status["event"] && status["event"]["follow"] return Pupil::Stream::User.new(status["source"], @access_token, :follow) elsif status["event"] return Pupil::Stream::Hash.new(status, status["event"].to_sym) elsif status["friends"] return Pupil::Stream::Array.new(status["friends"], :friends) elsif status["delete"] return Pupil::Stream::Hash.new(status["delete"]["status"], :delete) elsif status["direct_message"] return Pupil::Stream::Hash.new(status["direct_message"], :direct_message) elsif status["retweeted_status"] return Pupil::Stream::Status.new(status, @access_token, :retweeted) elsif status["text"] return Pupil::Stream::Status.new(status, @access_token, :status) else return Pupil::Stream::Hash.new(status, :unknown) end end |
#run_get_stream(type, param = nil, &block) ⇒ Object
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 |
# File 'lib/pupil/stream/base.rb', line 34 def run_get_stream(type, param=nil, &block) uri = URI.parse(STREAM_APIS[type] % serialize_parameter(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 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 end end |
#start(type, param = nil, &block) ⇒ Pupil::Stream::Shash, Pupil::Stream::Status Also known as: run
Returns event variable supported :status, :retweeted, :favorite, :friends and :delete.
27 28 29 30 |
# File 'lib/pupil/stream/base.rb', line 27 def start(type, param=nil, &block) raise ArgumentError unless block_given? run_get_stream(type, param, &block) end |