Skip to content

Commit 005fcd1

Browse files
author
Michael Bleigh
committed
Add .scope to API to allow for scoping without modifying the URL.
1 parent 63fdf74 commit 005fcd1

File tree

3 files changed

+54
-9
lines changed

3 files changed

+54
-9
lines changed

lib/grape/api.rb

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
require 'logger'
44

55
module Grape
6+
# The API class is the primary entry point for
7+
# creating Grape APIs. Users should subclass this
8+
# class in order to build an API.
69
class API
7-
module Helpers; end
8-
910
class << self
1011
attr_reader :route_set
1112

@@ -147,27 +148,34 @@ def namespace(space = nil, &block)
147148
end
148149
end
149150

151+
alias_method :group, :namespace
152+
alias_method :resource, :namespace
153+
alias_method :resources, :namespace
154+
155+
# Create a scope without affecting the URL.
156+
#
157+
# @param name [Symbol] Purely placebo, just allows to to name the scope to make the code more readable.
158+
def scope(name = nil, &block)
159+
nest(block)
160+
end
161+
162+
protected
163+
150164
# Execute first the provided block, then each of the
151165
# block passed in. Allows for simple 'before' setups
152166
# of settings stack pushes.
153167
def nest(*blocks, &block)
154168
blocks.reject!{|b| b.nil?}
155169
if blocks.any?
156170
settings_stack << {}
157-
instance_eval &block
171+
instance_eval &block if block_given?
158172
blocks.each{|b| instance_eval &b}
159173
settings_stack.pop
160174
else
161175
instance_eval &block
162176
end
163177
end
164178

165-
alias_method :group, :namespace
166-
alias_method :resource, :namespace
167-
alias_method :resources, :namespace
168-
169-
protected
170-
171179
def build_endpoint(&block)
172180
b = Rack::Builder.new
173181
b.use Grape::Middleware::Error

lib/grape/endpoint.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def self.call(env)
2525

2626
attr_reader :env, :request
2727

28+
# The parameters passed into the request as
29+
# well as parsed from URL segments.
2830
def params
2931
@params ||= request.params.merge(env['rack.routing_args'] || {}).inject({}) do |h,(k,v)|
3032
h[k.to_s] = v
@@ -33,13 +35,21 @@ def params
3335
end
3436
end
3537

38+
# The API version as specified in the URL.
3639
def version; env['api.version'] end
3740

41+
# End the request and display an error to the
42+
# end user with the specified message.
43+
#
44+
# @param message [String] The message to display.
45+
# @param status [Integer] the HTTP Status Code. Defaults to 403.
3846
def error!(message, status=403)
3947
throw :error, :message => message, :status => status
4048
end
4149

4250
# Set or retrieve the HTTP status code.
51+
#
52+
# @param status [Integer] The HTTP Status Code to return for this request.
4353
def status(status = nil)
4454
if status
4555
@status = status

spec/grape/api_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,31 @@ def two
313313
lambda{get '/howdy'}.should_not raise_error
314314
end
315315
end
316+
317+
describe '.scope' do
318+
it 'should scope the various settings' do
319+
subject.version 'v2'
320+
321+
subject.scope :legacy do
322+
version 'v1'
323+
324+
get '/abc' do
325+
version
326+
end
327+
end
328+
329+
subject.get '/def' do
330+
version
331+
end
332+
333+
get '/v2/abc'
334+
last_response.status.should == 404
335+
get '/v1/abc'
336+
last_response.status.should == 200
337+
get '/v1/def'
338+
last_response.status.should == 404
339+
get '/v2/def'
340+
last_response.status.should == 200
341+
end
342+
end
316343
end

0 commit comments

Comments
 (0)