diff --git a/Gemfile b/Gemfile index bb4b2ef..d4c5bc7 100644 --- a/Gemfile +++ b/Gemfile @@ -15,3 +15,4 @@ gemspec gem 'pry' gem 'rspec-rails' +gem 'generator_spec' diff --git a/Gemfile.lock b/Gemfile.lock index c698ae1..c22a941 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,130 +2,121 @@ PATH remote: . specs: swagger_rails (0.0.1) - rails (~> 4.2) + rails (>= 3.1, < 4) GEM remote: https://rubygems.org/ specs: - actionmailer (4.2.0) - actionpack (= 4.2.0) - actionview (= 4.2.0) - activejob (= 4.2.0) - mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 1.0, >= 1.0.5) - actionpack (4.2.0) - actionview (= 4.2.0) - activesupport (= 4.2.0) - rack (~> 1.6.0) - rack-test (~> 0.6.2) - rails-dom-testing (~> 1.0, >= 1.0.5) - rails-html-sanitizer (~> 1.0, >= 1.0.1) - actionview (4.2.0) - activesupport (= 4.2.0) - builder (~> 3.1) + actionmailer (3.2.22) + actionpack (= 3.2.22) + mail (~> 2.5.4) + actionpack (3.2.22) + activemodel (= 3.2.22) + activesupport (= 3.2.22) + builder (~> 3.0.0) erubis (~> 2.7.0) - rails-dom-testing (~> 1.0, >= 1.0.5) - rails-html-sanitizer (~> 1.0, >= 1.0.1) - activejob (4.2.0) - activesupport (= 4.2.0) - globalid (>= 0.3.0) - activemodel (4.2.0) - activesupport (= 4.2.0) - builder (~> 3.1) - activerecord (4.2.0) - activemodel (= 4.2.0) - activesupport (= 4.2.0) - arel (~> 6.0) - activesupport (4.2.0) - i18n (~> 0.7) - json (~> 1.7, >= 1.7.7) - minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) - tzinfo (~> 1.1) - arel (6.0.3) - builder (3.2.2) + journey (~> 1.0.4) + rack (~> 1.4.5) + rack-cache (~> 1.2) + rack-test (~> 0.6.1) + sprockets (~> 2.2.1) + activemodel (3.2.22) + activesupport (= 3.2.22) + builder (~> 3.0.0) + activerecord (3.2.22) + activemodel (= 3.2.22) + activesupport (= 3.2.22) + arel (~> 3.0.2) + tzinfo (~> 0.3.29) + activeresource (3.2.22) + activemodel (= 3.2.22) + activesupport (= 3.2.22) + activesupport (3.2.22) + i18n (~> 0.6, >= 0.6.4) + multi_json (~> 1.0) + arel (3.0.3) + builder (3.0.4) coderay (1.1.0) diff-lcs (1.2.5) erubis (2.7.0) - globalid (0.3.6) - activesupport (>= 4.1.0) + generator_spec (0.9.3) + activesupport (>= 3.0.0) + railties (>= 3.0.0) + hike (1.2.3) i18n (0.7.0) - json (1.8.2) - loofah (2.0.1) - nokogiri (>= 1.5.9) - mail (2.6.3) - mime-types (>= 1.16, < 3) + journey (1.0.4) + json (1.8.3) + mail (2.5.4) + mime-types (~> 1.16) + treetop (~> 1.4.8) method_source (0.8.2) - mime-types (2.6.2) - mini_portile (0.6.2) - minitest (5.5.1) - nokogiri (1.6.5) - mini_portile (~> 0.6.0) - pry (0.10.1) + mime-types (1.25.1) + multi_json (1.11.2) + polyglot (0.3.5) + pry (0.10.3) coderay (~> 1.1.0) method_source (~> 0.8.1) slop (~> 3.4) - rack (1.6.0) + rack (1.4.7) + rack-cache (1.5.1) + rack (>= 0.4) + rack-ssl (1.3.4) + rack rack-test (0.6.3) rack (>= 1.0) - rails (4.2.0) - actionmailer (= 4.2.0) - actionpack (= 4.2.0) - actionview (= 4.2.0) - activejob (= 4.2.0) - activemodel (= 4.2.0) - activerecord (= 4.2.0) - activesupport (= 4.2.0) - bundler (>= 1.3.0, < 2.0) - railties (= 4.2.0) - sprockets-rails - rails-deprecated_sanitizer (1.0.3) - activesupport (>= 4.2.0.alpha) - rails-dom-testing (1.0.5) - activesupport (>= 4.2.0.beta, < 5.0) - nokogiri (~> 1.6.0) - rails-deprecated_sanitizer (>= 1.0.1) - rails-html-sanitizer (1.0.1) - loofah (~> 2.0) - railties (4.2.0) - actionpack (= 4.2.0) - activesupport (= 4.2.0) + rails (3.2.22) + actionmailer (= 3.2.22) + actionpack (= 3.2.22) + activerecord (= 3.2.22) + activeresource (= 3.2.22) + activesupport (= 3.2.22) + bundler (~> 1.0) + railties (= 3.2.22) + railties (3.2.22) + actionpack (= 3.2.22) + activesupport (= 3.2.22) + rack-ssl (~> 1.3.2) rake (>= 0.8.7) - thor (>= 0.18.1, < 2.0) + rdoc (~> 3.4) + thor (>= 0.14.6, < 2.0) rake (10.4.2) - rspec-core (3.3.1) - rspec-support (~> 3.3.0) - rspec-expectations (3.3.0) + rdoc (3.12.2) + json (~> 1.4) + rspec-core (3.4.1) + rspec-support (~> 3.4.0) + rspec-expectations (3.4.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-mocks (3.3.1) + rspec-support (~> 3.4.0) + rspec-mocks (3.4.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-rails (3.3.2) + rspec-support (~> 3.4.0) + rspec-rails (3.4.0) actionpack (>= 3.0, < 4.3) activesupport (>= 3.0, < 4.3) railties (>= 3.0, < 4.3) - rspec-core (~> 3.3.0) - rspec-expectations (~> 3.3.0) - rspec-mocks (~> 3.3.0) - rspec-support (~> 3.3.0) - rspec-support (3.3.0) + rspec-core (~> 3.4.0) + rspec-expectations (~> 3.4.0) + rspec-mocks (~> 3.4.0) + rspec-support (~> 3.4.0) + rspec-support (3.4.1) slop (3.6.0) - sprockets (3.3.4) + sprockets (2.2.3) + hike (~> 1.2) + multi_json (~> 1.0) rack (~> 1.0) - sprockets-rails (2.3.3) - actionpack (>= 3.0) - activesupport (>= 3.0) - sprockets (>= 2.8, < 4.0) + tilt (~> 1.1, != 1.3.0) thor (0.19.1) - thread_safe (0.3.4) - tzinfo (1.2.2) - thread_safe (~> 0.1) + tilt (1.4.1) + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) + tzinfo (0.3.46) PLATFORMS ruby DEPENDENCIES + generator_spec pry rspec-rails swagger_rails! diff --git a/app/controllers/swagger_rails/swagger_docs_controller.rb b/app/controllers/swagger_rails/swagger_docs_controller.rb index 919814c..d2581bc 100644 --- a/app/controllers/swagger_rails/swagger_docs_controller.rb +++ b/app/controllers/swagger_rails/swagger_docs_controller.rb @@ -2,13 +2,14 @@ module SwaggerRails class SwaggerDocsController < ApplicationController def show - render file: swagger_file_path_for(params[:api_version]), layout: false + render json: swagger_json_for(params[:api_version]), layout: false end private - def swagger_file_path_for(api_version) - File.join(Rails.root, 'config', 'swagger', api_version, 'swagger.json') + def swagger_json_for(api_version) + path = File.join(Rails.root, 'config', 'swagger', api_version, 'swagger.json') + File.read(path) end end end diff --git a/app/controllers/swagger_rails/swagger_ui_controller.rb b/app/controllers/swagger_rails/swagger_ui_controller.rb index b77d932..a2a464e 100644 --- a/app/controllers/swagger_rails/swagger_ui_controller.rb +++ b/app/controllers/swagger_rails/swagger_ui_controller.rb @@ -2,7 +2,7 @@ module SwaggerRails class SwaggerUiController < ApplicationController def index - @discovery_path = swagger_path('v1') + @discovery_path = swagger_path(SwaggerRails.target_api_version) render :index, layout: false end end diff --git a/lib/generators/swagger_rails/install/USAGE b/lib/generators/swagger_rails/install/USAGE new file mode 100644 index 0000000..cece75d --- /dev/null +++ b/lib/generators/swagger_rails/install/USAGE @@ -0,0 +1,9 @@ +Description: + Adds default files required to use swagger_rails + +Example: + rails generate swagger_rails:install + + This will create: + config/swagger/v1/swagger.json + config/initializers/swagger_rails.rb diff --git a/lib/generators/swagger_rails/install/install_generator.rb b/lib/generators/swagger_rails/install/install_generator.rb new file mode 100644 index 0000000..d3b94ac --- /dev/null +++ b/lib/generators/swagger_rails/install/install_generator.rb @@ -0,0 +1,16 @@ +require 'rails/generators' + +module SwaggerRails + + class InstallGenerator < Rails::Generators::Base + source_root File.expand_path('../templates', __FILE__) + + def add_swagger_json + template('swagger.json', 'config/swagger/v1/swagger.json') + end + + def add_initializer + template('swagger_rails.rb', 'config/initializers/swagger_rails.rb') + end + end +end diff --git a/lib/generators/swagger_rails/install/templates/swagger.json b/lib/generators/swagger_rails/install/templates/swagger.json new file mode 100644 index 0000000..1b7597b --- /dev/null +++ b/lib/generators/swagger_rails/install/templates/swagger.json @@ -0,0 +1,45 @@ +{ + "swagger": "2.0", + "info": { + "version": "0.0.0", + "title": "[Enter a description for your API here]", + "description": "The docs below are powered by the default swagger.json installed with swagger_rails. Please update it to describe your API. See here for the complete swagger spec - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md" + }, + "paths": { + "/a/sample/resource": { + "post": { + "tags": [ + "a/sample/resource" + ], + "description": "Create a new sample resource", + "parameters": [ + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/CreateSampleResource" + } + } + ], + "responses": { + "200": { + "description": "Ok" + } + } + } + } + }, + "definitions": { + "CreateSampleResource": { + "properties": { + "name": { + "type": "string" + }, + "date_time": { + "type": "string", + "format": "date-time" + } + } + } + } +} diff --git a/lib/generators/swagger_rails/install/templates/swagger_rails.rb b/lib/generators/swagger_rails/install/templates/swagger_rails.rb new file mode 100644 index 0000000..cfa28ed --- /dev/null +++ b/lib/generators/swagger_rails/install/templates/swagger_rails.rb @@ -0,0 +1,6 @@ +SwaggerRails.configure do |c| + + # Specify the API version and hence discovery_url (e.g. swagger/v1/swagger.json) + # that will be used to power the embedded swagger-ui + c.target_api_version = 'v1' +end diff --git a/lib/swagger_rails.rb b/lib/swagger_rails.rb index d39e656..bd5c778 100644 --- a/lib/swagger_rails.rb +++ b/lib/swagger_rails.rb @@ -1,4 +1,15 @@ require "swagger_rails/engine" module SwaggerRails + + def self.configure + yield self + end + + class << self + attr_accessor :target_api_version + + #Defaults + @@target_api_version = 'v1' + end end diff --git a/spec/dummy/config/environments/development.rb b/spec/dummy/config/environments/development.rb index 0feba2e..31f9aaf 100644 --- a/spec/dummy/config/environments/development.rb +++ b/spec/dummy/config/environments/development.rb @@ -1,4 +1,4 @@ -Rails.application.configure do +Dummy::Application.configure do # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on diff --git a/spec/dummy/config/environments/production.rb b/spec/dummy/config/environments/production.rb index a008b61..4e1a029 100644 --- a/spec/dummy/config/environments/production.rb +++ b/spec/dummy/config/environments/production.rb @@ -1,4 +1,4 @@ -Rails.application.configure do +Dummy::Application.configure do # Settings specified here will take precedence over those in config/application.rb. # Code is not reloaded between requests. diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 1c19f08..a7e638b 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -1,4 +1,4 @@ -Rails.application.configure do +Dummy::Application.configure do # Settings specified here will take precedence over those in config/application.rb. # The test environment is used exclusively to run your application's diff --git a/spec/dummy/config/initializers/assets.rb b/spec/dummy/config/initializers/assets.rb deleted file mode 100644 index 01ef3e6..0000000 --- a/spec/dummy/config/initializers/assets.rb +++ /dev/null @@ -1,11 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Version of your assets, change this if you want to expire all your assets. -Rails.application.config.assets.version = '1.0' - -# Add additional assets to the asset load path -# Rails.application.config.assets.paths << Emoji.images_path - -# Precompile additional assets. -# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. -# Rails.application.config.assets.precompile += %w( search.js ) diff --git a/spec/dummy/config/initializers/cookies_serializer.rb b/spec/dummy/config/initializers/cookies_serializer.rb deleted file mode 100644 index 7f70458..0000000 --- a/spec/dummy/config/initializers/cookies_serializer.rb +++ /dev/null @@ -1,3 +0,0 @@ -# Be sure to restart your server when you modify this file. - -Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/spec/dummy/config/initializers/filter_parameter_logging.rb b/spec/dummy/config/initializers/filter_parameter_logging.rb deleted file mode 100644 index 4a994e1..0000000 --- a/spec/dummy/config/initializers/filter_parameter_logging.rb +++ /dev/null @@ -1,4 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Configure sensitive parameters which will be filtered from the log file. -Rails.application.config.filter_parameters += [:password] diff --git a/spec/dummy/config/initializers/secret_token.rb b/spec/dummy/config/initializers/secret_token.rb new file mode 100644 index 0000000..bb23a3e --- /dev/null +++ b/spec/dummy/config/initializers/secret_token.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +Dummy::Application.config.secret_token = '07099944b891552ced4568b48028614b5e481f61503d2794b50121e12c7ef560cda886d188636c504ae24bbb7dcdc01bb3fd9327c2cdae374a3afa42c37b02d9' diff --git a/spec/dummy/config/initializers/swagger_rails.rb b/spec/dummy/config/initializers/swagger_rails.rb new file mode 100644 index 0000000..cfa28ed --- /dev/null +++ b/spec/dummy/config/initializers/swagger_rails.rb @@ -0,0 +1,6 @@ +SwaggerRails.configure do |c| + + # Specify the API version and hence discovery_url (e.g. swagger/v1/swagger.json) + # that will be used to power the embedded swagger-ui + c.target_api_version = 'v1' +end diff --git a/spec/dummy/config/initializers/wrap_parameters.rb b/spec/dummy/config/initializers/wrap_parameters.rb deleted file mode 100644 index b81ea74..0000000 --- a/spec/dummy/config/initializers/wrap_parameters.rb +++ /dev/null @@ -1,9 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# This file contains settings for ActionController::ParamsWrapper which -# is enabled by default. - -# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. -ActiveSupport.on_load(:action_controller) do - wrap_parameters format: [:json] if respond_to?(:wrap_parameters) -end diff --git a/spec/dummy/config/swagger/v1/swagger.json b/spec/dummy/config/swagger/v1/swagger.json index 1552e6f..830009e 100644 --- a/spec/dummy/config/swagger/v1/swagger.json +++ b/spec/dummy/config/swagger/v1/swagger.json @@ -2,21 +2,22 @@ "swagger": "2.0", "info": { "version": "0.0.0", - "title": "Dummy app for testing swagger_rails" + "title": "[Enter a description for your API here]", + "description": "The docs below are powered by the default swagger.json that was installed with swagger_rails. You can update it to describe your API. See here for the complete swagger spec - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md" }, "paths": { - "/events": { + "/a/sample/resource": { "post": { "tags": [ - "events" + "a/sample/resource" ], - "description": "Create a new Event", + "description": "Create a new sample resource", "parameters": [ { "name": "body", "in": "body", "schema": { - "$ref": "#/definitions/CreateEventBody" + "$ref": "#/definitions/CreateSampleResource" } } ], @@ -29,7 +30,7 @@ } }, "definitions": { - "CreateEventBody": { + "CreateSampleResource": { "properties": { "name": { "type": "string" @@ -37,60 +38,6 @@ "date_time": { "type": "string", "format": "date-time" - }, - "repeat": { - "$ref": "#/definitions/EventRepeatOptions" - }, - "notification": { - "$ref": "#/definitions/EventNotificationOptions" - } - } - }, - "EventRepeatOptions": { - "properties": { - "frequency": { - "type": "string", - "enum": [ - "daily" - ] - }, - "ending_on": { - "type": "string", - "format": "date-time" - } - } - }, - "EventNotificationOptions": { - "properties": { - "intervals": { - "type": "array", - "items": { - "type": "integer", - "format": "int32" - } - }, - "targets": { - "type": "array", - "items": { - "$ref": "#/definitions/NotificationTarget" - } - } - } - }, - "NotificationTarget": { - "properties": { - "type": { - "type": "string", - "enum": [ - "sms", - "email" - ] - }, - "phone": { - "type": "string" - }, - "email": { - "type": "string" } } } diff --git a/spec/generators/swagger_rails/install_generator_spec.rb b/spec/generators/swagger_rails/install_generator_spec.rb new file mode 100644 index 0000000..41e6836 --- /dev/null +++ b/spec/generators/swagger_rails/install_generator_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' +require 'generators/swagger_rails/install/install_generator' + +describe SwaggerRails::InstallGenerator do + include GeneratorSpec::TestCase + destination File.expand_path('../tmp', __FILE__) + + before(:all) do + prepare_destination + run_generator + end + + it 'creates a default swagger.json file' do + assert_file('config/swagger/v1/swagger.json') + end + + it 'creates a swagger_rails initializer' do + assert_file('config/initializers/swagger_rails.rb') + end +end diff --git a/spec/generators/swagger_rails/tmp/config/initializers/swagger_rails.rb b/spec/generators/swagger_rails/tmp/config/initializers/swagger_rails.rb new file mode 100644 index 0000000..cfa28ed --- /dev/null +++ b/spec/generators/swagger_rails/tmp/config/initializers/swagger_rails.rb @@ -0,0 +1,6 @@ +SwaggerRails.configure do |c| + + # Specify the API version and hence discovery_url (e.g. swagger/v1/swagger.json) + # that will be used to power the embedded swagger-ui + c.target_api_version = 'v1' +end diff --git a/spec/generators/swagger_rails/tmp/config/swagger/v1/swagger.json b/spec/generators/swagger_rails/tmp/config/swagger/v1/swagger.json new file mode 100644 index 0000000..25b1314 --- /dev/null +++ b/spec/generators/swagger_rails/tmp/config/swagger/v1/swagger.json @@ -0,0 +1,54 @@ +{ + "swagger": "2.0", + "info": { + "version": "0.0.0", + "title": "[Enter a description for your API here]", + "description": "The docs below are powered by the default swagger.json that was installed with swagger_rails. You can update it to describe your API. See here for the complete swagger spec - https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md" + }, + "paths": { + "/a/sample/resource": { + "post": { + "tags": [ + "a/sample/resource" + ], + "description": "Create a new sample resource", + "parameters": [ + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/CreateSampleResource" + } + } + ], + "responses": { + "200": { + "description": "Ok" + } + } + } + } + }, + "definitions": { + "ClinicPatientLink": { + "properties": { + "clinic_patient_link": { + "schema": { + "$ref": "#/definitions/ClinicPatientLink" + } + } + } + } + "ClinicPatientLink": { + "properties": { + "name": { + "type": "string" + }, + "date_time": { + "type": "string", + "format": "date-time" + } + } + } + } +} diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 88ff2d0..dd86af5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,6 +1,6 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) +require File.expand_path('../dummy/config/environment', __FILE__) # Prevent database truncation if the environment is production abort("The Rails environment is running in production mode!") if Rails.env.production? require 'spec_helper' @@ -24,7 +24,7 @@ require 'rspec/rails' # Checks for pending migrations before tests are run. # If you are not using ActiveRecord, you can remove this line. -ActiveRecord::Migration.maintain_test_schema! +#ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures diff --git a/swagger_rails-0.0.1.gem b/swagger_rails-0.0.1.gem deleted file mode 100644 index 8206285..0000000 Binary files a/swagger_rails-0.0.1.gem and /dev/null differ diff --git a/swagger_rails.gemspec b/swagger_rails.gemspec index ef7b8e6..94c5321 100644 --- a/swagger_rails.gemspec +++ b/swagger_rails.gemspec @@ -16,5 +16,5 @@ Gem::Specification.new do |s| s.files = Dir["{app,bower_components/swagger-ui/dist,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] - s.add_dependency "rails", "~> 4.2" + s.add_dependency("rails", ">= 3.1", "< 5") end