From 87b0327230bd5b27b04562288850f5a2e4b8bf49 Mon Sep 17 00:00:00 2001 From: boni Date: Fri, 4 May 2018 22:56:09 +0200 Subject: [PATCH] WIP swagger --- lib/documentary/store.rb | 17 ++++++ spec/documentary/swagger_spec.rb | 90 ++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 spec/documentary/swagger_spec.rb diff --git a/lib/documentary/store.rb b/lib/documentary/store.rb index fbae42a..38bf54d 100644 --- a/lib/documentary/store.rb +++ b/lib/documentary/store.rb @@ -6,6 +6,10 @@ def to_strong convert_to_stong_param(self) end + def to_swagger + { parameters: convert_to_swagger(self) } + end + def authorized_params(controller) dup.tap { |store| (store.delete_unauthorized(controller)) } end @@ -39,5 +43,18 @@ def convert_to_stong_param(hash) def nested?(value) value.is_a?(Hash) && !(value.keys - %i[type desc required if]).empty? end + + def convert_to_swagger(hash) + hash.map do |key, value| + doc = { + name: key.to_s, + in: 'formData', + type: (value[:type] || 'string').downcase, + required: value[:required] + } + doc[:description] = value[:desc] if value[:desc] + doc + end + end end end diff --git a/spec/documentary/swagger_spec.rb b/spec/documentary/swagger_spec.rb new file mode 100644 index 0000000..4ce68b2 --- /dev/null +++ b/spec/documentary/swagger_spec.rb @@ -0,0 +1,90 @@ +require 'spec_helper' +require 'byebug' + +#RSpec.describe Documentary::Swagger do +RSpec.describe Documentary::Store do + let(:controller_class) { TestController } + + before { TestController.params(action, ¶ms) } + subject { TestController.params[action].to_swagger } + + describe '#swagger documentation' do + + context "GET show" do + let(:action) { :show } + let(:params) do + proc { + optional(:name) + required(:emails, type: Array, desc: 'Array of any permitted scalar values see Strong Parameters Docs') + } + end + + it { expect(subject) + .to eq({ + parameters: [{ + name: "name", + in: "formData", + type: "string", + required: false + },{ + name: "emails", + in: "formData", + type: "array", + description: 'Array of any permitted scalar values see Strong Parameters Docs', + required: true + } + ] + }) + } + end + end +end +# { +# "swagger": "2.0", +# "info": { +# "title": "API V1", +# "version": "v1" +# }, +# "paths": { +# "/cars": { +# "post": { +# "summary": "Creates a car", +# "tags": [ +# "Cars" +# ], +# "consumes": [ +# "application/json", +# "application/xml" +# ], +# "parameters": [ +# { +# "name": "blog", +# "in": "body", +# "schema": { +# "type": "object", +# "properties": { +# "vintage": { +# "type": "string" +# }, +# "type": { +# "type": "string" +# } +# }, +# "required": [ +# "vintage", +# "content" +# ] +# } +# } +# ], +# "responses": { +# "201": { +# "description": "car created" +# }, +# "422": { +# "description": "invalid request" +# } +# } +# } +# } +# }