fix add route in sinatra

This commit is contained in:
wing328 2015-12-13 17:22:30 +08:00
parent 79033e57fe
commit bef2dac022
8 changed files with 38 additions and 29 deletions

View File

@ -31,9 +31,10 @@ class Swaggering < Sinatra::Base
end end
def self.add_route(method, path, swag={}, opts={}, &block) def self.add_route(method, path, swag={}, opts={}, &block)
fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path #fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path
fullPath = path.gsub(/{(.*)}/, ':\1')
accepted = case method accepted = case method.to_s.downcase
when 'get' when 'get'
get(fullPath, opts, &block) get(fullPath, opts, &block)
true true
@ -47,6 +48,7 @@ class Swaggering < Sinatra::Base
put(fullPath, opts, &block) put(fullPath, opts, &block)
true true
else else
puts "Error adding route: #{method} #{fullPath}"
false false
end end

View File

@ -3,7 +3,7 @@ require 'json'
{{#operations}} {{#operations}}
{{#operation}} {{#operation}}
MyApp.add_route('{{httpMethod}}', '{{path}}', { MyApp.add_route('{{httpMethod}}', '{{basePathWithoutHost}}{{path}}', {
"resourcePath" => "/{{baseName}}", "resourcePath" => "/{{baseName}}",
"summary" => "{{{summary}}}", "summary" => "{{{summary}}}",
"nickname" => "{{nickname}}", "nickname" => "{{nickname}}",

View File

@ -7,6 +7,7 @@ class MyApp < Swaggering
end end
end end
{{#apis}} # include the api files
require './lib/{{className}}.rb' Dir["./api/*.rb"].each { |file|
{{/apis}} require file
}

View File

@ -1,7 +1,7 @@
require 'json' require 'json'
MyApp.add_route('PUT', '/pet', { MyApp.add_route('PUT', '/v2/pet', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Update an existing pet", "summary" => "Update an existing pet",
"nickname" => "update_pet", "nickname" => "update_pet",
@ -28,7 +28,7 @@ MyApp.add_route('PUT', '/pet', {
end end
MyApp.add_route('POST', '/pet', { MyApp.add_route('POST', '/v2/pet', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Add a new pet to the store", "summary" => "Add a new pet to the store",
"nickname" => "add_pet", "nickname" => "add_pet",
@ -55,7 +55,7 @@ MyApp.add_route('POST', '/pet', {
end end
MyApp.add_route('GET', '/pet/findByStatus', { MyApp.add_route('GET', '/v2/pet/findByStatus', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Finds Pets by status", "summary" => "Finds Pets by status",
"nickname" => "find_pets_by_status", "nickname" => "find_pets_by_status",
@ -85,7 +85,7 @@ MyApp.add_route('GET', '/pet/findByStatus', {
end end
MyApp.add_route('GET', '/pet/findByTags', { MyApp.add_route('GET', '/v2/pet/findByTags', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Finds Pets by tags", "summary" => "Finds Pets by tags",
"nickname" => "find_pets_by_tags", "nickname" => "find_pets_by_tags",
@ -115,7 +115,7 @@ MyApp.add_route('GET', '/pet/findByTags', {
end end
MyApp.add_route('GET', '/pet/{petId}', { MyApp.add_route('GET', '/v2/pet/{petId}', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Find pet by ID", "summary" => "Find pet by ID",
"nickname" => "get_pet_by_id", "nickname" => "get_pet_by_id",
@ -142,7 +142,7 @@ MyApp.add_route('GET', '/pet/{petId}', {
end end
MyApp.add_route('POST', '/pet/{petId}', { MyApp.add_route('POST', '/v2/pet/{petId}', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Updates a pet in the store with form data", "summary" => "Updates a pet in the store with form data",
"nickname" => "update_pet_with_form", "nickname" => "update_pet_with_form",
@ -169,7 +169,7 @@ MyApp.add_route('POST', '/pet/{petId}', {
end end
MyApp.add_route('DELETE', '/pet/{petId}', { MyApp.add_route('DELETE', '/v2/pet/{petId}', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "Deletes a pet", "summary" => "Deletes a pet",
"nickname" => "delete_pet", "nickname" => "delete_pet",
@ -203,7 +203,7 @@ MyApp.add_route('DELETE', '/pet/{petId}', {
end end
MyApp.add_route('POST', '/pet/{petId}/uploadImage', { MyApp.add_route('POST', '/v2/pet/{petId}/uploadImage', {
"resourcePath" => "/Pet", "resourcePath" => "/Pet",
"summary" => "uploads an image", "summary" => "uploads an image",
"nickname" => "upload_file", "nickname" => "upload_file",

View File

@ -1,7 +1,7 @@
require 'json' require 'json'
MyApp.add_route('GET', '/store/inventory', { MyApp.add_route('GET', '/v2/store/inventory', {
"resourcePath" => "/Store", "resourcePath" => "/Store",
"summary" => "Returns pet inventories by status", "summary" => "Returns pet inventories by status",
"nickname" => "get_inventory", "nickname" => "get_inventory",
@ -21,7 +21,7 @@ MyApp.add_route('GET', '/store/inventory', {
end end
MyApp.add_route('POST', '/store/order', { MyApp.add_route('POST', '/v2/store/order', {
"resourcePath" => "/Store", "resourcePath" => "/Store",
"summary" => "Place an order for a pet", "summary" => "Place an order for a pet",
"nickname" => "place_order", "nickname" => "place_order",
@ -48,7 +48,7 @@ MyApp.add_route('POST', '/store/order', {
end end
MyApp.add_route('GET', '/store/order/{orderId}', { MyApp.add_route('GET', '/v2/store/order/{orderId}', {
"resourcePath" => "/Store", "resourcePath" => "/Store",
"summary" => "Find purchase order by ID", "summary" => "Find purchase order by ID",
"nickname" => "get_order_by_id", "nickname" => "get_order_by_id",
@ -75,7 +75,7 @@ MyApp.add_route('GET', '/store/order/{orderId}', {
end end
MyApp.add_route('DELETE', '/store/order/{orderId}', { MyApp.add_route('DELETE', '/v2/store/order/{orderId}', {
"resourcePath" => "/Store", "resourcePath" => "/Store",
"summary" => "Delete purchase order by ID", "summary" => "Delete purchase order by ID",
"nickname" => "delete_order", "nickname" => "delete_order",

View File

@ -1,7 +1,7 @@
require 'json' require 'json'
MyApp.add_route('POST', '/user', { MyApp.add_route('POST', '/v2/user', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Create user", "summary" => "Create user",
"nickname" => "create_user", "nickname" => "create_user",
@ -28,7 +28,7 @@ MyApp.add_route('POST', '/user', {
end end
MyApp.add_route('POST', '/user/createWithArray', { MyApp.add_route('POST', '/v2/user/createWithArray', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Creates list of users with given input array", "summary" => "Creates list of users with given input array",
"nickname" => "create_users_with_array_input", "nickname" => "create_users_with_array_input",
@ -55,7 +55,7 @@ MyApp.add_route('POST', '/user/createWithArray', {
end end
MyApp.add_route('POST', '/user/createWithList', { MyApp.add_route('POST', '/v2/user/createWithList', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Creates list of users with given input array", "summary" => "Creates list of users with given input array",
"nickname" => "create_users_with_list_input", "nickname" => "create_users_with_list_input",
@ -82,7 +82,7 @@ MyApp.add_route('POST', '/user/createWithList', {
end end
MyApp.add_route('GET', '/user/login', { MyApp.add_route('GET', '/v2/user/login', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Logs user into the system", "summary" => "Logs user into the system",
"nickname" => "login_user", "nickname" => "login_user",
@ -122,7 +122,7 @@ MyApp.add_route('GET', '/user/login', {
end end
MyApp.add_route('GET', '/user/logout', { MyApp.add_route('GET', '/v2/user/logout', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Logs out current logged in user session", "summary" => "Logs out current logged in user session",
"nickname" => "logout_user", "nickname" => "logout_user",
@ -142,7 +142,7 @@ MyApp.add_route('GET', '/user/logout', {
end end
MyApp.add_route('GET', '/user/{username}', { MyApp.add_route('GET', '/v2/user/{username}', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Get user by user name", "summary" => "Get user by user name",
"nickname" => "get_user_by_name", "nickname" => "get_user_by_name",
@ -154,7 +154,7 @@ MyApp.add_route('GET', '/user/{username}', {
{ {
"name" => "username", "name" => "username",
"description" => "The name that needs to be fetched. Use user1 for testing. ", "description" => "The name that needs to be fetched. Use user1 for testing.",
"dataType" => "string", "dataType" => "string",
"paramType" => "path", "paramType" => "path",
}, },
@ -169,7 +169,7 @@ MyApp.add_route('GET', '/user/{username}', {
end end
MyApp.add_route('PUT', '/user/{username}', { MyApp.add_route('PUT', '/v2/user/{username}', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Updated user", "summary" => "Updated user",
"nickname" => "update_user", "nickname" => "update_user",
@ -203,7 +203,7 @@ MyApp.add_route('PUT', '/user/{username}', {
end end
MyApp.add_route('DELETE', '/user/{username}', { MyApp.add_route('DELETE', '/v2/user/{username}', {
"resourcePath" => "/User", "resourcePath" => "/User",
"summary" => "Delete user", "summary" => "Delete user",
"nickname" => "delete_user", "nickname" => "delete_user",

View File

@ -31,9 +31,10 @@ class Swaggering < Sinatra::Base
end end
def self.add_route(method, path, swag={}, opts={}, &block) def self.add_route(method, path, swag={}, opts={}, &block)
fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path #fullPath = swag["resourcePath"].to_s + @@configuration.format_specifier + path
fullPath = path.gsub(/{(.*)}/, ':\1')
accepted = case method accepted = case method.to_s.downcase
when 'get' when 'get'
get(fullPath, opts, &block) get(fullPath, opts, &block)
true true
@ -47,6 +48,7 @@ class Swaggering < Sinatra::Base
put(fullPath, opts, &block) put(fullPath, opts, &block)
true true
else else
puts "Error adding route: #{method} #{fullPath}"
false false
end end

View File

@ -7,3 +7,7 @@ class MyApp < Swaggering
end end
end end
# include the api files
Dir["./api/*.rb"].each { |file|
require file
}