Class: Cucumber::Formatter::ApiDocs

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/formatter/api_docs.rb

Defined Under Namespace

Classes: Endpoint, Example, Group, Parameter

Constant Summary collapse

VERB_ORDER =
%w(GET POST PUT PATCH DELETE)

Instance Method Summary collapse

Constructor Details

#initialize(step_mother, io, options) ⇒ ApiDocs



70
71
72
# File 'lib/cucumber/formatter/api_docs.rb', line 70

def initialize(step_mother, io, options)
  @endpoints = []
end

Instance Method Details

#after_feature(feature) ⇒ Object



83
84
85
86
# File 'lib/cucumber/formatter/api_docs.rb', line 83

def after_feature(feature)
  @endpoint.parameters.uniq! { |parameter| parameter.name }
  @endpoints << @endpoint
end

#after_feature_element(feature_element) ⇒ Object



97
98
99
# File 'lib/cucumber/formatter/api_docs.rb', line 97

def after_feature_element(feature_element)
  @endpoint.examples << @example
end

#after_features(features) ⇒ Object



74
75
76
# File 'lib/cucumber/formatter/api_docs.rb', line 74

def after_features(features)
  write_html
end

#before_comment(comment) ⇒ Object



88
89
90
# File 'lib/cucumber/formatter/api_docs.rb', line 88

def before_comment(comment)
  @endpoint.description = comment.to_sexp[1].gsub(/#\s*/, "")
end

#before_feature(feature) ⇒ Object



78
79
80
81
# File 'lib/cucumber/formatter/api_docs.rb', line 78

def before_feature(feature)
  verb, path = feature.name.split(" ")
  @endpoint = Endpoint.new(verb: verb, path: path)
end

#before_feature_element(feature_element) ⇒ Object



92
93
94
95
# File 'lib/cucumber/formatter/api_docs.rb', line 92

def before_feature_element(feature_element)
  @example = Example.new(name: feature_element.title)
  @prior_keyword = nil
end

#before_step(step) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cucumber/formatter/api_docs.rb', line 101

def before_step(step)
  keyword = if step.keyword.strip == "And" && @prior_keyword
    @prior_keyword
  else
    step.keyword.strip
  end

  case keyword
  when "Given"
    @example.prerequisites << step.name
    @prior_keyword = "Given"
  when "When"
    if step.multiline_arg
      step.multiline_arg.rows.each do |type, name, value|
        @example.parameters << Parameter.new(type: type, name: name, value: value)
        @endpoint.parameters << Parameter.new(type: type, name: name)
      end
    end

    @prior_keyword = "When"
  when "Then"
    if matches = step.name.match(/the status code is (\d+)/)
      @example.code = matches[1]
    elsif matches = step.name.match(/Content-Type is (.*)$/)
      @example.content_type = matches[1]
    elsif matches = step.name.match(/the JSON response at "(.*?)" should include (keys)*:/)
      key = matches[1]

      if step.multiline_arg.respond_to? :rows
        step.multiline_arg.rows.each do |nested_key, value|
          add_json("#{key}/#{nested_key}", json_value(value))
        end
      else
        add_json(key, JSON.parse(step.multiline_arg))
      end
    elsif matches = step.name.match(/the JSON response at "(.*?)" should be ([^ ]*?)$/)
      keys = matches[1]
      value = matches[2]
      add_json(keys, json_value(value))
    elsif matches = step.name.match(/the JSON response at "(.*?)" should be (variable )*"(.*?)"/)
      keys = matches[1]
      value = matches[3]
      add_json(keys, value)
    end

    @prior_keyword = "Then"
  end
end

#tag_name(tag) ⇒ Object



150
151
152
# File 'lib/cucumber/formatter/api_docs.rb', line 150

def tag_name(tag)
  @endpoint.group = Group.for_tag(tag)
end