class Gherkin::Formatter::JSONFormatter

This class doesn't really generate JSON - instead it populates an Array that can easily be turned into JSON.

Public Class Methods

new(io) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 15
def initialize(io)
  raise "Must be writeable" unless io.respond_to?(:write)
  @io = io
  @feature_hashes = []
  @current_step_or_hook = nil
end

Public Instance Methods

after(match, result) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 80
def after(match, result)
  add_hook(match, result, "after")
end
append_duration(timestamp) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 65
def append_duration(timestamp)
  #check to make sure result exists (scenario outlines do not have results yet)
  if !@current_step_or_hook['result'].nil?
          #convert to nanoseconds
          timestamp = timestamp * 1000000000
          rshash = @current_step_or_hook['result'].to_hash
          rshash['duration'] = timestamp.to_i
          @current_step_or_hook['result'] = rshash
  end
end
background(background) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 36
def background(background)
  feature_elements << background.to_hash
end
before(match, result) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 76
def before(match, result)
  add_hook(match, result, "before")
end
done() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 22
def done
  @io.write(MultiJson.dump(@feature_hashes, :pretty => true))
end
embedding(mime_type, data) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 84
def embedding(mime_type, data)
  embeddings << {'mime_type' => mime_type, 'data' => encode64s(data)}
end
eof() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 92
def eof
end
examples(examples) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 48
def examples(examples)
  all_examples << examples.to_hash
end
feature(feature) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 30
def feature(feature)
  @feature_hash = feature.to_hash
  @feature_hash['uri'] = @uri
  @feature_hashes << @feature_hash
end
match(match) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 57
def match(match)
  @current_step_or_hook['match'] = match.to_hash
end
result(result) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 61
def result(result)
  @current_step_or_hook['result'] = result.to_hash
end
scenario(scenario) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 40
def scenario(scenario)
  feature_elements << scenario.to_hash
end
scenario_outline(scenario_outline) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 44
def scenario_outline(scenario_outline)
  feature_elements << scenario_outline.to_hash
end
step(step) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 52
def step(step)
  @current_step_or_hook = step.to_hash
  steps << @current_step_or_hook
end
uri(uri) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 26
def uri(uri)
  @uri = uri
end
write(text) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 88
def write(text)
  output << text
end

Private Instance Methods

add_hook(match, result, hook) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 97
def add_hook(match, result, hook)
  hooks = feature_element[hook] ||= []
  hooks << {'match' => match.to_hash, 'result' => result.to_hash}
end
all_examples() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 110
def all_examples
  feature_element['examples'] ||= []
end
embeddings() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 118
def embeddings
  @current_step_or_hook['embeddings'] ||= []
end
encode64s(data) click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 126
def encode64s(data)
  # Strip newlines
  Base64.encode64(data).gsub(/\n/, '')
end
feature_element() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 106
def feature_element
  feature_elements[-1]
end
feature_elements() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 102
def feature_elements
  @feature_hash['elements'] ||= []
end
output() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 122
def output
  @current_step_or_hook['output'] ||= []
end
steps() click to toggle source
# File lib/gherkin/formatter/json_formatter.rb, line 114
def steps
  feature_element['steps'] ||= []
end