Class: Tapout::JsonParser

Inherits:
AbstractParser show all
Defined in:
lib/tapout/parsers/json.rb

Overview

The TAP-J Parser takes a TAP-J stream and routes it through a Tapout report format.

Constant Summary

Constants inherited from AbstractParser

AbstractParser::PAUSE_DOCUMENT, AbstractParser::RESUME_DOCUMENT

Instance Method Summary collapse

Methods inherited from AbstractParser

#passthru

Constructor Details

#initialize(options = {}) ⇒ JsonParser

Returns a new instance of JsonParser.



13
14
15
16
17
18
19
# File 'lib/tapout/parsers/json.rb', line 13

def initialize(options={})
  format    = options[:format]
  @reporter = Reporters.factory(format).new
  @input    = options[:input] || $stdin

  @resume = RESUME_DOCUMENT
end

Instance Method Details

#consume(input = nil) ⇒ Object Also known as: read

Read from input using ‘gets` and parse, routing entries to reporter.

input - Input channel, defaults to $stdin. [#gets]

Returns reporter exit code.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tapout/parsers/json.rb', line 26

def consume(input=nil)
  @input = input if input

  while line = input.gets
    case line
    when PAUSE_DOCUMENT
      passthru
    when RESUME_DOCUMENT  # has no effect here
    else
      handle(line)
    end
  end

  @reporter.finalize
end

#handle(entry) ⇒ Object Also known as: <<

Handle document entry.

Returns nothing.



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tapout/parsers/json.rb', line 48

def handle(entry)
  return if entry.empty?
  return if entry == RESUME_DOCUMENT

  begin
    data = JSON.load(entry)
    @reporter << data
  rescue JSON::ParserError
    passthru(entry)
  end
end