Skip to content

Commit ac91b0b

Browse files
committed
Merge pull request #142 from zipmark/post-body-formatter
Add a default post body formatter
2 parents 2e2a2b2 + 41c466c commit ac91b0b

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ RspecApiDocumentation.configure do |config|
135135
# Change the filter to only include :public examples
136136
config.filter = :public
137137
end
138+
139+
# Change how the post body is formatted by default, you can still override by `raw_post`
140+
# Can be :json, :xml, or a proc that will be passed the params
141+
config.post_body_formatter = Proc.new { |params| params }
138142
end
139143
```
140144

lib/rspec_api_documentation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'active_support'
22
require 'active_support/inflector'
3+
require 'active_support/core_ext/hash/conversions'
34
require 'cgi'
45
require 'json'
56

lib/rspec_api_documentation/configuration.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ def self.add_setting(name, opts = {})
7979
add_setting :request_headers_to_include, :default => nil
8080
add_setting :response_headers_to_include, :default => nil
8181

82+
# Change how the post body is formatted by default, you can still override by `raw_post`
83+
# Can be :json, :xml, or a proc that will be passed the params
84+
#
85+
# RspecApiDocumentation.configure do |config|
86+
# config.post_body_formatter = Proc.new do |params|
87+
# # convert to whatever you want
88+
# params.to_s
89+
# end
90+
# end
91+
#
92+
# See RspecApiDocumentation::DSL::Endpoint#do_request
93+
add_setting :post_body_formatter, :default => Proc.new { |_| Proc.new { |params| params } }
94+
8295
def client_method=(new_client_method)
8396
RspecApiDocumentation::DSL::Resource.module_eval <<-RUBY
8497
alias :#{new_client_method} #{client_method}

lib/rspec_api_documentation/dsl/endpoint.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,19 @@ def do_request(extra_params = {})
3838
if method == :get && !query_string.blank?
3939
path_or_query += "?#{query_string}"
4040
else
41-
params_or_body = respond_to?(:raw_post) ? raw_post : params
41+
formatter = RspecApiDocumentation.configuration.post_body_formatter
42+
case formatter
43+
when :json
44+
params_or_body = params.to_json
45+
when :xml
46+
params_or_body = params.to_xml
47+
when Proc
48+
params_or_body = formatter.call(params)
49+
else
50+
params_or_body = params
51+
end
52+
53+
params_or_body = respond_to?(:raw_post) ? raw_post : params_or_body
4254
end
4355

4456
rspec_api_documentation_client.send(method, path_or_query, params_or_body, headers)

spec/configuration_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
its(:io_docs_protocol) { should == "http" }
5757
its(:request_headers_to_include) { should be_nil }
5858
its(:response_headers_to_include) { should be_nil }
59+
60+
specify "post body formatter" do
61+
expect(configuration.post_body_formatter.call({ :page => 1})).to eq({ :page => 1 })
62+
end
5963
end
6064

6165
describe "#define_groups" do

spec/dsl_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,50 @@
471471
end
472472
end
473473
end
474+
475+
context "post body formatter" do
476+
after do
477+
RspecApiDocumentation.instance_variable_set(:@configuration, RspecApiDocumentation::Configuration.new)
478+
end
479+
480+
post "/orders" do
481+
parameter :page, "Page to view"
482+
483+
let(:page) { 1 }
484+
485+
specify "formatting by json" do
486+
RspecApiDocumentation.configure do |config|
487+
config.post_body_formatter = :json
488+
end
489+
490+
expect(client).to receive(method).with(path, { :page => 1 }.to_json , nil)
491+
492+
do_request
493+
end
494+
495+
specify "formatting by xml" do
496+
RspecApiDocumentation.configure do |config|
497+
config.post_body_formatter = :xml
498+
end
499+
500+
expect(client).to receive(method).with(path, { :page => 1 }.to_xml , nil)
501+
502+
do_request
503+
end
504+
505+
specify "formatting by proc" do
506+
RspecApiDocumentation.configure do |config|
507+
config.post_body_formatter = Proc.new do |params|
508+
{ :from => "a proc" }.to_json
509+
end
510+
end
511+
512+
expect(client).to receive(method).with(path, { :from => "a proc" }.to_json , nil)
513+
514+
do_request
515+
end
516+
end
517+
end
474518
end
475519

476520
resource "top level parameters" do

0 commit comments

Comments
 (0)