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

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

#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



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

#consumesObject



26
27
28
# File 'lib/rspec/rails/swagger/request_builder.rb', line 26

def consumes
  [:swagger_operation][:consumes] || document[:consumes]
end

#documentObject



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

#headersObject



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

#methodObject



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

#pathObject



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

#producesObject



22
23
24
# File 'lib/rspec/rails/swagger/request_builder.rb', line 22

def produces
  [:swagger_operation][:produces] || document[:produces]
end

#queryObject



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