Class: RSpec::Rails::Swagger::RequestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/rails/swagger/request_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata, instance) ⇒ RequestBuilder

Creates a new RequestBuilder from the Example class’s metadata hash and a test instance that we can use to populate the parameter values.



11
12
13
# File 'lib/rspec/rails/swagger/request_builder.rb', line 11

def initialize(, instance)
  @metadata, @instance = , instance
end

Instance Attribute Details

#instanceObject (readonly)

Returns the value of attribute instance.



5
6
7
# File 'lib/rspec/rails/swagger/request_builder.rb', line 5

def instance
  @instance
end

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/rspec/rails/swagger/request_builder.rb', line 5

def 
  @metadata
end

Instance Method Details

#bodyObject



98
99
100
101
102
103
104
105
# File 'lib/rspec/rails/swagger/request_builder.rb', line 98

def body
  # And here all we need is the first half of the key to find the body
  # parameter and its name to fetch a value.
  key = parameters(:body).keys.first
  if key
    instance.send(key.split('&').last).to_json
  end
end

#consumesObject



32
33
34
# File 'lib/rspec/rails/swagger/request_builder.rb', line 32

def consumes
  Array([:swagger_operation][:consumes]).presence || Array(document[:consumes])
end

#documentObject

Finds the Document associated with this request so things like schema and parameter references can be resolved.



18
19
20
21
22
# File 'lib/rspec/rails/swagger/request_builder.rb', line 18

def document
  @document ||= begin
    Document.new(RSpec.configuration.swagger_docs[[:swagger_doc]])
  end
end

#envObject

If instance defines an env method this will return those values for inclusion in the Rack env hash.



76
77
78
79
80
# File 'lib/rspec/rails/swagger/request_builder.rb', line 76

def env
  return {} unless instance.respond_to? :env

  instance.env
end

#headersObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec/rails/swagger/request_builder.rb', line 59

def headers
  headers = {}

  # Match the names that Rails uses internally
  headers['HTTP_ACCEPT'] = produces.first if produces.present?
  headers['CONTENT_TYPE'] = consumes.first if consumes.present?

  # TODO: do we need to do some capitalization to match the rack
  # conventions?
  parameter_values(:header).each { |k, v| headers[k] = v }

  headers
end

#methodObject



24
25
26
# File 'lib/rspec/rails/swagger/request_builder.rb', line 24

def method
  [:swagger_operation][:method]
end

#parameter_values(location) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/rspec/rails/swagger/request_builder.rb', line 51

def parameter_values location
  values = parameters(location).
    map{ |_, p| p['$ref'] ? document.resolve_ref(p['$ref']) : p }.
    select{ |p| p[:required] || instance.respond_to?(p[:name]) }.
    map{ |p| [p[:name], instance.send(p[:name])] }
  Hash[values]
end

#parameters(location = nil) ⇒ Object

Returns parameters defined in the operation and path item. Providing a location will filter the parameters to those with a matching in value.



40
41
42
43
44
45
46
47
48
49
# File 'lib/rspec/rails/swagger/request_builder.rb', line 40

def parameters location = nil
  path_item = [:swagger_path_item] || {}
  operation = [:swagger_operation] || {}
  params = path_item.fetch(:parameters, {}).merge(operation.fetch(:parameters, {}))
  if location.present?
    params.select{ |k, _| k.starts_with? "#{location}&" }
  else
    params
  end
end

#pathObject



82
83
84
85
86
87
88
89
90
91
# File 'lib/rspec/rails/swagger/request_builder.rb', line 82

def path
  base_path = document[:basePath] || ''
  # Find params in the path and replace them with values defined in
  # in the example group.
  base_path + [:swagger_path_item][:path].gsub(/(\{.*?\})/) do |match|
    # QUESTION: Should check that the parameter is actually defined in
    # `parameters` before fetch a value?
    instance.send(match[1...-1])
  end
end

#producesObject



28
29
30
# File 'lib/rspec/rails/swagger/request_builder.rb', line 28

def produces
  Array([:swagger_operation][:produces]).presence || Array(document[:produces])
end

#queryObject



93
94
95
96
# File 'lib/rspec/rails/swagger/request_builder.rb', line 93

def query
  query_params = parameter_values(:query).to_query
  "?#{query_params}" unless query_params.blank?
end