Class: Rswag::Specs::RequestFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/rswag/specs/request_factory.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_metadata, global_metadata) ⇒ RequestFactory



8
9
10
11
# File 'lib/rswag/specs/request_factory.rb', line 8

def initialize(, )
   = 
   = 
end

Instance Method Details

#build_body(example) ⇒ Object



29
30
31
32
# File 'lib/rswag/specs/request_factory.rb', line 29

def build_body(example)
  body_parameter = parameters_in(:body).first
  body_parameter.nil? ? '' : example.send(body_parameter[:name]).to_json
end

#build_fullpath(example) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/rswag/specs/request_factory.rb', line 13

def build_fullpath(example)
  [:path_item][:template].dup.tap do |t|
    t.prepend([:basePath] || '')
    parameters_in(:path).each { |p| t.gsub!("{#{p[:name]}}", example.send(p[:name]).to_s) }
    t.concat(build_query_string(example))
  end
end

#build_headers(example) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rswag/specs/request_factory.rb', line 34

def build_headers(example)
  headers = Hash[ parameters_in(:header).map { |p| [ p[:name], example.send(p[:name]).to_s ] } ]
  headers.tap do |h|
    produces = [:operation][:produces] || [:produces]
    consumes = [:operation][:consumes] || [:consumes]
    h['ACCEPT'] = produces.join(';') unless produces.nil?
    h['CONTENT_TYPE'] = consumes.join(';') unless consumes.nil?
  end
end

#build_query_string(example) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/rswag/specs/request_factory.rb', line 21

def build_query_string(example)
  query_string = parameters_in(:query)
    .map { |p| build_query_string_part(p, example.send(p[:name])) }
    .join('&')

  query_string.empty? ? '' : "?#{query_string}"
end