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



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

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



69
70
71
72
# File 'lib/rswag/specs/example_group_helpers.rb', line 69

def examples(example = nil)
  return super() if example.nil?
  [:response][:examples] = example
end

#header(name, attributes) ⇒ Object



61
62
63
64
# File 'lib/rswag/specs/example_group_helpers.rb', line 61

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

#parameter(attributes) ⇒ Object



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

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

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

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



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

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

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



52
53
54
55
# File 'lib/rswag/specs/example_group_helpers.rb', line 52

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

#run_test!(&block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rswag/specs/example_group_helpers.rb', line 74

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



57
58
59
# File 'lib/rswag/specs/example_group_helpers.rb', line 57

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