Module: Sinja::ResourceRoutes

Defined in:
lib/sinja/resource_routes.rb

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object


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
# File 'lib/sinja/resource_routes.rb', line 4

def self.registered(app)
  app.def_action_helper(app, :show, :roles)
  app.def_action_helper(app, :show_many)
  app.def_action_helper(app, :index, %i[roles filter_by sort_by])
  app.def_action_helper(app, :create, :roles)
  app.def_action_helper(app, :update, :roles)
  app.def_action_helper(app, :destroy, :roles)

  app.options '', :qcaptures=>{ :filter=>:id } do
    allow :get=>:show
  end

  app.get '', :qcaptures=>{ :filter=>:id }, :qparams=>%i[include fields], :actions=>:show do
    ids = @qcaptures.first # TODO: Get this as a block parameter?
    ids = ids.split(',') if ids.instance_of?(String)
    ids = Array(ids).tap(&:uniq!)

    collection, opts =
      if respond_to?(:show_many)
        show_many(ids)
      else
        finder =
          if respond_to?(:find)
            method(:find)
          else
            proc { |id| show(id).first }
          end

        [ids.map!(&finder).tap(&:compact!), {}]
      end

    raise NotFoundError, "Resource(s) not found" \
      unless ids.length == collection.length

    serialize_models(collection, opts)
  end

  app.options '' do
    allow :get=>:index, :post=>:create
  end

  app.get '', :qparams=>%i[include fields filter sort page], :actions=>:index do
    fsp_opts = filter_sort_page?(:index)
    collection, opts = index
    collection, pagination = filter_sort_page(collection, fsp_opts.to_h)
    serialize_models(collection, opts, pagination)
  end

  app.post '', :qparams=>%i[include fields], :actions=>:create do
    sanity_check!

    opts = {}
    transaction do
      id, self.resource, opts =
        begin
          create(*[attributes].tap { |a| a << data[:id] if data.key?(:id) })
        rescue ArgumentError
          kind = data.key?(:id) ? 'supported' : 'provided'

          raise ForbiddenError, "Client-generated ID not #{kind}"
        end

      dispatch_relationship_requests!(id, :from=>:create, :methods=>{ :has_many=>:post })
      validate! if respond_to?(:validate!)
    end

    if resource
      content = serialize_model(resource, opts)
      if content.respond_to?(:dig) && self_link = content.dig(*%w[data links self])
        headers 'Location'=>self_link
      end
      [201, content]
    elsif data.key?(:id)
      204
    else
      raise ActionHelperError, "Unexpected return value(s) from `create' action helper"
    end
  end

  pkre = app._resource_config[:route_opts][:pkre]

  app.options %r{/#{pkre}} do
    allow :get=>:show, :patch=>:update, :delete=>:destroy
  end

  app.get %r{/(#{pkre})}, :qparams=>%i[include fields], :actions=>:show do |id|
    tmp, opts = show(*[].tap { |a| a << id unless respond_to?(:find) })
    raise NotFoundError, "Resource '#{id}' not found" unless tmp
    serialize_model(tmp, opts)
  end

  app.patch %r{/(#{pkre})}, :qparams=>%i[include fields], :actions=>:update do |id|
    sanity_check!(id)
    tmp, opts = transaction do
      update(attributes).tap do
        dispatch_relationship_requests!(id, :from=>:update)
        validate! if respond_to?(:validate!)
      end
    end
    serialize_model?(tmp, opts)
  end

  app.delete %r{/#{pkre}}, :actions=>:destroy do
    _, opts = destroy
    serialize_model?(nil, opts)
  end
end