Class: Mastodon::Streaming::Response
- Inherits:
-
Object
- Object
- Mastodon::Streaming::Response
- Defined in:
- lib/mastodon/streaming/response.rb
Overview
When someone responses to toots.
Instance Method Summary collapse
- #<<(data) ⇒ Object
-
#initialize(&block) ⇒ Mastodon::Streaming::Response
constructor
Initializes a new Response object.
- #on_body(data) ⇒ Object
- #on_headers_complete(_headers) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Mastodon::Streaming::Response
Initializes a new Response object
15 16 17 18 19 20 |
# File 'lib/mastodon/streaming/response.rb', line 15 def initialize(&block) @block = block @parser = Http::Parser.new(self) @tokenizer = BufferedTokenizer.new("\n\n") @match = Regexp.new(/event: ([a-z]+)\ndata: (.+)/m) end |
Instance Method Details
#<<(data) ⇒ Object
22 23 24 |
# File 'lib/mastodon/streaming/response.rb', line 22 def <<(data) @parser << data end |
#on_body(data) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mastodon/streaming/response.rb', line 32 def on_body(data) @tokenizer.extract(data).each do |line| has_data = @match.match(line) next if has_data.nil? type = has_data[1] data = has_data[2] @block.call(type, JSON.parse(data)) end end |
#on_headers_complete(_headers) ⇒ Object
Note:
this does get called
27 28 29 30 |
# File 'lib/mastodon/streaming/response.rb', line 27 def on_headers_complete(_headers) error = Mastodon::Error::ERRORS[@parser.status_code] raise error if error end |