forked from sinatra/sinatra-contrib
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request sinatra#131 from dtaniwaki/add-required-params
Add required params method
- Loading branch information
Showing
5 changed files
with
145 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ module Common | |
helpers :JSON | ||
helpers :LinkHeader | ||
helpers :Streaming | ||
helpers :Params | ||
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,71 @@ | ||
require 'sinatra/base' | ||
|
||
module Sinatra | ||
# = Sinatra::Params | ||
# | ||
# Ensure required query parameters | ||
# | ||
# == Usage | ||
# | ||
# Set required query parameter keys in the argument. | ||
# It'll halt with 400 if requried keys don't exist. | ||
# | ||
# get '/simple_keys' do | ||
# required_params :p1, :p2 | ||
# end | ||
# | ||
# Complicated pattern is also fine. | ||
# | ||
# get '/complicated_keys' do | ||
# required_params :p1, :p2 => [:p3, :p4] | ||
# end | ||
# | ||
# === Classic Application | ||
# | ||
# In a classic application simply require the helpers, and start using them: | ||
# | ||
# require "sinatra" | ||
# require "sinatra/params" | ||
# | ||
# # The rest of your classic application code goes here... | ||
# | ||
# === Modular Application | ||
# | ||
# In a modular application you need to require the helpers, and then tell | ||
# the application to use them: | ||
# | ||
# require "sinatra/base" | ||
# require "sinatra/params" | ||
# | ||
# class MyApp < Sinatra::Base | ||
# helpers Sinatra::Params | ||
# | ||
# # The rest of your modular application code goes here... | ||
# end | ||
# | ||
module Params | ||
def required_params(*keys) | ||
_required_params(params, *keys) | ||
end | ||
|
||
private | ||
|
||
def _required_params(p, *keys) | ||
keys.each do |key| | ||
if key.is_a?(Hash) | ||
_required_params(p, *key.keys) | ||
key.each do |k, v| | ||
_required_params(p[k.to_s], v) | ||
end | ||
elsif key.is_a?(Array) | ||
_required_params(p, *key) | ||
else | ||
halt 400 unless p.has_key?(key.to_s) | ||
end | ||
end | ||
true | ||
end | ||
end | ||
|
||
helpers Params | ||
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
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,68 @@ | ||
require_relative 'spec_helper' | ||
|
||
describe Sinatra::Params do | ||
context "#required_params" do | ||
context "simple keys" do | ||
before do | ||
mock_app do | ||
helpers Sinatra::Params | ||
get('/') { required_params(:p1, :p2) } | ||
end | ||
end | ||
it 'return 400 if required params do not exist' do | ||
get('/') | ||
expect(last_response.status).to eq(400) | ||
end | ||
it 'return 400 if required params do not exist partially' do | ||
get('/', :p1 => 1) | ||
expect(last_response.status).to eq(400) | ||
end | ||
it 'return 200 if required params exist' do | ||
get('/', :p1 => 1, :p2 => 2) | ||
expect(last_response.status).to eq(200) | ||
end | ||
it 'return 200 if required params exist with array' do | ||
get('/', :p1 => 1, :p2 => [31, 32, 33]) | ||
expect(last_response.status).to eq(200) | ||
end | ||
end | ||
context "hash keys" do | ||
before do | ||
mock_app do | ||
helpers Sinatra::Params | ||
get('/') { required_params(:p1, :p2 => :p21) } | ||
end | ||
end | ||
it 'return 400 if required params do not exist' do | ||
get('/') | ||
expect(last_response.status).to eq(400) | ||
end | ||
it 'return 200 if required params exist' do | ||
get('/', :p1 => 1, :p2 => {:p21 => 21}) | ||
expect(last_response.status).to eq(200) | ||
end | ||
end | ||
context "complex keys" do | ||
before do | ||
mock_app do | ||
helpers Sinatra::Params | ||
get('/') { required_params(:p1 => [:p11, {:p12 => :p121, :p122 => [:p123, {:p124 => :p1241}]}]) } | ||
end | ||
end | ||
it 'return 400 if required params do not exist' do | ||
get('/') | ||
expect(last_response.status).to eq(400) | ||
end | ||
it 'return 200 if required params exist' do | ||
get('/', :p1 => {:p11 => 11, :p12 => {:p121 => 121}, :p122 => {:p123 => 123, :p124 => {:p1241 => 1241}}}) | ||
expect(last_response.status).to eq(200) | ||
end | ||
end | ||
end | ||
|
||
context "#_required_params" do | ||
it "is invisible" do | ||
expect { _required_params }.to raise_error(NameError) | ||
end | ||
end | ||
end |