Class: RSpec::Rails::Swagger::RequestBuilder
- Inherits:
-
Object
- Object
- RSpec::Rails::Swagger::RequestBuilder
- Defined in:
- lib/rspec/rails/swagger/request_builder.rb
Instance Attribute Summary collapse
-
#instance ⇒ Object
readonly
Returns the value of attribute instance.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
Instance Method Summary collapse
- #body ⇒ Object
- #consumes ⇒ Object
- #document ⇒ Object
- #headers ⇒ Object
-
#initialize(metadata, instance) ⇒ RequestBuilder
constructor
A new instance of RequestBuilder.
- #method ⇒ Object
- #parameter_values(location) ⇒ Object
- #parameters(location = nil) ⇒ Object
- #path ⇒ Object
- #produces ⇒ Object
- #query ⇒ Object
Constructor Details
#initialize(metadata, instance) ⇒ RequestBuilder
Returns a new instance of RequestBuilder.
7 8 9 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 7 def initialize(, instance) @metadata, @instance = , instance end |
Instance Attribute Details
#instance ⇒ Object (readonly)
Returns the value of attribute instance.
5 6 7 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 5 def instance @instance end |
#metadata ⇒ Object (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
#body ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 81 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. if key = parameters(:body).keys.first instance.send(key.split('&').last).to_json end end |
#consumes ⇒ Object
26 27 28 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 26 def consumes [:swagger_operation][:consumes] || document[:consumes] end |
#document ⇒ Object
11 12 13 14 15 16 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 11 def document @document ||= begin name = [:swagger_document] Document.new(RSpec.configuration.swagger_docs[name]) end end |
#headers ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 51 def headers headers = {} # Match the names that Rails uses internally headers['HTTP_ACCEPT'] = produces.join(';') 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 |
#method ⇒ Object
18 19 20 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 18 def method [:swagger_operation][:method] end |
#parameter_values(location) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 41 def parameter_values location # Don't bother looking at the full parameter bodies since all we need # are location and name which are in the key. values = parameters(location) .keys .map{ |k| k.split('&').last } .map{ |name| [name, instance.send(name)] } Hash[values] end |
#parameters(location = nil) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 30 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 |
#path ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 65 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 |
#produces ⇒ Object
22 23 24 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 22 def produces [:swagger_operation][:produces] || document[:produces] end |
#query ⇒ Object
76 77 78 79 |
# File 'lib/rspec/rails/swagger/request_builder.rb', line 76 def query query_params = parameter_values(:query).to_query "?#{query_params}" unless query_params.blank? end |