-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from ifeelgoods/generate_api_route
Instead of manually redefine the route for the action, retrieve it from the routes of the application.
- Loading branch information
Showing
10 changed files
with
212 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Apipie | ||
class RoutesFormatter | ||
API_METHODS = %w{GET POST PUT PATCH OPTIONS DELETE} | ||
|
||
# The entry method called by Apipie to extract the array | ||
# representing the api dsl from the routes definition. | ||
def format_routes(rails_routes, args) | ||
rails_routes.map { |rails_route| format_route(rails_route, args) } | ||
end | ||
|
||
def format_route(rails_route, args) | ||
{ :path => format_path(rails_route), | ||
:verb => format_verb(rails_route), | ||
:desc => args[:desc], | ||
:options => args[:options] } | ||
end | ||
|
||
def format_path(rails_route) | ||
rails_route.path.spec.to_s.gsub('(.:format)', '') | ||
end | ||
|
||
def format_verb(rails_route) | ||
verb = API_METHODS.select{|defined_verb| defined_verb =~ /\A#{rails_route.verb}\z/} | ||
if verb.count != 1 | ||
verb = API_METHODS.select{|defined_verb| defined_verb == rails_route.constraints[:method]} | ||
if verb.blank? | ||
raise "Unknow verb #{rails_route.path.spec.to_s}" | ||
end | ||
end | ||
verb.first | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters