Add install generator and support Rails 3 or 4

This commit is contained in:
domaindrivendev 2015-12-10 18:24:35 -08:00
parent 13b0fe83c5
commit ec2bda1e3d
25 changed files with 284 additions and 191 deletions

View File

@ -15,3 +15,4 @@ gemspec
gem 'pry' gem 'pry'
gem 'rspec-rails' gem 'rspec-rails'
gem 'generator_spec'

View File

@ -2,130 +2,121 @@ PATH
remote: . remote: .
specs: specs:
swagger_rails (0.0.1) swagger_rails (0.0.1)
rails (~> 4.2) rails (>= 3.1, < 4)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
actionmailer (4.2.0) actionmailer (3.2.22)
actionpack (= 4.2.0) actionpack (= 3.2.22)
actionview (= 4.2.0) mail (~> 2.5.4)
activejob (= 4.2.0) actionpack (3.2.22)
mail (~> 2.5, >= 2.5.4) activemodel (= 3.2.22)
rails-dom-testing (~> 1.0, >= 1.0.5) activesupport (= 3.2.22)
actionpack (4.2.0) builder (~> 3.0.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)
erubis (~> 2.7.0) erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5) journey (~> 1.0.4)
rails-html-sanitizer (~> 1.0, >= 1.0.1) rack (~> 1.4.5)
activejob (4.2.0) rack-cache (~> 1.2)
activesupport (= 4.2.0) rack-test (~> 0.6.1)
globalid (>= 0.3.0) sprockets (~> 2.2.1)
activemodel (4.2.0) activemodel (3.2.22)
activesupport (= 4.2.0) activesupport (= 3.2.22)
builder (~> 3.1) builder (~> 3.0.0)
activerecord (4.2.0) activerecord (3.2.22)
activemodel (= 4.2.0) activemodel (= 3.2.22)
activesupport (= 4.2.0) activesupport (= 3.2.22)
arel (~> 6.0) arel (~> 3.0.2)
activesupport (4.2.0) tzinfo (~> 0.3.29)
i18n (~> 0.7) activeresource (3.2.22)
json (~> 1.7, >= 1.7.7) activemodel (= 3.2.22)
minitest (~> 5.1) activesupport (= 3.2.22)
thread_safe (~> 0.3, >= 0.3.4) activesupport (3.2.22)
tzinfo (~> 1.1) i18n (~> 0.6, >= 0.6.4)
arel (6.0.3) multi_json (~> 1.0)
builder (3.2.2) arel (3.0.3)
builder (3.0.4)
coderay (1.1.0) coderay (1.1.0)
diff-lcs (1.2.5) diff-lcs (1.2.5)
erubis (2.7.0) erubis (2.7.0)
globalid (0.3.6) generator_spec (0.9.3)
activesupport (>= 4.1.0) activesupport (>= 3.0.0)
railties (>= 3.0.0)
hike (1.2.3)
i18n (0.7.0) i18n (0.7.0)
json (1.8.2) journey (1.0.4)
loofah (2.0.1) json (1.8.3)
nokogiri (>= 1.5.9) mail (2.5.4)
mail (2.6.3) mime-types (~> 1.16)
mime-types (>= 1.16, < 3) treetop (~> 1.4.8)
method_source (0.8.2) method_source (0.8.2)
mime-types (2.6.2) mime-types (1.25.1)
mini_portile (0.6.2) multi_json (1.11.2)
minitest (5.5.1) polyglot (0.3.5)
nokogiri (1.6.5) pry (0.10.3)
mini_portile (~> 0.6.0)
pry (0.10.1)
coderay (~> 1.1.0) coderay (~> 1.1.0)
method_source (~> 0.8.1) method_source (~> 0.8.1)
slop (~> 3.4) 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-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
rails (4.2.0) rails (3.2.22)
actionmailer (= 4.2.0) actionmailer (= 3.2.22)
actionpack (= 4.2.0) actionpack (= 3.2.22)
actionview (= 4.2.0) activerecord (= 3.2.22)
activejob (= 4.2.0) activeresource (= 3.2.22)
activemodel (= 4.2.0) activesupport (= 3.2.22)
activerecord (= 4.2.0) bundler (~> 1.0)
activesupport (= 4.2.0) railties (= 3.2.22)
bundler (>= 1.3.0, < 2.0) railties (3.2.22)
railties (= 4.2.0) actionpack (= 3.2.22)
sprockets-rails activesupport (= 3.2.22)
rails-deprecated_sanitizer (1.0.3) rack-ssl (~> 1.3.2)
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)
rake (>= 0.8.7) rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0) rdoc (~> 3.4)
thor (>= 0.14.6, < 2.0)
rake (10.4.2) rake (10.4.2)
rspec-core (3.3.1) rdoc (3.12.2)
rspec-support (~> 3.3.0) json (~> 1.4)
rspec-expectations (3.3.0) rspec-core (3.4.1)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0) rspec-support (~> 3.4.0)
rspec-mocks (3.3.1) rspec-mocks (3.4.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0) rspec-support (~> 3.4.0)
rspec-rails (3.3.2) rspec-rails (3.4.0)
actionpack (>= 3.0, < 4.3) actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3) activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3) railties (>= 3.0, < 4.3)
rspec-core (~> 3.3.0) rspec-core (~> 3.4.0)
rspec-expectations (~> 3.3.0) rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.3.0) rspec-mocks (~> 3.4.0)
rspec-support (~> 3.3.0) rspec-support (~> 3.4.0)
rspec-support (3.3.0) rspec-support (3.4.1)
slop (3.6.0) slop (3.6.0)
sprockets (3.3.4) sprockets (2.2.3)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0) rack (~> 1.0)
sprockets-rails (2.3.3) tilt (~> 1.1, != 1.3.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
thor (0.19.1) thor (0.19.1)
thread_safe (0.3.4) tilt (1.4.1)
tzinfo (1.2.2) treetop (1.4.15)
thread_safe (~> 0.1) polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.46)
PLATFORMS PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
generator_spec
pry pry
rspec-rails rspec-rails
swagger_rails! swagger_rails!

