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::Response::Parser.new
  @tokenizer = BufferedTokenizer.new("\n\n")
  @match = Regexp.new(/event: ([a-z]+)\ndata: (.+)/m)
end

Instance Method Details

#<<(data) ⇒ Object



19
20
21
22
23
# File 'lib/mastodon/streaming/response.rb', line 19

def <<(data)
  @parser.add(data)
  error = Mastodon::Error::ERRORS[@parser.status_code]
  raise error if error
end

#on_body(data) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/mastodon/streaming/response.rb', line 30

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



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

def on_headers_complete(_headers)
  @finished[:headers] = true
end