Module: Grape::DSL::InsideRoute

Defined in:
lib/ruby-swagger/grape/grape_presenter.rb

Instance Method Summary collapse

Instance Method Details

#api_present(*args) ⇒ Object



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
# File 'lib/ruby-swagger/grape/grape_presenter.rb', line 6

def api_present(*args)
  args_list = args || []
  options = {}

  # Initialize the options hash - either by assigning to the current options for the method or with a new one
  if args_list.count == 2

    if args_list.last.is_a?(Hash)
      options = args_list.last
    else
      raise ArgumentError.new "The expected second argument for api_present is a Hash, but I got a #{args_list.last.class}"
    end

  elsif args_list.count == 1

    # Initialize the option list
    args_list << options

  elsif args_list.count > 2 || args_list.count == 0
    raise ArgumentError.new "Invalid number of arguments - got #{args_list.count}. expected 1 or 2 parameters"
  end

  # Setting the grape :with
  if route.route_response.present? && route.route_response[:entity].present? && !options[:with].present? && route.route_response[:entity].is_a?(Class)
    options[:with] = route.route_response[:entity]
  end

  # Setting the grape :root
  if route.route_response.present? && route.route_response[:root].present? && !options[:root].present? && route.route_response[:root].is_a?(String)
    options[:root] = route.route_response[:root]
  end

  # Setting the :current_user extension
  options[:current_user] = current_user if defined?(current_user)

  present(*args_list)
end