Module: Openapi::Rswag::Specs::ExampleGroupHelpers

Defined in:
lib/openapi/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
# File 'lib/openapi/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



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

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

#header(name, attributes) ⇒ Object



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

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

#parameter(attributes) ⇒ Object



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

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



6
7
8
9
# File 'lib/openapi/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



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

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

#run_test!(&block) ⇒ Object



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

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



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

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