Class: Mastodon::Streaming::Response
- Inherits:
-
Object
- Object
- Mastodon::Streaming::Response
- Defined in:
- lib/mastodon/streaming/response.rb
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
12 13 14 15 16 17 |
# File 'lib/mastodon/streaming/response.rb', line 12 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
19 20 21 |
# File 'lib/mastodon/streaming/response.rb', line 19 def <<(data) @parser << data end |
#on_body(data) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mastodon/streaming/response.rb', line 29 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 |