module Hippo::API::RequestWrapper
Constants
- DEFAULT_OPTIONS
Public Class Methods
delete(*args)
click to toggle source
# File lib/hippo/api/request_wrapper.rb, line 29 def delete(*args) make_handler(*args) do |controller| controller.destroy end end
get(*args)
click to toggle source
# File lib/hippo/api/request_wrapper.rb, line 11 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/hippo/api/request_wrapper.rb, line 36 def make_handler(model, controller, options = {}) lambda do authentication = Hippo::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/hippo/api/request_wrapper.rb, line 17 def post(*args) make_handler(*args) do |controller| controller.create end end
update(*args)
click to toggle source
# File lib/hippo/api/request_wrapper.rb, line 23 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/hippo/api/request_wrapper.rb, line 59 def with_authenticated_user(options = {with_transaction: true}) role = options[:role] lambda do authentication = Hippo::API::AuthenticationProvider.new(request) user = authentication.current_user if user && (role.nil? || 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/hippo/api/request_wrapper.rb, line 115 def log_request Hippo.logger.info "UserID: #{session['user_id']}, Params: #{request.params}" Hippo.logger.debug JSON.pretty_generate(data) unless Hippo.env.production? or data.nil? end
with_user(options = DEFAULT_OPTIONS) { |current_user| ... }
click to toggle source
# File lib/hippo/api/request_wrapper.rb, line 75 def with_user(options = DEFAULT_OPTIONS) authentication = Hippo::API::AuthenticationProvider.new(request) wrap_reply(options) do yield authentication.current_user end end
wrap_reply(options = DEFAULT_OPTIONS) { ||| {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 @option opts [Boolean] :require_tenant return error if tenant is not found
# File lib/hippo/api/request_wrapper.rb, line 87 def wrap_reply(options = DEFAULT_OPTIONS) if options[:require_tenant] && Hippo::Tenant.current.nil? return json_reply( { success: false, message: "invalid address", errors: { address: 'invalid' } } ) end response = { success: false, message: "No response was generated" } log_request if options[:with_transaction] Hippo::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 !Hippo.env.production? && request.env['HTTP_X_ROLLBACK_AFTER_REQUEST'] Hippo::Model.connection.rollback_db_transaction end end else response = yield end if false == response[:success] status(406) end json_reply response end