File: callbacks.rb

package info (click to toggle)
ruby-grape 1.6.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,156 kB
  • sloc: ruby: 25,265; makefile: 7
file content (71 lines) | stat: -rw-r--r-- 2,129 bytes parent folder | download
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
# frozen_string_literal: true

require 'active_support/concern'

module Grape
  module DSL
    # Blocks can be executed before or after every API call, using `before`, `after`,
    # `before_validation` and `after_validation`.
    #
    # Before and after callbacks execute in the following order:
    #
    # 1. `before`
    # 2. `before_validation`
    # 3. _validations_
    # 4. `after_validation`
    # 5. _the API call_
    # 6. `after`
    #
    # Steps 4, 5 and 6 only happen if validation succeeds.
    module Callbacks
      extend ActiveSupport::Concern

      include Grape::DSL::Configuration

      module ClassMethods
        # Execute the given block before validation, coercion, or any endpoint
        # code is executed.
        def before(&block)
          namespace_stackable(:befores, block)
        end

        # Execute the given block after `before`, but prior to validation or
        # coercion.
        def before_validation(&block)
          namespace_stackable(:before_validations, block)
        end

        # Execute the given block after validations and coercions, but before
        # any endpoint code.
        def after_validation(&block)
          namespace_stackable(:after_validations, block)
        end

        # Execute the given block after the endpoint code has run.
        def after(&block)
          namespace_stackable(:afters, block)
        end

        # Allows you to specify a something that will always be executed after a call
        # API call. Unlike the `after` block, this code will run even on
        # unsuccesful requests.
        # @example
        #   class ExampleAPI < Grape::API
        #     before do
        #       ApiLogger.start
        #     end
        #     finally do
        #       ApiLogger.close
        #     end
        #   end
        #
        # This will make sure that the ApiLogger is opened and closed around every
        # request
        # @param  ensured_block [Proc] The block to be executed after every api_call
        def finally(&block)
          namespace_stackable(:finallies, block)
        end
      end
    end
  end
end