Module: Rswag::Specs::ExampleGroupHelpers

Defined in:
lib/rswag/specs/example_group_helpers.rb

Instance Method Summary collapse

Instance Method Details

#description(value = nil) ⇒ Object

NOTE: ‘description’ requires special treatment because ExampleGroup already defines a method with that name. Provide an override that supports the existing functionality while also setting the appropriate metadata if applicable



27
28
29
30
31
# File 'lib/rswag/specs/example_group_helpers.rb', line 27

def description(value = nil)
  return super() if value.nil?

  [:operation][:description] = value
end

#examples(example = nil) ⇒ Object

NOTE: Similar to ‘description’, ‘examples’ need to handle the case when being invoked with no params to avoid overriding ‘examples’ method of rspec-core ExampleGroup



72
73
74
75
76
# File 'lib/rswag/specs/example_group_helpers.rb', line 72

def examples(example = nil)
  return super() if example.nil?

  [:response][:examples] = example
end

#header(name, attributes) ⇒ Object



63
64
65
66
67
# File 'lib/rswag/specs/example_group_helpers.rb', line 63

def header(name, attributes)
  [:response][:headers] ||= {}

  [:response][:headers][name] = attributes
end

#parameter(attributes) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rswag/specs/example_group_helpers.rb', line 40

def parameter(attributes)
  if attributes[:in] && attributes[:in].to_sym == :path
    attributes[:required] = true
  end

  if .key?(:operation)
    [:operation][:parameters] ||= []
    [:operation][:parameters] << attributes
  else
    [:path_item][:parameters] ||= []
    [:path_item][:parameters] << attributes
  end
end

#path(template, metadata = {}, &block) ⇒ Object



6
7
8
9
# File 'lib/rswag/specs/example_group_helpers.rb', line 6

def path(template,  = {}, &block)
  [:path_item] = { template: template }
  describe(template, , &block)
end

#response(code, description, metadata = {}, &block) ⇒ Object



54
55
56
57
# File 'lib/rswag/specs/example_group_helpers.rb', line 54

def response(code, description,  = {}, &block)
  [:response] = { code: code, description: description }
  context(description, , &block)
end

#run_test!(&block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rswag/specs/example_group_helpers.rb', line 78

def run_test!(&block)
  # NOTE: rspec 2.x support
  if RSPEC_VERSION < 3
    before do
      submit_request(example.)
    end

    it "returns a #{[:response][:code]} response" do
      ()
      block.call(response) if block_given?
    end
  else
    before do |example|
      submit_request(example.)
    end

    it "returns a #{[:response][:code]} response" do |example|
      (example., &block)
      example.instance_exec(response, &block) if block_given?
    end
  end
end

#schema(value) ⇒ Object



59
60
61
# File 'lib/rswag/specs/example_group_helpers.rb', line 59

def schema(value)
  [:response][:schema] = value
end