View File

@ -2,13 +2,14 @@ module SwaggerRails
class SwaggerDocsController < ApplicationController class SwaggerDocsController < ApplicationController
def show def show
render file: swagger_file_path_for(params[:api_version]), layout: false render json: swagger_json_for(params[:api_version]), layout: false
end end
private private
def swagger_file_path_for(api_version) def swagger_json_for(api_version)
File.join(Rails.root, 'config', 'swagger', api_version, 'swagger.json') path = File.join(Rails.root, 'config', 'swagger', api_version, 'swagger.json')
File.read(path)
end end
end end
end end

View File

@ -2,7 +2,7 @@ module SwaggerRails
class SwaggerUiController < ApplicationController class SwaggerUiController < ApplicationController
def index def index
@discovery_path = swagger_path('v1') @discovery_path = swagger_path(SwaggerRails.target_api_version)
render :index, layout: false render :index, layout: false
end end
end end

View File

@ -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

View File

@ -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

View File

@ -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"
}
}
}
}
}

View File

@ -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

View File

@ -1,4 +1,15 @@
require "swagger_rails/engine" require "swagger_rails/engine"
module SwaggerRails module SwaggerRails
def self.configure
yield self
end
class << self
attr_accessor :target_api_version
#Defaults
@@target_api_version = 'v1'
end
end end

View File

@ -1,4 +1,4 @@
Rails.application.configure do Dummy::Application.configure do
# Settings specified here will take precedence over those in config/application.rb. # Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on # In the development environment your application's code is reloaded on

View File

@ -1,4 +1,4 @@
Rails.application.configure do Dummy::Application.configure do
# Settings specified here will take precedence over those in config/application.rb. # Settings specified here will take precedence over those in config/application.rb.
# Code is not reloaded between requests. # Code is not reloaded between requests.

View File

@ -1,4 +1,4 @@
Rails.application.configure do Dummy::Application.configure do
# Settings specified here will take precedence over those in config/application.rb. # Settings specified here will take precedence over those in config/application.rb.
# The test environment is used exclusively to run your application's # The test environment is used exclusively to run your application's

View File

@ -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 )

View File

@ -1,3 +0,0 @@
# Be sure to restart your server when you modify this file.
Rails.application.config.action_dispatch.cookies_serializer = :json

View File

@ -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]

View File

@ -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'

View File

@ -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

View File

@ -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

View File

@ -2,21 +2,22 @@
"swagger": "2.0", "swagger": "2.0",
"info": { "info": {
"version": "0.0.0", "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": { "paths": {
"/events": { "/a/sample/resource": {
"post": { "post": {
"tags": [ "tags": [
"events" "a/sample/resource"
], ],
"description": "Create a new Event", "description": "Create a new sample resource",
"parameters": [ "parameters": [
{ {
"name": "body", "name": "body",
"in": "body", "in": "body",
"schema": { "schema": {
"$ref": "#/definitions/CreateEventBody" "$ref": "#/definitions/CreateSampleResource"
} }
} }
], ],
@ -29,7 +30,7 @@
} }
}, },
"definitions": { "definitions": {
"CreateEventBody": { "CreateSampleResource": {
"properties": { "properties": {
"name": { "name": {
"type": "string" "type": "string"
@ -37,60 +38,6 @@
"date_time": { "date_time": {
"type": "string", "type": "string",
"format": "date-time" "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"
} }
} }
} }

View File

@ -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

View File

@ -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

View File

@ -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"
}
}
}
}
}

View File

@ -1,6 +1,6 @@
# This file is copied to spec/ when you run 'rails generate rspec:install' # This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test' 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 # Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production? abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper' require 'spec_helper'
@ -24,7 +24,7 @@ require 'rspec/rails'
# Checks for pending migrations before tests are run. # Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line. # 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| RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures

Binary file not shown.

View File

@ -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.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 end