Sha256: f7fbe015f62ada5a975df9de60a4ee5c1dcfc98f6f41762fa0a3339398f8819c

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

require 'body_parser' # from the lib folder
# Change the JSON parser if you want to use Yajl for instance.
require 'json'
BodyParser.json_parser = JSON

module WDSinatraHooks

  MOBILE_X_HEADER   = 'HTTP_X_MOBILE_TOKEN'
  INTERNAL_X_HEADER = 'HTTP_X_INTERNAL_API_KEY'
  JSON_BODY_VERBS   =  %w(POST PUT DELETE)

  ####### HOOKS #############################
  #
  # This hook gets called before the params
  # are being verified. It's a good place to add a content type
  # handler for instance like shown below.
  # If you don't need this hook, just delete it.
  # Note that you have access to the request context, see sinatra_ext.rb
  # in the wd_sinatra repository to see how this is implemented.
  #
  # @param [Hash] params the incoming params.
  # processed.
  # @returns [Hash] the pre processed params.
  def params_preprocessor_hook(params)
    if JSON_BODY_VERBS.include?(request.request_method)
      BodyParser.parse(params, request.body, request.content_type)
    else
      params
    end
  end

  # This hook gets called after the params are being verified.
  # You can use this hook to modify the params before sending them to
  # your service.
  #
  # @param [Hash] params the incoming params.
  # processed.
  # @returns [Hash] the post processed params.
  # def params_postprocessor_hook(params)
  # end

  # This hook gets called before dispatching any
  # requests.
  # 
  # Implementation example
  def pre_dispatch_hook
    if service.extra[:mobile]
      mobile_auth_check
    elsif service.extra[:internal]
      internal_api_key_check
    elsif !service.auth_required
      return
    else
      halt 403 # protect by default
    end
  end
  #
  #########################################

  # AUTHENTICATION

  # Implementation example
  def mobile_auth_check
    true
  end

  # Implementation example
  def internal_api_key_check
    true
  end

end

Sinatra::Helpers.send(:include, WDSinatraHooks)

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wd_sinatra-0.1.0 templates/config/hooks.rb
wd_sinatra-0.0.2 templates/config/hooks.rb
wd_sinatra-0.0.1 templates/config/hooks.rb