Sha256: 8b09fd1be8ca2a89e439ded1ad5b84c492d809cf1c5c151323bbc48d78c2046c

Contents?: true

Size: 1.94 KB

Versions: 2

Compression:

Stored size: 1.94 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)
WeaselDiesel::RequestHandler.send(:include, WDSinatraHooks)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
wd_sinatra-0.2.1 templates/lib/hooks.rb
wd_sinatra-0.2.0 templates/lib/hooks.rb