Class: Mastodon::Streaming::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/mastodon/streaming/response.rb

Instance Method Summary collapse

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

#on_headers_complete(_headers) ⇒ Object

Note:

this does get called


24
25
26
27
# File 'lib/mastodon/streaming/response.rb', line 24

def on_headers_complete(_headers)
  error = Mastodon::Error::ERRORS[@parser.status_code]
  raise error if error
end