Last active
August 29, 2015 13:57
-
-
Save tsnow/9558002 to your computer and use it in GitHub Desktop.
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
# app/resourceful_api/resourceful_actions.rb | |
# Requires #params, #render, Presenters::Api | |
module ResourcefulAction | |
def presents_json_endpoints(service, *actions) | |
respond_to :json | |
actions.each do |action| | |
define_method action do | |
resp = Presenters::Api.call(service,action, params) | |
render :json => resp, :status => resp[:status] | |
end | |
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
class RidesController < ApplicationController | |
skip_before_filter :verify_authenticity_token | |
extend ResourcefulActions | |
presents_json_endpoints Services::Ride, | |
:send_payment_confirmation, | |
:book, | |
:confirm, | |
:cancel, | |
:pay, | |
:rate, | |
:status | |
end | |
#vs: | |
class RidesController < ApplicationController | |
skip_before_filter :verify_authenticity_token | |
respond_to :json | |
def book | |
resp = Presenters::Api.call(Services::Ride, :book, params) | |
render json: resp, status: resp.status | |
end | |
def confirm | |
resp = Presenters::Api.call(Services::Ride, :confirm, params) | |
render json: resp, status: resp.status | |
end | |
def cancel | |
resp = Presenters::Api.call(Services::Ride, :cancel, params) | |
render json: resp, status: resp.status | |
end | |
def pay | |
resp = Presenters::Api.call(Services::Ride, :pay, params) | |
render json: resp, status: resp.status | |
end | |
def rate | |
resp = Presenters::Api.call(Services::Ride, :rate, params) | |
render json: resp, status: resp.status | |
end | |
def status | |
resp = Presenters::Api.call(Services::Ride, :status, params) | |
render json: resp, status: resp.status | |
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
class MessagesController < ApplicationController | |
extend ResourcefulActions | |
presents_json_endpoints Services::Ride, :send_payment_confirmation | |
end | |
# vs: | |
class MessagesController < ApplicationController | |
respond_to :json | |
def send_payment_confirmation | |
resp = Presenters::Api.call(Services::Ride, :send_payment_confirmation, params) | |
render json: resp, status: resp.status | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's pretty slick. I was thinking we might need custom params filtering for each action but each service/call that needs something from the params can return errors if something is missing or invalid.