Module: Readapt::Error

Defined in:
lib/readapt/error.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.adapterObject

Returns the value of attribute adapter.



6
7
8
# File 'lib/readapt/error.rb', line 6

def adapter
  @adapter
end

Class Method Details

.procid=(pid) ⇒ Object



47
48
49
# File 'lib/readapt/error.rb', line 47

def self.procid= pid
  @@procid = pid
end

Instance Method Details

#close_messageObject



59
60
61
# File 'lib/readapt/error.rb', line 59

def close_message
  @@close_message ||= "</readapt-#{procid}>"
end

#open_messageObject



55
56
57
# File 'lib/readapt/error.rb', line 55

def open_message
  @@open_message ||= "<readapt-#{procid}>"
end

#openingObject



9
10
11
# File 'lib/readapt/error.rb', line 9

def opening
  @buffer = ''
end

#procidObject



51
52
53
# File 'lib/readapt/error.rb', line 51

def procid
  @@procid
end

#receiving(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/readapt/error.rb', line 13

def receiving data
  output = ''
  data.each_char do |char|
    @buffer += char
    if open_message.start_with?(@buffer) || @buffer.start_with?(open_message)
      if @buffer.end_with?(close_message)
        msg = @buffer[open_message.length..-(close_message.length+1)]
        exit if msg == '__TERMINATE__'
        Error.adapter.write msg
        @buffer.clear
      end
    else
      output += @buffer
      @buffer.clear
    end
  end
  return if output.empty?
  send_event('output', {
    output: output,
    category: 'stderr'
  })
end

#send_event(event, data) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/readapt/error.rb', line 36

def send_event event, data
  obj = {
    type: 'event',
    event: event
  }
  obj[:body] = data unless data.nil?
  json = obj.to_json
  envelope = "Content-Length: #{json.bytesize}\r\n\r\n#{json}"
  Error.adapter.write envelope
end