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
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'
get(fullPath, opts, &block)
true
@ -47,6 +48,7 @@ class Swaggering < Sinatra::Base
put(fullPath, opts, &block)
true
else
puts "Error adding route: #{method} #{fullPath}"
false
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -31,9 +31,10 @@ class Swaggering < Sinatra::Base
end
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'
get(fullPath, opts, &block)
true
@ -47,6 +48,7 @@ class Swaggering < Sinatra::Base
put(fullPath, opts, &block)
true
else
puts "Error adding route: #{method} #{fullPath}"
false
end

View File

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