From 5662891d627fd26cb608b61ff3c367fee42ce62a Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Wed, 5 Sep 2012 17:25:01 -0700 Subject: [PATCH 01/10] added sinatra server generator --- .../sinatra/SinatraServerGenerator.scala | 76 +++++++++ .../sinatra/templates/MyApp.rb | 12 ++ .../sinatra/templates/README.md | 1 + .../sinatra/templates/api.mustache | 57 +++++++ .../sinatra/templates/config.ru | 2 + .../sinatra/templates/lib/Swaggering.rb | 154 ++++++++++++++++++ .../sinatra/templates/main.mustache | 42 +++++ 7 files changed, 344 insertions(+) create mode 100644 samples/server-generator/sinatra/SinatraServerGenerator.scala create mode 100644 samples/server-generator/sinatra/templates/MyApp.rb create mode 100644 samples/server-generator/sinatra/templates/README.md create mode 100644 samples/server-generator/sinatra/templates/api.mustache create mode 100644 samples/server-generator/sinatra/templates/config.ru create mode 100644 samples/server-generator/sinatra/templates/lib/Swaggering.rb create mode 100644 samples/server-generator/sinatra/templates/main.mustache diff --git a/samples/server-generator/sinatra/SinatraServerGenerator.scala b/samples/server-generator/sinatra/SinatraServerGenerator.scala new file mode 100644 index 00000000000..10f7beab74c --- /dev/null +++ b/samples/server-generator/sinatra/SinatraServerGenerator.scala @@ -0,0 +1,76 @@ +/** + * Copyright 2012 Wordnik, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import com.wordnik.swagger.codegen.BasicScalaGenerator + +import com.wordnik.swagger.core._ + +import java.io.File + +import scala.collection.mutable.{ HashMap, ListBuffer } + +object SinatraServerGenerator extends BasicScalaGenerator { + def main(args: Array[String]) = generateClient(args) + + override def templateDir = "samples/server-generator/sinatra/templates" + + val outputFolder = "samples/server-generator/sinatra/output" + + // where to write generated code + override def destinationDir = outputFolder + "" + + override def modelPackage = Some("com.wordnik.client.model") + + // template used for apis + apiTemplateFiles ++= Map("api.mustache" -> ".rb") + + modelTemplateFiles.clear + + override def apiPackage = Some("lib") + + // supporting classes + override def supportingFiles = List( + ("README.md", outputFolder, "README.md"), + ("config.ru", outputFolder, "config.ru"), + ("MyApp.rb", outputFolder, "MyApp.rb"), + ("lib/Swaggering.rb", outputFolder + File.separator + "lib", "Swaggering.rb")) + + override def processApiMap(m: Map[String, AnyRef]): Map[String, AnyRef] = { + val mutable = scala.collection.mutable.Map() ++ m + + mutable.map(k => { + k._1 match { + // the scalatra templates like lower-case httpMethods + case e: String if (e == "httpMethod") => mutable += "httpMethod" -> k._2.toString.toLowerCase + + // convert path into ruby-ish syntax without basePart (i.e. /pet.{format}/{petId} => /:petId + case e: String if (e == "path") => { + val path = { + val arr = k._2.toString.split("/") + if (arr.length >= 2) { + mutable += "basePart" -> (arr.slice(2, arr.length).mkString("", "/", "")) + "/" + arr.slice(2, arr.length).mkString("", "/", "") + } else + k._2.toString + } + mutable += "path" -> path.replaceAll("\\{", ":").replaceAll("\\}", "") + } + case _ => + } + }) + mutable.toMap + } +} diff --git a/samples/server-generator/sinatra/templates/MyApp.rb b/samples/server-generator/sinatra/templates/MyApp.rb new file mode 100644 index 00000000000..d01cf69327d --- /dev/null +++ b/samples/server-generator/sinatra/templates/MyApp.rb @@ -0,0 +1,12 @@ +require './lib/swaggering' + +# only need to extend if you want special configuration! +class MyApp < Swaggering + self.configure do |config| + config.api_version = '0.2' + end +end + +require './lib/PetApi.rb' +require './lib/StoreApi.rb' +require './lib/UserApi.rb' diff --git a/samples/server-generator/sinatra/templates/README.md b/samples/server-generator/sinatra/templates/README.md new file mode 100644 index 00000000000..2774d951da2 --- /dev/null +++ b/samples/server-generator/sinatra/templates/README.md @@ -0,0 +1 @@ +## empty diff --git a/samples/server-generator/sinatra/templates/api.mustache b/samples/server-generator/sinatra/templates/api.mustache new file mode 100644 index 00000000000..4dede10ab79 --- /dev/null +++ b/samples/server-generator/sinatra/templates/api.mustache @@ -0,0 +1,57 @@ +require 'json' + +{{#operations}} +{{#operation}} + +MyApp.add_route('{{httpMethod}}', '{{path}}', { + "resourcePath" => "/{{name}}", + "summary" => "{{{summary}}}", + "nickname" => "{{nickname}}", + "responseClass" => "{{#returnType}}{{returnType}}{{/returnType}}{{^returnType}}void{{/returnType}}", + "endpoint" => "{{path}}", + "notes" => "{{{notes}}}", + "parameters" => [ + {{#queryParams}} + { + "name" => "{{paramName}}", + "description" => "{{description}}", + "dataType" => "{{swaggerDataType}}", + "paramType" => "query", + "allowMultiple" => {{allowMultiple}}, + "allowableValues" => "{{allowableValues}}", + {{#defaultValue}}"defaultValue" => {{{defaultValue}}}{{/defaultValue}} + }, + {{/queryParams}} + {{#pathParams}} + { + "name" => "{{paramName}}", + "description" => "{{description}}", + "dataType" => "{{swaggerDataType}}", + "paramType" => "path", + }, + {{/pathParams}} + {{#headerParams}} + { + "name" => "{{paramName}}", + "description" => "{{description}}", + "dataType" => "{{swaggerDataType}}", + "paramType" => "header", + }, + {{/headerParams}} + {{#bodyParams}} + { + "name" => "body", + "description" => "{{description}}", + "dataType" => "{{swaggerDataType}}", + "paramType" => "body", + } + {{/bodyParams}} + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +{{/operation}} +{{/operations}} diff --git a/samples/server-generator/sinatra/templates/config.ru b/samples/server-generator/sinatra/templates/config.ru new file mode 100644 index 00000000000..dd65689c94e --- /dev/null +++ b/samples/server-generator/sinatra/templates/config.ru @@ -0,0 +1,2 @@ +require './myapp' +run MyApp diff --git a/samples/server-generator/sinatra/templates/lib/Swaggering.rb b/samples/server-generator/sinatra/templates/lib/Swaggering.rb new file mode 100644 index 00000000000..79aca1da2c9 --- /dev/null +++ b/samples/server-generator/sinatra/templates/lib/Swaggering.rb @@ -0,0 +1,154 @@ +require 'json' +require 'sinatra/base' +require 'sinatra/cross_origin' + +class Configuration + attr_accessor :base_path, :api_version, :swagger_version, :format_specifier + + def initialize + @api_version = '1.0' + @base_path = 'http://localhost:4567' + @swagger_version = '1.1' + @format_specifier = ".json" + end +end + +class Swaggering < Sinatra::Base + register Sinatra::CrossOrigin + + @@routes = {} + @@configuration = Configuration.new + + attr_accessor :configuration + + def self.configure + get("/resources" + @@configuration.format_specifier) { + cross_origin + Swaggering.to_resource_listing + } + @@configuration ||= Configuration.new + yield(@@configuration) if block_given? + end + + def self.add_route(method, path, swag={}, opts={}, &block) + fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path + + accepted = case method + when 'get' + get(fullPath, opts, &block) + true + when 'post' + post(fullPath, opts, &block) + true + when 'delete' + delete(fullPath, opts, &block) + true + when 'put' + put(fullPath, opts, &block) + true + else + false + end + + if accepted then + resourcePath = swag["resourcePath"].to_s + ops = @@routes[resourcePath] + if ops.nil? + ops = Array.new + @@routes.merge!(resourcePath => ops) + + get(resourcePath + @@configuration.format_specifier) do + cross_origin + Swaggering.to_api(resourcePath) + end + end + + swag.merge!("httpMethod" => method.to_s.upcase) + ops.push(swag) + end + end + + def self.to_resource_listing + apis = Array.new + (@@routes.keys).each do |key| + api = { + "path" => (key + ".{format}"), + "description" => "no description" + } + apis.push api + end + + resource = { + "apiVersion" => @@configuration.api_version, + "swaggerVersion" => @@configuration.swagger_version, + "apis" => apis + } + + resource.to_json + end + + def self.to_api(resourcePath) + apis = {} + models = [] + + @@routes[resourcePath].each do |route| + endpoint = route["endpoint"].gsub(/:(\w+)(\/?)/,'{\1}\2') + path = (resourcePath + ".{format}" + endpoint) + api = apis[path] + if api.nil? + api = {"path" => path, "description" => "description", "operations" => []} + apis.merge!(path => api) + end + + parameters = route["parameters"] + + unless parameters.nil? then + parameters.each do |param| + av_string = param["allowableValues"] + unless av_string.nil? + if av_string.count('[') > 0 + pattern = /^([A-Z]*)\[(.*)\]/ + match = pattern.match av_string + case match.to_a[1] + when "LIST" + allowables = match.to_a[2].split(',') + param["allowableValues"] = { + "valueType" => "LIST", + "values" => allowables + } + when "RANGE" + allowables = match.to_a[2].split(',') + param["allowableValues"] = { + "valueType" => "RANGE", + "min" => allowables[0], + "max" => allowables[1] + } + end + end + end + end + end + + op = { + "httpMethod" => route["httpMethod"], + "description" => route["summary"], + "responseClass" => route["responseClass"], + "notes" => route["notes"], + "nickname" => route["nickname"], + "summary" => route["summary"], + "parameters" => route["parameters"] + } + api["operations"].push(op) + end + + api_listing = { + "apiVersion" => @@configuration.api_version, + "swaggerVersion" => @@configuration.swagger_version, + "basePath" => @@configuration.base_path, + "resourcePath" => resourcePath, + "apis" => apis.values, + "models" => models + } + api_listing.to_json + end +end diff --git a/samples/server-generator/sinatra/templates/main.mustache b/samples/server-generator/sinatra/templates/main.mustache new file mode 100644 index 00000000000..038dc22cf9e --- /dev/null +++ b/samples/server-generator/sinatra/templates/main.mustache @@ -0,0 +1,42 @@ +require 'json' +require './lib/swaggering' +require 'sinatra/cross_origin' + +configure do + +end + + +# only need to extend if you want special configuration! +class MyApp < Swaggering + self.configure do |config| + config.api_version = '0.2' + end +end + +# add all your routes +{{#apis}} +{{#operations}} +MyApp.add_route('{{httpMethod}}', '{{path}}', { + "resourcePath" => "{{resourcePath}}", + "summary" => "{{{summary}}}", + "nickname" => "{{nickname}}", + "responseClass" => "{{responseClass}}", + "endpoint" => "{{name}}", + "notes" => "{{{notes}}}", + "parameters" => [{ + "name" => "status", + "description" => "Status values that need to be considered for filter", + "dataType" => "string", + "paramType" => "path", + "allowMultiple" => true, + "allowableValues" => "LIST[available,pending,sold]", + "defaultValue" => "available"}]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +{{/operations}} +{{/apis}} From 4b43e705550c7f37246f1dfa21817c736bbfa064 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Wed, 5 Sep 2012 17:43:21 -0700 Subject: [PATCH 02/10] added sinatra samples --- .../server-generator/sinatra/output/MyApp.rb | 12 ++ .../server-generator/sinatra/output/README.md | 1 + .../server-generator/sinatra/output/config.ru | 2 + .../sinatra/output/lib/PetApi.rb | 112 +++++++++++ .../sinatra/output/lib/StoreApi.rb | 66 +++++++ .../sinatra/output/lib/Swaggering.rb | 154 +++++++++++++++ .../sinatra/output/lib/UserApi.rb | 181 ++++++++++++++++++ 7 files changed, 528 insertions(+) create mode 100644 samples/server-generator/sinatra/output/MyApp.rb create mode 100644 samples/server-generator/sinatra/output/README.md create mode 100644 samples/server-generator/sinatra/output/config.ru create mode 100644 samples/server-generator/sinatra/output/lib/PetApi.rb create mode 100644 samples/server-generator/sinatra/output/lib/StoreApi.rb create mode 100644 samples/server-generator/sinatra/output/lib/Swaggering.rb create mode 100644 samples/server-generator/sinatra/output/lib/UserApi.rb diff --git a/samples/server-generator/sinatra/output/MyApp.rb b/samples/server-generator/sinatra/output/MyApp.rb new file mode 100644 index 00000000000..d01cf69327d --- /dev/null +++ b/samples/server-generator/sinatra/output/MyApp.rb @@ -0,0 +1,12 @@ +require './lib/swaggering' + +# only need to extend if you want special configuration! +class MyApp < Swaggering + self.configure do |config| + config.api_version = '0.2' + end +end + +require './lib/PetApi.rb' +require './lib/StoreApi.rb' +require './lib/UserApi.rb' diff --git a/samples/server-generator/sinatra/output/README.md b/samples/server-generator/sinatra/output/README.md new file mode 100644 index 00000000000..2774d951da2 --- /dev/null +++ b/samples/server-generator/sinatra/output/README.md @@ -0,0 +1 @@ +## empty diff --git a/samples/server-generator/sinatra/output/config.ru b/samples/server-generator/sinatra/output/config.ru new file mode 100644 index 00000000000..dd65689c94e --- /dev/null +++ b/samples/server-generator/sinatra/output/config.ru @@ -0,0 +1,2 @@ +require './myapp' +run MyApp diff --git a/samples/server-generator/sinatra/output/lib/PetApi.rb b/samples/server-generator/sinatra/output/lib/PetApi.rb new file mode 100644 index 00000000000..774349d2e9c --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/PetApi.rb @@ -0,0 +1,112 @@ +require 'json' + +MyApp.add_route('get', '/:petId', { + "resourcePath" => "/pet", + "summary" => "Find pet by ID", + "nickname" => "getPetById", + "responseClass" => "Pet", + "endpoint" => "/:petId", + "notes" => "Returns a pet based on ID", + "parameters" => [ + { + "name" => "petId", + "description" => "ID of pet that needs to be fetched", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/', { + "resourcePath" => "/pet", + "summary" => "Add a new pet to the store", + "nickname" => "addPet", + "responseClass" => "void", + "endpoint" => "/", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "Pet object that needs to be added to the store", + "dataType" => "Pet", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('put', '/', { + "resourcePath" => "/pet", + "summary" => "Update an existing pet", + "nickname" => "updatePet", + "responseClass" => "void", + "endpoint" => "/", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "Pet object that needs to be updated in the store", + "dataType" => "Pet", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/findByStatus', { + "resourcePath" => "/pet", + "summary" => "Finds Pets by status", + "nickname" => "findPetsByStatus", + "responseClass" => "List[Pet]", + "endpoint" => "/findByStatus", + "notes" => "Multiple status values can be provided with comma seperated strings", + "parameters" => [ + { + "name" => "status", + "description" => "Status values that need to be considered for filter", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => true, + "allowableValues" => "LIST[available,pending,sold]", + "defaultValue" => "available"}, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/findByTags', { + "resourcePath" => "/pet", + "summary" => "Finds Pets by tags", + "nickname" => "findPetsByTags", + "responseClass" => "List[Pet]", + "endpoint" => "/findByTags", + "notes" => "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", + "parameters" => [ + { + "name" => "tags", + "description" => "Tags to filter by", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => true, + "allowableValues" => "", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + + diff --git a/samples/server-generator/sinatra/output/lib/StoreApi.rb b/samples/server-generator/sinatra/output/lib/StoreApi.rb new file mode 100644 index 00000000000..8a5e2315c49 --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/StoreApi.rb @@ -0,0 +1,66 @@ +require 'json' + +MyApp.add_route('get', '/order/:orderId', { + "resourcePath" => "/store", + "summary" => "Find purchase order by ID", + "nickname" => "getOrderById", + "responseClass" => "Order", + "endpoint" => "/order/:orderId", + "notes" => "For valid response try integer IDs with value <= 5. Anything above 5 or nonintegers will generate API errors", + "parameters" => [ + { + "name" => "orderId", + "description" => "ID of pet that needs to be fetched", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('delete', '/order/:orderId', { + "resourcePath" => "/store", + "summary" => "Delete purchase order by ID", + "nickname" => "deleteOrder", + "responseClass" => "void", + "endpoint" => "/order/:orderId", + "notes" => "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors", + "parameters" => [ + { + "name" => "orderId", + "description" => "ID of the order that needs to be deleted", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/order', { + "resourcePath" => "/store", + "summary" => "Place an order for a pet", + "nickname" => "placeOrder", + "responseClass" => "void", + "endpoint" => "/order", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "order placed for purchasing the pet", + "dataType" => "Order", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + + diff --git a/samples/server-generator/sinatra/output/lib/Swaggering.rb b/samples/server-generator/sinatra/output/lib/Swaggering.rb new file mode 100644 index 00000000000..79aca1da2c9 --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/Swaggering.rb @@ -0,0 +1,154 @@ +require 'json' +require 'sinatra/base' +require 'sinatra/cross_origin' + +class Configuration + attr_accessor :base_path, :api_version, :swagger_version, :format_specifier + + def initialize + @api_version = '1.0' + @base_path = 'http://localhost:4567' + @swagger_version = '1.1' + @format_specifier = ".json" + end +end + +class Swaggering < Sinatra::Base + register Sinatra::CrossOrigin + + @@routes = {} + @@configuration = Configuration.new + + attr_accessor :configuration + + def self.configure + get("/resources" + @@configuration.format_specifier) { + cross_origin + Swaggering.to_resource_listing + } + @@configuration ||= Configuration.new + yield(@@configuration) if block_given? + end + + def self.add_route(method, path, swag={}, opts={}, &block) + fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path + + accepted = case method + when 'get' + get(fullPath, opts, &block) + true + when 'post' + post(fullPath, opts, &block) + true + when 'delete' + delete(fullPath, opts, &block) + true + when 'put' + put(fullPath, opts, &block) + true + else + false + end + + if accepted then + resourcePath = swag["resourcePath"].to_s + ops = @@routes[resourcePath] + if ops.nil? + ops = Array.new + @@routes.merge!(resourcePath => ops) + + get(resourcePath + @@configuration.format_specifier) do + cross_origin + Swaggering.to_api(resourcePath) + end + end + + swag.merge!("httpMethod" => method.to_s.upcase) + ops.push(swag) + end + end + + def self.to_resource_listing + apis = Array.new + (@@routes.keys).each do |key| + api = { + "path" => (key + ".{format}"), + "description" => "no description" + } + apis.push api + end + + resource = { + "apiVersion" => @@configuration.api_version, + "swaggerVersion" => @@configuration.swagger_version, + "apis" => apis + } + + resource.to_json + end + + def self.to_api(resourcePath) + apis = {} + models = [] + + @@routes[resourcePath].each do |route| + endpoint = route["endpoint"].gsub(/:(\w+)(\/?)/,'{\1}\2') + path = (resourcePath + ".{format}" + endpoint) + api = apis[path] + if api.nil? + api = {"path" => path, "description" => "description", "operations" => []} + apis.merge!(path => api) + end + + parameters = route["parameters"] + + unless parameters.nil? then + parameters.each do |param| + av_string = param["allowableValues"] + unless av_string.nil? + if av_string.count('[') > 0 + pattern = /^([A-Z]*)\[(.*)\]/ + match = pattern.match av_string + case match.to_a[1] + when "LIST" + allowables = match.to_a[2].split(',') + param["allowableValues"] = { + "valueType" => "LIST", + "values" => allowables + } + when "RANGE" + allowables = match.to_a[2].split(',') + param["allowableValues"] = { + "valueType" => "RANGE", + "min" => allowables[0], + "max" => allowables[1] + } + end + end + end + end + end + + op = { + "httpMethod" => route["httpMethod"], + "description" => route["summary"], + "responseClass" => route["responseClass"], + "notes" => route["notes"], + "nickname" => route["nickname"], + "summary" => route["summary"], + "parameters" => route["parameters"] + } + api["operations"].push(op) + end + + api_listing = { + "apiVersion" => @@configuration.api_version, + "swaggerVersion" => @@configuration.swagger_version, + "basePath" => @@configuration.base_path, + "resourcePath" => resourcePath, + "apis" => apis.values, + "models" => models + } + api_listing.to_json + end +end diff --git a/samples/server-generator/sinatra/output/lib/UserApi.rb b/samples/server-generator/sinatra/output/lib/UserApi.rb new file mode 100644 index 00000000000..cbea015d767 --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/UserApi.rb @@ -0,0 +1,181 @@ +require 'json' + +MyApp.add_route('post', '/createWithArray', { + "resourcePath" => "/user", + "summary" => "Creates list of users with given input array", + "nickname" => "createUsersWithArrayInput", + "responseClass" => "void", + "endpoint" => "/createWithArray", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "List of user object", + "dataType" => "Array[User]", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/', { + "resourcePath" => "/user", + "summary" => "Create user", + "nickname" => "createUser", + "responseClass" => "void", + "endpoint" => "/", + "notes" => "This can only be done by the logged in user.", + "parameters" => [ + { + "name" => "body", + "description" => "Created user object", + "dataType" => "User", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/createWithList', { + "resourcePath" => "/user", + "summary" => "Creates list of users with given list input", + "nickname" => "createUsersWithListInput", + "responseClass" => "void", + "endpoint" => "/createWithList", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "List of user object", + "dataType" => "List[User]", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('put', '/:username', { + "resourcePath" => "/user", + "summary" => "Updated user", + "nickname" => "updateUser", + "responseClass" => "void", + "endpoint" => "/:username", + "notes" => "This can only be done by the logged in user.", + "parameters" => [ + { + "name" => "username", + "description" => "name that need to be deleted", + "dataType" => "string", + "paramType" => "path", + }, + { + "name" => "body", + "description" => "Updated user object", + "dataType" => "User", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('delete', '/:username', { + "resourcePath" => "/user", + "summary" => "Delete user", + "nickname" => "deleteUser", + "responseClass" => "void", + "endpoint" => "/:username", + "notes" => "This can only be done by the logged in user.", + "parameters" => [ + { + "name" => "username", + "description" => "The name that needs to be deleted", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/:username', { + "resourcePath" => "/user", + "summary" => "Get user by user name", + "nickname" => "getUserByName", + "responseClass" => "User", + "endpoint" => "/:username", + "notes" => "", + "parameters" => [ + { + "name" => "username", + "description" => "The name that needs to be fetched. Use user1 for testing.", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/login', { + "resourcePath" => "/user", + "summary" => "Logs user into the system", + "nickname" => "loginUser", + "responseClass" => "String", + "endpoint" => "/login", + "notes" => "", + "parameters" => [ + { + "name" => "username", + "description" => "The user name for login", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => false, + "allowableValues" => "", + }, + { + "name" => "password", + "description" => "The password for login in clear text", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => false, + "allowableValues" => "", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/logout', { + "resourcePath" => "/user", + "summary" => "Logs out current logged in user session", + "nickname" => "logoutUser", + "responseClass" => "void", + "endpoint" => "/logout", + "notes" => "", + "parameters" => [ + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + + From 812213164f587fb77f0dfeba760720c6f10434b9 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Wed, 5 Sep 2012 17:45:07 -0700 Subject: [PATCH 03/10] added sinatra samples --- .../server-generator/sinatra/output/README.md | 29 ++++++++++++++++++- .../sinatra/templates/README.md | 29 ++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/samples/server-generator/sinatra/output/README.md b/samples/server-generator/sinatra/output/README.md index 2774d951da2..4791f77eedd 100644 --- a/samples/server-generator/sinatra/output/README.md +++ b/samples/server-generator/sinatra/output/README.md @@ -1 +1,28 @@ -## empty +# Swagger for Sinatra + +## Overview +This is a project to provide Swagger support inside the [Sinatra](http://www.sinatrarb.com/) framework. You can find +out more about both the spec and the framework at http://swagger.wordnik.com. For more information about +Wordnik's APIs, please visit http://developer.wordnik.com. + +## Prerequisites +You need to install ruby 1.9.3 and the following gems: + +``` +sinatra +sinatra-cross_origin +``` + +## Getting started + +``` +rackup -p 4567 examples/config.ru +``` + +In your [swagger ui](https://github.com/wordnik/swagger-ui), put in the following URL: + +``` +http://localhost:4567/resources.json +``` + +Voila! diff --git a/samples/server-generator/sinatra/templates/README.md b/samples/server-generator/sinatra/templates/README.md index 2774d951da2..4791f77eedd 100644 --- a/samples/server-generator/sinatra/templates/README.md +++ b/samples/server-generator/sinatra/templates/README.md @@ -1 +1,28 @@ -## empty +# Swagger for Sinatra + +## Overview +This is a project to provide Swagger support inside the [Sinatra](http://www.sinatrarb.com/) framework. You can find +out more about both the spec and the framework at http://swagger.wordnik.com. For more information about +Wordnik's APIs, please visit http://developer.wordnik.com. + +## Prerequisites +You need to install ruby 1.9.3 and the following gems: + +``` +sinatra +sinatra-cross_origin +``` + +## Getting started + +``` +rackup -p 4567 examples/config.ru +``` + +In your [swagger ui](https://github.com/wordnik/swagger-ui), put in the following URL: + +``` +http://localhost:4567/resources.json +``` + +Voila! From 2dcfebea985dc09606ed0c223cf1ddce183c2863 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Wed, 5 Sep 2012 17:48:37 -0700 Subject: [PATCH 04/10] added sinatra samples --- samples/server-generator/sinatra/output/README.md | 1 + samples/server-generator/sinatra/templates/README.md | 1 + 2 files changed, 2 insertions(+) diff --git a/samples/server-generator/sinatra/output/README.md b/samples/server-generator/sinatra/output/README.md index 4791f77eedd..beec4407438 100644 --- a/samples/server-generator/sinatra/output/README.md +++ b/samples/server-generator/sinatra/output/README.md @@ -14,6 +14,7 @@ sinatra-cross_origin ``` ## Getting started +This sample was generated with the [swagger-codegen](https://github.com/wordnik/swagger-codegen) project. ``` rackup -p 4567 examples/config.ru diff --git a/samples/server-generator/sinatra/templates/README.md b/samples/server-generator/sinatra/templates/README.md index 4791f77eedd..beec4407438 100644 --- a/samples/server-generator/sinatra/templates/README.md +++ b/samples/server-generator/sinatra/templates/README.md @@ -14,6 +14,7 @@ sinatra-cross_origin ``` ## Getting started +This sample was generated with the [swagger-codegen](https://github.com/wordnik/swagger-codegen) project. ``` rackup -p 4567 examples/config.ru From 7c812c2426e19b2f712942ac53e4c182700193a9 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 6 Sep 2012 09:09:12 -0700 Subject: [PATCH 05/10] updated script --- bin/runscala.sh | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/bin/runscala.sh b/bin/runscala.sh index 101392d5063..f900693818d 100755 --- a/bin/runscala.sh +++ b/bin/runscala.sh @@ -1,24 +1,5 @@ -#!/bin/bash -echo "" > classpath.txt -for file in `ls target/*.jar`; - do echo -n '' >> classpath.txt; - echo -n $file >> classpath.txt; - echo -n ':' >> classpath.txt; -done -for file in `ls target/lib`; - do echo -n 'target/lib/' >> classpath.txt; - echo -n $file >> classpath.txt; - echo -n ':' >> classpath.txt; -done +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +export CLASSPATH="$DIR/../target/lib/*:$DIR/../target/*" +export JAVA_OPTS="${JAVA_OPTS} -DXmx4096M" +scala $WORDNIK_OPTS $JAVA_OPTS -cp $CLASSPATH "$@" -WORDNIK_OPTS=${WORDNIK_OPTS:=NOPE} - -if [ $WORDNIK_OPTS = NOPE ]; - then - export WORDNIK_OPTS="-DconfigFile=conf/config.xml -DdevMode=true" - -fi - -export CLASSPATH=$(cat classpath.txt) -export JAVA_OPTS="${JAVA_OPTS} -DrulePath=data -Xmx4096M -DloggerPath=conf/log4j.properties" -scala $WORDNIK_OPTS $JAVA_CONFIG_OPTIONS -cp $CLASSPATH "$@" From a7156e6f0045d978d9c76096b85651d648157a37 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 6 Sep 2012 09:09:28 -0700 Subject: [PATCH 06/10] removed println --- src/main/scala/com/wordnik/swagger/codegen/BasicGenerator.scala | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/scala/com/wordnik/swagger/codegen/BasicGenerator.scala b/src/main/scala/com/wordnik/swagger/codegen/BasicGenerator.scala index 9916dcc3f65..e98d1c53695 100644 --- a/src/main/scala/com/wordnik/swagger/codegen/BasicGenerator.scala +++ b/src/main/scala/com/wordnik/swagger/codegen/BasicGenerator.scala @@ -107,8 +107,6 @@ abstract class BasicGenerator extends CodegenConfig with PathUtil { m += "outputDirectory" -> (destinationDir + File.separator + apiPackage.getOrElse("").replaceAll("\\.", File.separator)) m += "newline" -> "\n" -// println(json.writeValueAsString(m)) - for ((file, suffix) <- apiTemplateFiles) { m += "filename" -> (className + suffix) generateAndWrite(m.toMap, file) From f37f284605da4a1bcf02b3dcf9b40aef3fafa5dd Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 6 Sep 2012 09:09:47 -0700 Subject: [PATCH 07/10] updated to make names friendly --- .../sinatra/SinatraServerGenerator.scala | 13 +- .../server-generator/sinatra/output/MyApp.rb | 12 -- .../server-generator/sinatra/output/config.ru | 2 +- .../sinatra/output/lib/PetApi.rb | 112 ----------- .../sinatra/output/lib/StoreApi.rb | 66 ------- .../sinatra/output/lib/UserApi.rb | 181 ------------------ .../sinatra/templates/MyApp.rb | 12 -- .../sinatra/templates/config.ru | 2 +- .../sinatra/templates/main.mustache | 42 ---- 9 files changed, 10 insertions(+), 432 deletions(-) delete mode 100644 samples/server-generator/sinatra/output/MyApp.rb delete mode 100644 samples/server-generator/sinatra/output/lib/PetApi.rb delete mode 100644 samples/server-generator/sinatra/output/lib/StoreApi.rb delete mode 100644 samples/server-generator/sinatra/output/lib/UserApi.rb delete mode 100644 samples/server-generator/sinatra/templates/MyApp.rb delete mode 100644 samples/server-generator/sinatra/templates/main.mustache diff --git a/samples/server-generator/sinatra/SinatraServerGenerator.scala b/samples/server-generator/sinatra/SinatraServerGenerator.scala index 10f7beab74c..46a7eae211a 100644 --- a/samples/server-generator/sinatra/SinatraServerGenerator.scala +++ b/samples/server-generator/sinatra/SinatraServerGenerator.scala @@ -14,17 +14,20 @@ * limitations under the License. */ -import com.wordnik.swagger.codegen.BasicScalaGenerator - +import com.wordnik.swagger.codegen.BasicRubyGenerator import com.wordnik.swagger.core._ import java.io.File import scala.collection.mutable.{ HashMap, ListBuffer } -object SinatraServerGenerator extends BasicScalaGenerator { +object SinatraServerGenerator extends BasicRubyGenerator { def main(args: Array[String]) = generateClient(args) + override def toApiName(name: String): String = { + name + "_api" + } + override def templateDir = "samples/server-generator/sinatra/templates" val outputFolder = "samples/server-generator/sinatra/output" @@ -45,8 +48,8 @@ object SinatraServerGenerator extends BasicScalaGenerator { override def supportingFiles = List( ("README.md", outputFolder, "README.md"), ("config.ru", outputFolder, "config.ru"), - ("MyApp.rb", outputFolder, "MyApp.rb"), - ("lib/Swaggering.rb", outputFolder + File.separator + "lib", "Swaggering.rb")) + ("my_app.mustache", outputFolder, "my_app.rb"), + ("lib/swaggering.rb", outputFolder + File.separator + "lib", "swaggering.rb")) override def processApiMap(m: Map[String, AnyRef]): Map[String, AnyRef] = { val mutable = scala.collection.mutable.Map() ++ m diff --git a/samples/server-generator/sinatra/output/MyApp.rb b/samples/server-generator/sinatra/output/MyApp.rb deleted file mode 100644 index d01cf69327d..00000000000 --- a/samples/server-generator/sinatra/output/MyApp.rb +++ /dev/null @@ -1,12 +0,0 @@ -require './lib/swaggering' - -# only need to extend if you want special configuration! -class MyApp < Swaggering - self.configure do |config| - config.api_version = '0.2' - end -end - -require './lib/PetApi.rb' -require './lib/StoreApi.rb' -require './lib/UserApi.rb' diff --git a/samples/server-generator/sinatra/output/config.ru b/samples/server-generator/sinatra/output/config.ru index dd65689c94e..395d0587123 100644 --- a/samples/server-generator/sinatra/output/config.ru +++ b/samples/server-generator/sinatra/output/config.ru @@ -1,2 +1,2 @@ -require './myapp' +require './my_app' run MyApp diff --git a/samples/server-generator/sinatra/output/lib/PetApi.rb b/samples/server-generator/sinatra/output/lib/PetApi.rb deleted file mode 100644 index 774349d2e9c..00000000000 --- a/samples/server-generator/sinatra/output/lib/PetApi.rb +++ /dev/null @@ -1,112 +0,0 @@ -require 'json' - -MyApp.add_route('get', '/:petId', { - "resourcePath" => "/pet", - "summary" => "Find pet by ID", - "nickname" => "getPetById", - "responseClass" => "Pet", - "endpoint" => "/:petId", - "notes" => "Returns a pet based on ID", - "parameters" => [ - { - "name" => "petId", - "description" => "ID of pet that needs to be fetched", - "dataType" => "string", - "paramType" => "path", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('post', '/', { - "resourcePath" => "/pet", - "summary" => "Add a new pet to the store", - "nickname" => "addPet", - "responseClass" => "void", - "endpoint" => "/", - "notes" => "", - "parameters" => [ - { - "name" => "body", - "description" => "Pet object that needs to be added to the store", - "dataType" => "Pet", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('put', '/', { - "resourcePath" => "/pet", - "summary" => "Update an existing pet", - "nickname" => "updatePet", - "responseClass" => "void", - "endpoint" => "/", - "notes" => "", - "parameters" => [ - { - "name" => "body", - "description" => "Pet object that needs to be updated in the store", - "dataType" => "Pet", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('get', '/findByStatus', { - "resourcePath" => "/pet", - "summary" => "Finds Pets by status", - "nickname" => "findPetsByStatus", - "responseClass" => "List[Pet]", - "endpoint" => "/findByStatus", - "notes" => "Multiple status values can be provided with comma seperated strings", - "parameters" => [ - { - "name" => "status", - "description" => "Status values that need to be considered for filter", - "dataType" => "string", - "paramType" => "query", - "allowMultiple" => true, - "allowableValues" => "LIST[available,pending,sold]", - "defaultValue" => "available"}, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('get', '/findByTags', { - "resourcePath" => "/pet", - "summary" => "Finds Pets by tags", - "nickname" => "findPetsByTags", - "responseClass" => "List[Pet]", - "endpoint" => "/findByTags", - "notes" => "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", - "parameters" => [ - { - "name" => "tags", - "description" => "Tags to filter by", - "dataType" => "string", - "paramType" => "query", - "allowMultiple" => true, - "allowableValues" => "", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - - diff --git a/samples/server-generator/sinatra/output/lib/StoreApi.rb b/samples/server-generator/sinatra/output/lib/StoreApi.rb deleted file mode 100644 index 8a5e2315c49..00000000000 --- a/samples/server-generator/sinatra/output/lib/StoreApi.rb +++ /dev/null @@ -1,66 +0,0 @@ -require 'json' - -MyApp.add_route('get', '/order/:orderId', { - "resourcePath" => "/store", - "summary" => "Find purchase order by ID", - "nickname" => "getOrderById", - "responseClass" => "Order", - "endpoint" => "/order/:orderId", - "notes" => "For valid response try integer IDs with value <= 5. Anything above 5 or nonintegers will generate API errors", - "parameters" => [ - { - "name" => "orderId", - "description" => "ID of pet that needs to be fetched", - "dataType" => "string", - "paramType" => "path", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('delete', '/order/:orderId', { - "resourcePath" => "/store", - "summary" => "Delete purchase order by ID", - "nickname" => "deleteOrder", - "responseClass" => "void", - "endpoint" => "/order/:orderId", - "notes" => "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors", - "parameters" => [ - { - "name" => "orderId", - "description" => "ID of the order that needs to be deleted", - "dataType" => "string", - "paramType" => "path", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('post', '/order', { - "resourcePath" => "/store", - "summary" => "Place an order for a pet", - "nickname" => "placeOrder", - "responseClass" => "void", - "endpoint" => "/order", - "notes" => "", - "parameters" => [ - { - "name" => "body", - "description" => "order placed for purchasing the pet", - "dataType" => "Order", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - - diff --git a/samples/server-generator/sinatra/output/lib/UserApi.rb b/samples/server-generator/sinatra/output/lib/UserApi.rb deleted file mode 100644 index cbea015d767..00000000000 --- a/samples/server-generator/sinatra/output/lib/UserApi.rb +++ /dev/null @@ -1,181 +0,0 @@ -require 'json' - -MyApp.add_route('post', '/createWithArray', { - "resourcePath" => "/user", - "summary" => "Creates list of users with given input array", - "nickname" => "createUsersWithArrayInput", - "responseClass" => "void", - "endpoint" => "/createWithArray", - "notes" => "", - "parameters" => [ - { - "name" => "body", - "description" => "List of user object", - "dataType" => "Array[User]", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('post', '/', { - "resourcePath" => "/user", - "summary" => "Create user", - "nickname" => "createUser", - "responseClass" => "void", - "endpoint" => "/", - "notes" => "This can only be done by the logged in user.", - "parameters" => [ - { - "name" => "body", - "description" => "Created user object", - "dataType" => "User", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('post', '/createWithList', { - "resourcePath" => "/user", - "summary" => "Creates list of users with given list input", - "nickname" => "createUsersWithListInput", - "responseClass" => "void", - "endpoint" => "/createWithList", - "notes" => "", - "parameters" => [ - { - "name" => "body", - "description" => "List of user object", - "dataType" => "List[User]", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('put', '/:username', { - "resourcePath" => "/user", - "summary" => "Updated user", - "nickname" => "updateUser", - "responseClass" => "void", - "endpoint" => "/:username", - "notes" => "This can only be done by the logged in user.", - "parameters" => [ - { - "name" => "username", - "description" => "name that need to be deleted", - "dataType" => "string", - "paramType" => "path", - }, - { - "name" => "body", - "description" => "Updated user object", - "dataType" => "User", - "paramType" => "body", - } - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('delete', '/:username', { - "resourcePath" => "/user", - "summary" => "Delete user", - "nickname" => "deleteUser", - "responseClass" => "void", - "endpoint" => "/:username", - "notes" => "This can only be done by the logged in user.", - "parameters" => [ - { - "name" => "username", - "description" => "The name that needs to be deleted", - "dataType" => "string", - "paramType" => "path", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('get', '/:username', { - "resourcePath" => "/user", - "summary" => "Get user by user name", - "nickname" => "getUserByName", - "responseClass" => "User", - "endpoint" => "/:username", - "notes" => "", - "parameters" => [ - { - "name" => "username", - "description" => "The name that needs to be fetched. Use user1 for testing.", - "dataType" => "string", - "paramType" => "path", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('get', '/login', { - "resourcePath" => "/user", - "summary" => "Logs user into the system", - "nickname" => "loginUser", - "responseClass" => "String", - "endpoint" => "/login", - "notes" => "", - "parameters" => [ - { - "name" => "username", - "description" => "The user name for login", - "dataType" => "string", - "paramType" => "query", - "allowMultiple" => false, - "allowableValues" => "", - }, - { - "name" => "password", - "description" => "The password for login in clear text", - "dataType" => "string", - "paramType" => "query", - "allowMultiple" => false, - "allowableValues" => "", - }, - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -MyApp.add_route('get', '/logout', { - "resourcePath" => "/user", - "summary" => "Logs out current logged in user session", - "nickname" => "logoutUser", - "responseClass" => "void", - "endpoint" => "/logout", - "notes" => "", - "parameters" => [ - ]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - - diff --git a/samples/server-generator/sinatra/templates/MyApp.rb b/samples/server-generator/sinatra/templates/MyApp.rb deleted file mode 100644 index d01cf69327d..00000000000 --- a/samples/server-generator/sinatra/templates/MyApp.rb +++ /dev/null @@ -1,12 +0,0 @@ -require './lib/swaggering' - -# only need to extend if you want special configuration! -class MyApp < Swaggering - self.configure do |config| - config.api_version = '0.2' - end -end - -require './lib/PetApi.rb' -require './lib/StoreApi.rb' -require './lib/UserApi.rb' diff --git a/samples/server-generator/sinatra/templates/config.ru b/samples/server-generator/sinatra/templates/config.ru index dd65689c94e..395d0587123 100644 --- a/samples/server-generator/sinatra/templates/config.ru +++ b/samples/server-generator/sinatra/templates/config.ru @@ -1,2 +1,2 @@ -require './myapp' +require './my_app' run MyApp diff --git a/samples/server-generator/sinatra/templates/main.mustache b/samples/server-generator/sinatra/templates/main.mustache deleted file mode 100644 index 038dc22cf9e..00000000000 --- a/samples/server-generator/sinatra/templates/main.mustache +++ /dev/null @@ -1,42 +0,0 @@ -require 'json' -require './lib/swaggering' -require 'sinatra/cross_origin' - -configure do - -end - - -# only need to extend if you want special configuration! -class MyApp < Swaggering - self.configure do |config| - config.api_version = '0.2' - end -end - -# add all your routes -{{#apis}} -{{#operations}} -MyApp.add_route('{{httpMethod}}', '{{path}}', { - "resourcePath" => "{{resourcePath}}", - "summary" => "{{{summary}}}", - "nickname" => "{{nickname}}", - "responseClass" => "{{responseClass}}", - "endpoint" => "{{name}}", - "notes" => "{{{notes}}}", - "parameters" => [{ - "name" => "status", - "description" => "Status values that need to be considered for filter", - "dataType" => "string", - "paramType" => "path", - "allowMultiple" => true, - "allowableValues" => "LIST[available,pending,sold]", - "defaultValue" => "available"}]}) do - cross_origin - # the guts live here - - {"message" => "yes, it worked"}.to_json -end - -{{/operations}} -{{/apis}} From 1e73944722481a27715658c1422e21932b089710 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 6 Sep 2012 09:11:34 -0700 Subject: [PATCH 08/10] added ruby generator stub --- .../com/wordnik/swagger/codegen/BasicRubyGenerator.scala | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/scala/com/wordnik/swagger/codegen/BasicRubyGenerator.scala diff --git a/src/main/scala/com/wordnik/swagger/codegen/BasicRubyGenerator.scala b/src/main/scala/com/wordnik/swagger/codegen/BasicRubyGenerator.scala new file mode 100644 index 00000000000..97daef36c9e --- /dev/null +++ b/src/main/scala/com/wordnik/swagger/codegen/BasicRubyGenerator.scala @@ -0,0 +1,5 @@ +package com.wordnik.swagger.codegen + +class BasicRubyGenerator extends BasicGenerator { + +} \ No newline at end of file From e9146234c7491e90071a9e68fec892c4c8c02f64 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 6 Sep 2012 09:12:11 -0700 Subject: [PATCH 09/10] updated sinatra sample --- .../sinatra/output/lib/pet_api.rb | 112 +++++++++++ .../sinatra/output/lib/store_api.rb | 66 +++++++ .../sinatra/output/lib/user_api.rb | 181 ++++++++++++++++++ .../server-generator/sinatra/output/my_app.rb | 13 ++ .../sinatra/templates/my_app.mustache | 12 ++ 5 files changed, 384 insertions(+) create mode 100644 samples/server-generator/sinatra/output/lib/pet_api.rb create mode 100644 samples/server-generator/sinatra/output/lib/store_api.rb create mode 100644 samples/server-generator/sinatra/output/lib/user_api.rb create mode 100644 samples/server-generator/sinatra/output/my_app.rb create mode 100644 samples/server-generator/sinatra/templates/my_app.mustache diff --git a/samples/server-generator/sinatra/output/lib/pet_api.rb b/samples/server-generator/sinatra/output/lib/pet_api.rb new file mode 100644 index 00000000000..774349d2e9c --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/pet_api.rb @@ -0,0 +1,112 @@ +require 'json' + +MyApp.add_route('get', '/:petId', { + "resourcePath" => "/pet", + "summary" => "Find pet by ID", + "nickname" => "getPetById", + "responseClass" => "Pet", + "endpoint" => "/:petId", + "notes" => "Returns a pet based on ID", + "parameters" => [ + { + "name" => "petId", + "description" => "ID of pet that needs to be fetched", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/', { + "resourcePath" => "/pet", + "summary" => "Add a new pet to the store", + "nickname" => "addPet", + "responseClass" => "void", + "endpoint" => "/", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "Pet object that needs to be added to the store", + "dataType" => "Pet", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('put', '/', { + "resourcePath" => "/pet", + "summary" => "Update an existing pet", + "nickname" => "updatePet", + "responseClass" => "void", + "endpoint" => "/", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "Pet object that needs to be updated in the store", + "dataType" => "Pet", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/findByStatus', { + "resourcePath" => "/pet", + "summary" => "Finds Pets by status", + "nickname" => "findPetsByStatus", + "responseClass" => "List[Pet]", + "endpoint" => "/findByStatus", + "notes" => "Multiple status values can be provided with comma seperated strings", + "parameters" => [ + { + "name" => "status", + "description" => "Status values that need to be considered for filter", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => true, + "allowableValues" => "LIST[available,pending,sold]", + "defaultValue" => "available"}, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/findByTags', { + "resourcePath" => "/pet", + "summary" => "Finds Pets by tags", + "nickname" => "findPetsByTags", + "responseClass" => "List[Pet]", + "endpoint" => "/findByTags", + "notes" => "Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.", + "parameters" => [ + { + "name" => "tags", + "description" => "Tags to filter by", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => true, + "allowableValues" => "", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + + diff --git a/samples/server-generator/sinatra/output/lib/store_api.rb b/samples/server-generator/sinatra/output/lib/store_api.rb new file mode 100644 index 00000000000..8a5e2315c49 --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/store_api.rb @@ -0,0 +1,66 @@ +require 'json' + +MyApp.add_route('get', '/order/:orderId', { + "resourcePath" => "/store", + "summary" => "Find purchase order by ID", + "nickname" => "getOrderById", + "responseClass" => "Order", + "endpoint" => "/order/:orderId", + "notes" => "For valid response try integer IDs with value <= 5. Anything above 5 or nonintegers will generate API errors", + "parameters" => [ + { + "name" => "orderId", + "description" => "ID of pet that needs to be fetched", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('delete', '/order/:orderId', { + "resourcePath" => "/store", + "summary" => "Delete purchase order by ID", + "nickname" => "deleteOrder", + "responseClass" => "void", + "endpoint" => "/order/:orderId", + "notes" => "For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors", + "parameters" => [ + { + "name" => "orderId", + "description" => "ID of the order that needs to be deleted", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/order', { + "resourcePath" => "/store", + "summary" => "Place an order for a pet", + "nickname" => "placeOrder", + "responseClass" => "void", + "endpoint" => "/order", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "order placed for purchasing the pet", + "dataType" => "Order", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + + diff --git a/samples/server-generator/sinatra/output/lib/user_api.rb b/samples/server-generator/sinatra/output/lib/user_api.rb new file mode 100644 index 00000000000..8e1d5715151 --- /dev/null +++ b/samples/server-generator/sinatra/output/lib/user_api.rb @@ -0,0 +1,181 @@ +require 'json' + +MyApp.add_route('post', '/createWithArray', { + "resourcePath" => "/user", + "summary" => "Creates list of users with given input array", + "nickname" => "createUsersWithArrayInput", + "responseClass" => "void", + "endpoint" => "/createWithArray", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "List of user object", + "dataType" => "Array[User]", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/', { + "resourcePath" => "/user", + "summary" => "Create user", + "nickname" => "createUser", + "responseClass" => "void", + "endpoint" => "/", + "notes" => "This can only be done by the logged in user.", + "parameters" => [ + { + "name" => "body", + "description" => "Created user object", + "dataType" => "User", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('post', '/createWithList', { + "resourcePath" => "/user", + "summary" => "Creates list of users with given list input", + "nickname" => "createUsersWithListInput", + "responseClass" => "void", + "endpoint" => "/createWithList", + "notes" => "", + "parameters" => [ + { + "name" => "body", + "description" => "List of user object", + "dataType" => "List[User]", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('put', '/:username', { + "resourcePath" => "/user", + "summary" => "Updated user", + "nickname" => "updateUser", + "responseClass" => "void", + "endpoint" => "/:username", + "notes" => "This can only be done by the logged in user.", + "parameters" => [ + { + "name" => "username", + "description" => "name that need to be deleted", + "dataType" => "string", + "paramType" => "path", + }, + { + "name" => "body", + "description" => "Updated user object", + "dataType" => "User", + "paramType" => "body", + } + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('delete', '/:username', { + "resourcePath" => "/user", + "summary" => "Delete user", + "nickname" => "deleteUser", + "responseClass" => "void", + "endpoint" => "/:username", + "notes" => "This can only be done by the logged in user.", + "parameters" => [ + { + "name" => "username", + "description" => "The name that needs to be deleted", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/:username', { + "resourcePath" => "/user", + "summary" => "Get user by user name", + "nickname" => "getUserByName", + "responseClass" => "User", + "endpoint" => "/:username", + "notes" => "", + "parameters" => [ + { + "name" => "username", + "description" => "The name that needs to be fetched. Use user1 for testing.", + "dataType" => "string", + "paramType" => "path", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/login', { + "resourcePath" => "/user", + "summary" => "Logs user into the system", + "nickname" => "loginUser", + "responseClass" => "string", + "endpoint" => "/login", + "notes" => "", + "parameters" => [ + { + "name" => "username", + "description" => "The user name for login", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => false, + "allowableValues" => "", + }, + { + "name" => "password", + "description" => "The password for login in clear text", + "dataType" => "string", + "paramType" => "query", + "allowMultiple" => false, + "allowableValues" => "", + }, + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + +MyApp.add_route('get', '/logout', { + "resourcePath" => "/user", + "summary" => "Logs out current logged in user session", + "nickname" => "logoutUser", + "responseClass" => "void", + "endpoint" => "/logout", + "notes" => "", + "parameters" => [ + ]}) do + cross_origin + # the guts live here + + {"message" => "yes, it worked"}.to_json +end + + diff --git a/samples/server-generator/sinatra/output/my_app.rb b/samples/server-generator/sinatra/output/my_app.rb new file mode 100644 index 00000000000..d0dd2d68ad3 --- /dev/null +++ b/samples/server-generator/sinatra/output/my_app.rb @@ -0,0 +1,13 @@ +require './lib/swaggering' + +# only need to extend if you want special configuration! +class MyApp < Swaggering + self.configure do |config| + config.api_version = '0.2' + end +end + +require './lib/pet_api.rb' +require './lib/store_api.rb' +require './lib/user_api.rb' + diff --git a/samples/server-generator/sinatra/templates/my_app.mustache b/samples/server-generator/sinatra/templates/my_app.mustache new file mode 100644 index 00000000000..72b5267a140 --- /dev/null +++ b/samples/server-generator/sinatra/templates/my_app.mustache @@ -0,0 +1,12 @@ +require './lib/swaggering' + +# only need to extend if you want special configuration! +class MyApp < Swaggering + self.configure do |config| + config.api_version = '0.2' + end +end + +{{#apis}} +require './lib/{{className}}.rb' +{{/apis}} \ No newline at end of file From b6a070e604f92b2d854f1f6c562925a61f0d2774 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Thu, 6 Sep 2012 09:19:13 -0700 Subject: [PATCH 10/10] added gemfile --- samples/server-generator/sinatra/SinatraServerGenerator.scala | 1 + samples/server-generator/sinatra/output/Gemfile | 4 ++++ samples/server-generator/sinatra/templates/Gemfile | 4 ++++ 3 files changed, 9 insertions(+) create mode 100644 samples/server-generator/sinatra/output/Gemfile create mode 100644 samples/server-generator/sinatra/templates/Gemfile diff --git a/samples/server-generator/sinatra/SinatraServerGenerator.scala b/samples/server-generator/sinatra/SinatraServerGenerator.scala index 46a7eae211a..c7f23c66b10 100644 --- a/samples/server-generator/sinatra/SinatraServerGenerator.scala +++ b/samples/server-generator/sinatra/SinatraServerGenerator.scala @@ -48,6 +48,7 @@ object SinatraServerGenerator extends BasicRubyGenerator { override def supportingFiles = List( ("README.md", outputFolder, "README.md"), ("config.ru", outputFolder, "config.ru"), + ("Gemfile", outputFolder, "Gemfile"), ("my_app.mustache", outputFolder, "my_app.rb"), ("lib/swaggering.rb", outputFolder + File.separator + "lib", "swaggering.rb")) diff --git a/samples/server-generator/sinatra/output/Gemfile b/samples/server-generator/sinatra/output/Gemfile new file mode 100644 index 00000000000..be9c3168ea6 --- /dev/null +++ b/samples/server-generator/sinatra/output/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem "sinatra" +gem "sinatra-cross_origin" \ No newline at end of file diff --git a/samples/server-generator/sinatra/templates/Gemfile b/samples/server-generator/sinatra/templates/Gemfile new file mode 100644 index 00000000000..be9c3168ea6 --- /dev/null +++ b/samples/server-generator/sinatra/templates/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +gem "sinatra" +gem "sinatra-cross_origin" \ No newline at end of file