Skip to content

Commit

Permalink
Switch usage syntax to use -- it reads better.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Sep 23, 2012
1 parent e4cfbae commit bbd0ecc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions lib/interpol/sinatra/request_params_parser.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/interpol/sinatra/request_params_parser_spec.rb
Expand Up @@ -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)
Expand Down

0 comments on commit bbd0ecc

Please sign in to comment.