forked from ruby-grape/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.rb
433 lines (381 loc) · 14.4 KB
/
api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
require 'rack/mount'
require 'rack/auth/basic'
require 'rack/auth/digest/md5'
require 'logger'
module Grape
# The API class is the primary entry point for
# creating Grape APIs. Users should subclass this
# class in order to build an API.
class API
class << self
attr_reader :route_set
attr_reader :versions
attr_reader :routes
attr_reader :settings
attr_writer :logger
def logger(logger = nil)
if logger
@logger = logger
else
@logger ||= Logger.new($stdout)
end
end
def reset!
@settings = Grape::Util::HashStack.new
@route_set = Rack::Mount::RouteSet.new
end
def call(env)
logger.info "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
route_set.freeze.call(env)
end
# Set a configuration value for this namespace.
#
# @param key [Symbol] The key of the configuration variable.
# @param value [Object] The value to which to set the configuration variable.
def set(key, value)
settings[key.to_sym] = value
end
# Add to a configuration value for this
# namespace.
#
# @param key [Symbol] The key of the configuration variable.
# @param value [Object] The value to which to set the configuration variable.
def imbue(key, value)
settings.imbue(key, value)
end
# Define a root URL prefix for your entire
# API.
def prefix(prefix = nil)
prefix ? set(:root_prefix, prefix) : settings[:root_prefix]
end
# Specify an API version.
#
# @example API with legacy support.
# class MyAPI < Grape::API
# version 'v2'
#
# get '/main' do
# {:some => 'data'}
# end
#
# version 'v1' do
# get '/main' do
# {:legacy => 'data'}
# end
# end
# end
#
def version(*args, &block)
if args.any?
options = args.pop if args.last.is_a? Hash
options ||= {}
options = {:using => :path}.merge!(options)
@versions = versions | args
nest(block) do
set(:version, args)
set(:version_options, options)
end
end
end
# Specify the default format for the API's
# serializers. Currently only `:json` is
# supported.
def default_format(new_format = nil)
new_format ? set(:default_format, new_format.to_sym) : settings[:default_format]
end
# Specify the format for error messages.
# May be `:json` or `:txt` (default).
def error_format(new_format = nil)
new_format ? set(:error_format, new_format.to_sym) : settings[:error_format]
end
# Specify the default status code for errors.
def default_error_status(new_status = nil)
new_status ? set(:default_error_status, new_status) : settings[:default_error_status]
end
# Allows you to rescue certain exceptions that occur to return
# a grape error rather than raising all the way to the
# server level.
#
# @example Rescue from custom exceptions
# class ExampleAPI < Grape::API
# class CustomError < StandardError; end
#
# rescue_from CustomError
# end
#
# @overload rescue_from(*exception_classes, options = {})
# @param [Array] exception_classes A list of classes that you want to rescue, or
# the symbol :all to rescue from all exceptions.
# @param [Block] block Execution block to handle the given exception.
# @param [Hash] options Options for the rescue usage.
# @option options [Boolean] :backtrace Include a backtrace in the rescue response.
def rescue_from(*args, &block)
if block_given?
args.each do |arg|
imbue(:rescue_handlers, { arg => block })
end
end
imbue(:rescue_options, args.pop) if args.last.is_a?(Hash)
set(:rescue_all, true) and return if args.include?(:all)
imbue(:rescued_errors, args)
end
# Allows you to specify a default representation entity for a
# class. This allows you to map your models to their respective
# entities once and then simply call `present` with the model.
#
# @example
# class ExampleAPI < Grape::API
# represent User, :with => Entity::User
#
# get '/me' do
# present current_user # :with => Entity::User is assumed
# end
# end
#
# Note that Grape will automatically go up the class ancestry to
# try to find a representing entity, so if you, for example, define
# an entity to represent `Object` then all presented objects will
# bubble up and utilize the entity provided on that `represent` call.
#
# @param model_class [Class] The model class that will be represented.
# @option options [Class] :with The entity class that will represent the model.
def represent(model_class, options)
raise ArgumentError, "You must specify an entity class in the :with option." unless options[:with] && options[:with].is_a?(Class)
imbue(:representations, model_class => options[:with])
end
# Add helper methods that will be accessible from any
# endpoint within this namespace (and child namespaces).
#
# When called without a block, all known helpers within this scope
# are included.
#
# @param mod [Module] optional module of methods to include
# @param &block [Block] optional block of methods to include
#
# @example Define some helpers.
# class ExampleAPI < Grape::API
# helpers do
# def current_user
# User.find_by_id(params[:token])
# end
# end
# end
def helpers(mod = nil, &block)
if block_given? || mod
mod ||= settings.peek[:helpers] || Module.new
mod.class_eval &block if block_given?
set(:helpers, mod)
else
mod = Module.new
settings.stack.each do |s|
mod.send :include, s[:helpers] if s[:helpers]
end
mod
end
end
# Add an authentication type to the API. Currently
# only `:http_basic`, `:http_digest` and `:oauth2` are supported.
def auth(type = nil, options = {}, &block)
if type
set(:auth, {:type => type.to_sym, :proc => block}.merge(options))
else
settings[:auth]
end
end
# Add HTTP Basic authorization to the API.
#
# @param [Hash] options A hash of options.
# @option options [String] :realm "API Authorization" The HTTP Basic realm.
def http_basic(options = {}, &block)
options[:realm] ||= "API Authorization"
auth :http_basic, options, &block
end
def http_digest(options = {}, &block)
options[:realm] ||= "API Authorization"
options[:opaque] ||= "secret"
auth :http_digest, options, &block
end
def mount(mounts)
mounts = {mounts => '/'} unless mounts.respond_to?(:each_pair)
mounts.each_pair do |app, path|
next unless app.respond_to?(:call)
route_set.add_route(app,
:path_info => compile_path(path, false)
)
end
end
# Defines a route that will be recognized
# by the Grape API.
#
# @param methods [HTTP Verb] One or more HTTP verbs that are accepted by this route. Set to `:any` if you want any verb to be accepted.
# @param paths [String] One or more strings representing the URL segment(s) for this route.
#
# @example Defining a basic route.
# class MyAPI < Grape::API
# route(:any, '/hello') do
# {:hello => 'world'}
# end
# end
def route(methods, paths = ['/'], route_options = {}, &block)
methods = Array(methods)
paths = ['/'] if ! paths || paths == []
paths = Array(paths)
endpoint = build_endpoint(&block)
route_options ||= {}
methods.each do |method|
paths.each do |path|
prepared_path = prepare_path(path)
path = compile_path(path)
regex = Rack::Mount::RegexpWithNamedGroups.new(path)
path_params = regex.named_captures.map { |nc| nc[0] } - [ 'version', 'format' ]
path_params |= (route_options[:params] || [])
request_method = (method.to_s.upcase unless method == :any)
routes << Route.new(route_options.merge({
:prefix => prefix,
:version => settings[:version] ? settings[:version].join('|') : nil,
:namespace => namespace,
:method => request_method,
:path => prepared_path,
:params => path_params}))
route_set.add_route(endpoint,
:path_info => path,
:request_method => request_method
)
end
end
end
def before(&block)
settings.imbue(:befores, [block])
end
def after(&block)
settings.imbue(:afters, [block])
end
def get(paths = ['/'], options = {}, &block); route('GET', paths, options, &block) end
def post(paths = ['/'], options = {}, &block); route('POST', paths, options, &block) end
def put(paths = ['/'], options = {}, &block); route('PUT', paths, options, &block) end
def head(paths = ['/'], options = {}, &block); route('HEAD', paths, options, &block) end
def delete(paths = ['/'], options = {}, &block); route('DELETE', paths, options, &block) end
def options(paths = ['/'], options = {}, &block); route('OPTIONS', paths, options, &block) end
def namespace(space = nil, &block)
if space || block_given?
nest(block) do
set(:namespace, space.to_s) if space
end
else
Rack::Mount::Utils.normalize_path(settings.stack.map{|s| s[:namespace]}.join('/'))
end
end
alias_method :group, :namespace
alias_method :resource, :namespace
alias_method :resources, :namespace
alias_method :segment, :namespace
# Create a scope without affecting the URL.
#
# @param name [Symbol] Purely placebo, just allows to to name the scope to make the code more readable.
def scope(name = nil, &block)
nest(block)
end
# Apply a custom middleware to the API. Applies
# to the current namespace and any children, but
# not parents.
#
# @param middleware_class [Class] The class of the middleware you'd like
# to inject.
def use(middleware_class, *args)
settings.imbue(:middleware, [[middleware_class, *args]])
end
# Retrieve an array of the middleware classes
# and arguments that are currently applied to the
# application.
def middleware
settings.stack.inject([]){|a,s| a += s[:middleware] if s[:middleware]; a}
end
# An array of API routes.
def routes
@routes ||= []
end
def versions
@versions ||= []
end
protected
# Execute first the provided block, then each of the
# block passed in. Allows for simple 'before' setups
# of settings stack pushes.
def nest(*blocks, &block)
blocks.reject!{|b| b.nil?}
if blocks.any?
settings.push # create a new context to eval the follow
instance_eval &block if block_given?
blocks.each{|b| instance_eval &b}
settings.pop # when finished, we pop the context
else
instance_eval &block
end
end
def aggregate_setting(key)
settings.stack.inject([]) do |aggregate, frame|
aggregate += (frame[key] || [])
end
end
def build_endpoint(&block)
b = Rack::Builder.new
b.use Grape::Middleware::Error,
:default_status => settings[:default_error_status] || 403,
:rescue_all => settings[:rescue_all],
:rescued_errors => settings[:rescued_errors],
:format => settings[:error_format] || :txt,
:rescue_options => settings[:rescue_options],
:rescue_handlers => settings[:rescue_handlers] || {}
b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic
b.use Rack::Auth::Digest::MD5, settings[:auth][:realm], settings[:auth][:opaque], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_digest
b.use Grape::Middleware::Prefixer, :prefix => prefix if prefix
if settings[:version]
b.use Grape::Middleware::Versioner.using(settings[:version_options][:using]), {
:versions => settings[:version],
:version_options => settings[:version_options]
}
end
b.use Grape::Middleware::Formatter, :default_format => default_format || :json
middleware.each{|m| b.use *m }
befores = aggregate_setting(:befores)
afters = aggregate_setting(:afters)
representations = settings[:representations] || {}
endpoint = Grape::Endpoint.generate({
:befores => befores,
:afters => afters,
:representations => representations
}, &block)
endpoint.send :include, helpers
b.run endpoint
b.to_app
end
def inherited(subclass)
subclass.reset!
subclass.logger = logger.clone
end
def inherit(other_stack)
# settings stack should know how to merge aggregate keys / values
# settings_stack.unshift *other_stack
# raise settings_stack.inspect
end
def route_set
@route_set ||= Rack::Mount::RouteSet.new
end
def prepare_path(path)
parts = []
parts << prefix if prefix
parts << ':version' if settings[:version] && settings[:version_options][:using] == :path
parts << namespace.to_s if namespace
parts << path.to_s if path && '/' != path
parts.last << '(.:format)'
Rack::Mount::Utils.normalize_path(parts.join('/'))
end
def compile_path(path, anchor = true)
endpoint_options = {}
endpoint_options[:version] = /#{settings[:version].join('|')}/ if settings[:version]
Rack::Mount::Strexp.compile(prepare_path(path), endpoint_options, %w( / . ? ), anchor)
end
end
reset!
end
end