lib/pupil/stream/base.rb in pupil-0.1.1 vs lib/pupil/stream/base.rb in pupil-0.1.2
- old
+ new
@@ -1,17 +1,16 @@
class Pupil
class Stream
+ attr_reader :screen_name
class StreamError < StandardError ; end
STREAM_APIS = {
:userstream => "https://userstream.twitter.com/2/user.json",
:filter => "https://stream.twitter.com/1/statuses/filter.json%s"
}
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
@@ -20,11 +19,12 @@
@consumer,
key[:access_token],
key[:access_token_secret]
)
end
-
+
+ # @return [Pupil::Stream::Shash, Pupil::Stream::Status] event variable supported :status, :retweeted, :favorite, :friends and :delete
def start(type, param=nil, &block)
raise ArgumentError unless block_given?
run_get_stream type, param, &block
end
@@ -53,42 +53,66 @@
status = JSON.parse(line)
rescue
break
end
- block.call StreamEvent.new(status)
+ event = self.guess_event status
+ block.call event
end
end
end
end
rescue => vars
raise StreamError, "StreamError: #{vars}"
end
end
end
- class StreamEvent
- attr_reader :type
- attr_reader :content
+ 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
+
+ # Stream Status
+ class Status < Pupil::Status
+ attr_reader :event
+ attr_reader :retweeted_status
- def initialize status
- @type, @content = self.guess_event status
+ def initialize status, event=nil
+ super(status)
+ @event = (event)? event : :status
+ @retweeted_status = status["retweeted_status"]
end
+ end
+
+ # Stream Hash
+ class Shash
+ attr_reader :event
- def guess_event status
- if status["delete"]
- return :delete, status["delete"]["status"]
- elsif status["friends"]
- return :friends, status["friends"]
- elsif status["event"] == "favorite"
- return :favorite, status
- elsif status["retweeted_status"]
- return :retweet, status
- elsif status["text"]
- return :status, status#Pupil::Status.new(status)
- else
- return :unknown, status
- end
+ def initialize event, status
+ @hash = status
+ @event = event
end
+
+ def [] param
+ @hash[param]
+ end
+
+ def size
+ @hash.size
+ end
+
+ alias_method :length, :size
end
end
end