module Lanes::API::RequestWrapper
Public Class Methods
delete(*args)
click to toggle source
# File lib/lanes/api/request_wrapper.rb, line 25 def delete(*args) make_handler(*args) do |controller| controller.destroy end end
get(*args)
click to toggle source
# File lib/lanes/api/request_wrapper.rb, line 7 def get(*args) make_handler(*args) do |controller| controller.show end end
make_handler(model, controller, options = {}) { |controller| ... }
click to toggle source
@!visibility private
# File lib/lanes/api/request_wrapper.rb, line 32 def make_handler(model, controller, options = {}) lambda do authentication = Lanes::API::AuthenticationProvider.new(request) authentication.wrap_model_access(model, self, options) do if options[:parent_attribute] params[:nested_attribute] = Hash[ options[:parent_attribute], params[parent_attribute] ] end wrap_reply(options.reverse_merge(with_transaction: !request.get?)) do yield controller.new(model, authentication, params, data) end end end end
post(*args)
click to toggle source
# File lib/lanes/api/request_wrapper.rb, line 13 def post(*args) make_handler(*args) do |controller| controller.create end end
update(*args)
click to toggle source
# File lib/lanes/api/request_wrapper.rb, line 19 def update(*args) make_handler(*args) do |controller| controller.update end end
with_authenticated_user(options = {with_transaction: true}) { |current_user, self| ... }
click to toggle source
Ensure request is performed with a logged in user. The provided block will be called with |user, request|
@param [options] options for additional checks @option options [String] :role A role name that the user must have @option opts [Boolean] :with_transaction rollback DB
transaction if exceptions occur
# File lib/lanes/api/request_wrapper.rb, line 55 def with_authenticated_user(options = {with_transaction: true}) role = options[:role] lambda do authentication = Lanes::API::AuthenticationProvider.new(request) user = authentication.current_user if user and ( role.nil? or user.roles.include?(role) ) wrap_reply(options) do yield authentication.current_user, self end else authentication.fail_request(self) end end end
Public Instance Methods
log_request()
click to toggle source
Logs UserID and params for a request. In non-production, the JSON payload is also logged
# File lib/lanes/api/request_wrapper.rb, line 97 def log_request Lanes.logger.info "UserID: #{session['user_id']}, Params: #{request.params}" Lanes.logger.debug JSON.pretty_generate(data) unless Lanes.env.production? or data.nil? end
wrap_reply(options = {with_transaction: true}) { ||| {success: false}| ... }
click to toggle source
Wraps a HTTP request in an optional DB
transaction and converts yeilded data to JSON
@param [options] options for additional checks @option opts [Boolean] :with_transaction rollback DB
transaction if exceptions occur
# File lib/lanes/api/request_wrapper.rb, line 75 def wrap_reply(options = {with_transaction: true}) response = { success: false, message: "No response was generated" } log_request if options[:with_transaction] Lanes::Model.transaction do response = yield || {success: false} # This is quite possibly a horrible idea. # It enables test specs to reset the db state after a request if !Lanes.env.production? && request.env['HTTP_X_ROLLBACK_AFTER_REQUEST'] Lanes::Model.connection.rollback_db_transaction end end else response = yield end if false == response[:success] status(406) end json_reply response end