Class: Grape::API

Inherits:
Object
  • Object
show all
Defined in:
lib/grape-dsl/dsl.rb,
lib/grape-dsl/mounter.rb

Overview

The API class is the primary entry point for creating Grape APIs.Users should subclass this class in order to build an API.

Class Method Summary collapse

Class Method Details

.console_write_out_routesObject

write out to the console the class routes



32
33
34
35
36
37
38
39
40
# File 'lib/grape-dsl/dsl.rb', line 32

def console_write_out_routes

  $stdout.puts "\n\nREST::API ROUTES:"
  self.routes.map do |route|
    $stdout.puts "\t#{route.route_method}","#{route.route_path}"
  end

  return nil
end

.mount_method(*args) ⇒ Object

Args will be seperated by they class type string = path part, will be joined with “/” hash = options element Class = target class Symbol = method name Proc = These procs will be called with the binding of GrapeEndpoint,

so params and headers Hash::Mash will be allowed to use
they will run BEFORE the method been called, so ideal for auth stuffs

Array = This is for argument pre parsing, like when you use “hash” and the input will be a json

simple use case: [:hello,:json],


looks like this:

mount_method TestClass, :test_method, "funny_path",:GET

you can give hash options just like to any other get,post put delete etc methods, it will work



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
# File 'lib/grape-dsl/mounter.rb', line 35

def mount_method *args

  options      =  Hash[*args.extract_class!(Hash)]
  path_name    = args.extract_class!(String).join('/')
  class_name   = args.extract_class!(Class)[0]
  before_procs = args.extract_class!(Proc)

  tmp_array    = args.extract_class!(Array)
  adapter_opt  = Hash.new

  tmp_array.each do |array_obj|
    if array_obj.count == 2
      adapter_opt[array_obj[0]]= array_obj[1]
    end
  end

  method_name  = nil
  rest_method  = nil

  args.extract_class!(Symbol).each do |element|
    if element.to_s == element.to_s.downcase
      method_name = element
    elsif element.to_s == element.to_s.upcase
      rest_method = element.to_s.downcase
    end
  end

  rest_method ||= "get"
  method_obj   =  class_name.method(method_name).clone

  if path_name == String.new
    path_name= method_name.to_s
  end


  params do

    method_obj.parameters.each do |array_obj|

      case array_obj[0]

        when :req
          requires array_obj[1]
        when :opt
          optional array_obj[1]
        when :rest
          optional array_obj[1],
                   type: Array

        #when :block
        #  optional array_obj[1],
        #           type: String,
        #           desc: "Ruby code to be used"


      end

    end

  end


  __send__(rest_method,path_name,options) do


    method_arguments= (method_obj.parameters.map{|element|
      if !params[element[1]].nil?

        case adapter_opt[element[1]]
          when :json
            params[element[1]]= JSON.parse(params[element[1]])

          when :yaml
            params[element[1]]= YAML.parse(params[element[1]])

        end

        params[element[1]]
      end
    }-[nil])

    before_procs.each do |proc_obj|
      proc_obj.call_with_binding self.binding?
    end

    method_obj.call(*method_arguments)

  end


end

.mount_subclasses(*exception) ⇒ Object

mount all the rest api classes that is subclass of the Grape::API make easy to manage



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/grape-dsl/dsl.rb', line 11

def mount_subclasses(*exception)

  # mark self as exception
  begin
    exception.push(self)
  end

  # mount components
  begin
    Grape::API.subclasses.each do |component|
      unless exception.include?(component)
        mount(component)
      end
    end
  end

  return nil

end