lib/rspec_api_docs/formatter/resource/example.rb in rspec-api-docs-0.8.0 vs lib/rspec_api_docs/formatter/resource/example.rb in rspec-api-docs-0.9.0
- old
+ new
@@ -1,6 +1,7 @@
require 'rspec_api_docs/formatter/resource/example/request_headers'
+require 'rspec_api_docs/formatter/resource/example/deep_hash_set'
module RspecApiDocs
class Resource
class Example
attr_reader :example
@@ -29,21 +30,21 @@
# Parameters for the example.
#
# @return [Array<Parameter>]
def parameters
- metadata.fetch(:parameters, []).map do |name, parameter|
- Parameter.new(name, parameter)
+ metadata.fetch(:parameters, []).map do |name_hash, parameter|
+ Parameter.new(name_hash[:name], parameter)
end
end
# Response fields for the example.
#
# @return [Array<ResponseField>]
def response_fields
- metadata.fetch(:fields, []).map do |name, field|
- ResponseField.new(name, field)
+ metadata.fetch(:fields, []).map do |name_hash, field|
+ ResponseField.new(name_hash[:name], field)
end
end
# Requests stored for the example.
#
@@ -87,10 +88,19 @@
# @return [Hash<Symbol,String>, nil]
def notes
metadata.fetch(:note, {})
end
+ # @return [String, nil]
+ def precedence
+ metadata[:example_precedence]
+ end
+
+ def inspect
+ "#<RspecApiDocs::Resource::Example #{name.inspect}>"
+ end
+
private
def request_response_pairs
metadata.fetch(:requests, []).reject { |pair| pair.any?(&:nil?) }
end
@@ -109,10 +119,18 @@
body = body.read
body.empty? ? nil : body
end
def response_body(body)
- body.empty? ? nil : body
+ unless body.empty?
+ parsed_body = JSON.parse(body, symbolize_names: true)
+ response_fields.each do |f|
+ unless f.example.nil?
+ DeepHashSet.call(parsed_body, f.scope + [f.name], f.example)
+ end
+ end
+ JSON.dump(parsed_body)
+ end
end
def response_status_text(status)
Rack::Utils::HTTP_STATUS_CODES[status]
end