-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Middleware to parse Rails style url parameters #5
Open
rsslldnphy
wants to merge
2
commits into
postrank-labs:master
Choose a base branch
from
rsslldnphy:parse_rails_style_url_params
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
require 'rack/utils' | ||
|
||
module Goliath | ||
module Contrib | ||
|
||
# A middleware to parse Rails-style path parameters. This will parse | ||
# parameters from the request path based on the supplied pattern and | ||
# place them in the _params_ hash of the Goliath::Env for the request. | ||
# | ||
# @example | ||
# use Goliath::Contrib::Rack::PathParams, '/records/:artist/:title' | ||
# | ||
class PathParams | ||
|
||
module Parser | ||
|
||
PARAM = %r{:([^/]+)(\/|$)} | ||
CAPTURE_GROUP = '(?<\1>[^\/\?]+)\2' | ||
|
||
def retrieve_params(env) | ||
md = matched_params(env) | ||
md.names.each_with_object({}) do |key, params| | ||
params[key] = md[key] | ||
end | ||
end | ||
|
||
def matched_params(env) | ||
request_path(env).match(regexp).tap do |matched| | ||
raise Goliath::Validation::BadRequestError, "Request path does not match expected pattern: #{request_path(env)}" if matched.nil? | ||
end | ||
end | ||
|
||
def request_path(env) | ||
env[Goliath::Request::REQUEST_PATH] | ||
end | ||
|
||
def regexp | ||
@regexp ||= %r{^#{@url_pattern.gsub(PARAM, CAPTURE_GROUP)}\/?$} | ||
end | ||
|
||
end | ||
|
||
include Goliath::Rack::Validator | ||
include Parser | ||
|
||
def initialize(app, url_pattern) | ||
@app = app | ||
@url_pattern = url_pattern | ||
end | ||
|
||
def call(env) | ||
Goliath::Rack::Validator.safely(env) do | ||
env['params'] ||= {} | ||
env['params'].merge! retrieve_params(env) | ||
@app.call(env) | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require 'spec_helper' | ||
require 'goliath/contrib/path_params' | ||
|
||
describe Goliath::Contrib::PathParams do | ||
it 'accepts an app and a url pattern' do | ||
lambda { Goliath::Contrib::PathParams.new('my app', 'url') }.should_not raise_error | ||
end | ||
|
||
describe 'with middleware' do | ||
before(:each) do | ||
@app = mock('app').as_null_object | ||
@env = { 'REQUEST_PATH' => '/records/the_fall/hex_enduction_hour' } | ||
@path_params = Goliath::Contrib::PathParams.new(@app, '/records/:artist/:title') | ||
end | ||
|
||
it 'returns the app status, headers and body' do | ||
app_headers = {'Content-Type' => 'app'} | ||
app_body = {'b' => 'c'} | ||
@app.should_receive(:call).and_return([201, app_headers, app_body]) | ||
|
||
status, headers, body = @path_params.call(@env) | ||
status.should == 201 | ||
headers.should == app_headers | ||
body.should == app_body | ||
end | ||
|
||
context 'a request that matches the url pattern' do | ||
before do | ||
@env = { 'REQUEST_PATH' => '/records/sparks/angst_in_my_pants' } | ||
end | ||
|
||
it 'parses the params from the path' do | ||
@path_params.call(@env) | ||
@env['params']['artist'].should == 'sparks' | ||
@env['params']['title'].should == 'angst_in_my_pants' | ||
end | ||
end | ||
|
||
context 'a request that does not match the url pattern' do | ||
before do | ||
@env = { 'REQUEST_PATH' => '/animals/cat/noah' } | ||
end | ||
|
||
it 'raises a BadRequestError' do | ||
expect{ @path_params.retrieve_params(@env) }.to raise_error(Goliath::Validation::BadRequestError) | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
require 'goliath/contrib/path_params' | ||
|
||
class RailsStyleParams < Goliath::API | ||
use Goliath::Contrib::PathParams, '/kittens/:name/:favourite_colour' | ||
|
||
def response(env) | ||
[200, {}, 'OK'] | ||
end | ||
end | ||
|
||
describe RailsStyleParams do | ||
let(:err) { Proc.new { fail "API request failed" } } | ||
|
||
context 'a valid path' do | ||
it 'returns OK' do | ||
with_api(RailsStyleParams) do | ||
get_request({:path => '/kittens/ginger/blue'}, err) do |c| | ||
c.response.should == 'OK' | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'an invalid path' do | ||
it 'returns an error' do | ||
with_api(RailsStyleParams) do | ||
get_request({:path => '/donkeys/toothy'}, err) do |c| | ||
c.response.should == '[:error, "Request path does not match expected pattern: /donkeys/toothy"]' | ||
end | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why this has to be exclusive? As in, currently if you include this middleware then you can't use any other type of route.