Class: Apiwork::API::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/apiwork/api/router.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.drawObject



7
8
9
# File 'lib/apiwork/api/router.rb', line 7

def draw
  new.draw
end

Instance Method Details

#drawObject



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
# File 'lib/apiwork/api/router.rb', line 12

def draw
  api_classes = Registry.values
  router = self

  set = ActionDispatch::Routing::RouteSet.new

  set.draw do
    api_classes.each do |api_class|
      next if api_class.base_path.blank? || api_class.root_resource.blank?

      if api_class.export_configs.any?
        scope path: api_class.transform_path(api_class.base_path) do
          api_class.export_configs.each do |export_name, export_config|
            next unless case export_config.endpoint.mode
                        when :always then true
                        when :never then false
                        when :auto then Rails.env.development?
                        end

            get export_config.endpoint.path || "/.#{export_name}",
                defaults: { export_name:, api_base_path: api_class.base_path },
                to: 'apiwork/exports#show'
          end
        end
      end

      if api_class.explorer_config && defined?(Apiwork::Explorer::Engine)
        mount_explorer = case api_class.explorer_config.mode
                         when :always then true
                         when :never then false
                         when :auto then Rails.env.development?
                         end

        if mount_explorer
          scope path: api_class.transform_path(api_class.base_path) do
            mount Apiwork::Explorer::Engine,
                  at: api_class.explorer_config.path,
                  defaults: { api_base_path: api_class.base_path }
          end
        end
      end

      scope module: api_class.namespaces.map(&:to_s).join('/').underscore,
            path: api_class.transform_path(api_class.base_path) do
        router.draw_resources(self, api_class.root_resource.resources, api_class)
      end

      scope path: api_class.transform_path(api_class.base_path) do
        match '*unmatched', to: 'apiwork/errors#not_found', via: :all
      end
    end
  end

  set
end

#draw_resources(context, resources_hash, api_class) ⇒ Object



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
# File 'lib/apiwork/api/router.rb', line 68

def draw_resources(context, resources_hash, api_class)
  resources_hash.each_value do |resource|
    resource_method = resource.singular ? :resource : :resources
    options = {
      constraints: resource.constraints,
      controller: resource.controller,
      defaults: resource.defaults,
      except: resource.except,
      only: resource.only,
      param: resource.param,
    }.compact

    path_option = api_class.transform_path(resource.path || resource.name)
    options[:path] = path_option unless path_option == resource.name.to_s

    router = self

    context.instance_eval do
      send(resource_method, resource.name, **options) do
        if resource.member_actions.any?
          member do
            resource.member_actions.each_value do |action|
              action_path = api_class.transform_path(action.name)
              if action_path == action.name.to_s
                send(action.method, action.name)
              else
                send(action.method, action.name, path: action_path)
              end
            end
          end
        end

        if resource.collection_actions.any?
          collection do
            resource.collection_actions.each_value do |action|
              action_path = api_class.transform_path(action.name)
              if action_path == action.name.to_s
                send(action.method, action.name)
              else
                send(action.method, action.name, path: action_path)
              end
            end
          end
        end

        router.draw_resources(self, resource.resources, api_class)
      end
    end
  end
end