Skip to content

Instantly share code, notes, and snippets.

@tsnow
Last active August 29, 2015 13:57
Show Gist options
  • Save tsnow/9558002 to your computer and use it in GitHub Desktop.
Save tsnow/9558002 to your computer and use it in GitHub Desktop.
# 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
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
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
@wbrady
Copy link

wbrady commented Mar 17, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment