Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switch usage syntax to `use` -- it reads better.

  • Loading branch information...
commit bbd0ecc63a9d7252c576a7304e662d37c8b180a4 1 parent e4cfbae
@myronmarston myronmarston authored
View
27 lib/interpol/sinatra/request_params_parser.rb
@@ -2,15 +2,34 @@
module Interpol
module Sinatra
- module RequestParamsParser
- def self.add_to(app, &block)
+ # Parses and validates a sinatra params hash based on the
+ # endpoint definitions.
+ # Note that you use this like a sinatra middleware
+ # (using a `use` directive in the body of the sinatra class), but
+ # it hooks into sinatra differently so that it has access to the params.
+ # It's more like a mix-in, honestly, but we piggyback on `use` so that
+ # it can take a config block.
+ class RequestParamsParser
+ def initialize(app, &block)
+ @app = app
+ hook_into(app, &block)
+ end
+
+ def call(env)
+ @app.call(env)
+ end
+
+ private
+
+ def hook_into(app, &block)
+ return if defined?(app.settings.interpol_config)
config = Configuration.default.customized_duplicate(&block)
- app.class_eval do
+ app.class.class_eval do
alias unparsed_params params
helpers SinatraHelpers
set :interpol_config, config
- enable :parse_params
+ enable :parse_params unless settings.respond_to?(:parse_params)
include SinatraOverriddes
end
end
View
2  spec/unit/interpol/sinatra/request_params_parser_spec.rb
@@ -26,7 +26,7 @@ def configure_parser(&block)
_endpoint = endpoint
::Sinatra.new do
- RequestParamsParser.add_to self do |config|
+ use RequestParamsParser do |config|
config.endpoints = [_endpoint]
config.api_version '1.0'
parser_configuration.call(config)
Please sign in to comment.
Something went wrong with that request. Please try again.