Class: Roda::RodaPlugins::RestApi::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/roda/plugins/rest_api.rb

Constant Summary collapse

POST_BODY =
'rack.input'.freeze
FORM_INPUT =
'rack.request.form_input'.freeze
FORM_HASH =
'rack.request.form_hash'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, request, parent, options = {}) ⇒ Resource

Returns a new instance of Resource.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/roda/plugins/rest_api.rb', line 20

def initialize(path, request, parent, options={})
  @request = request
  @path = path.to_s
  bare = options.delete(:bare) || false
  @singleton = options.delete(:singleton) || false
  @primary_key = options.delete(:primary_key) || "id"
  @parent_key = options.delete(:parent_key) || "parent_id"
  @content_type = options.delete(:content_type) || APPLICATION_JSON
  if parent
    @parent = parent
    @path = [':d', @path].join('/') unless bare
  end
end

Instance Attribute Details

#capturesObject

Returns the value of attribute captures.



18
19
20
# File 'lib/roda/plugins/rest_api.rb', line 18

def captures
  @captures
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



17
18
19
# File 'lib/roda/plugins/rest_api.rb', line 17

def content_type
  @content_type
end

#parentObject (readonly)

Returns the value of attribute parent.



17
18
19
# File 'lib/roda/plugins/rest_api.rb', line 17

def parent
  @parent
end

#pathObject (readonly)

Returns the value of attribute path.



17
18
19
# File 'lib/roda/plugins/rest_api.rb', line 17

def path
  @path
end

#requestObject (readonly)

Returns the value of attribute request.



17
18
19
# File 'lib/roda/plugins/rest_api.rb', line 17

def request
  @request
end

#singletonObject (readonly)

Returns the value of attribute singleton.



17
18
19
# File 'lib/roda/plugins/rest_api.rb', line 17

def singleton
  @singleton
end

Instance Method Details

#delete(&block) ⇒ Object



49
50
51
52
53
# File 'lib/roda/plugins/rest_api.rb', line 49

def delete(&block)
  @delete = block if block
  @content_type = nil
  @delete || ->(_){raise NotImplementedError, "delete"}
end

#list(&block) ⇒ Object



34
35
36
37
# File 'lib/roda/plugins/rest_api.rb', line 34

def list(&block)
  @list = block if block
  @list || ->(_){raise NotImplementedError, "list"}
end

#one(&block) ⇒ Object



39
40
41
42
# File 'lib/roda/plugins/rest_api.rb', line 39

def one(&block)
  @one = block if block
  @one || ->(_){raise NotImplementedError, "one"}
end

#perform(method, id = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/roda/plugins/rest_api.rb', line 83

def perform(method, id = nil)
  begin
    args = self.arguments(method)
    args.merge!(@primary_key.to_sym => id) if id
    args.merge!(@parent_key.to_sym => @captures[0]) if @captures
    self.send(method).call(args)
  rescue StandardError => e
    raise if ENV['RACK_ENV'] == 'development'
    @request.response.status = method === :save ? 422 : 404
    @request.response.write e
  end
end

#permit(*permitted) ⇒ Object



66
67
68
# File 'lib/roda/plugins/rest_api.rb', line 66

def permit(*permitted)
  @permitted = permitted
end

#routes(*routes) ⇒ Object



60
61
62
63
64
# File 'lib/roda/plugins/rest_api.rb', line 60

def routes(*routes)
  routes! if @routes
  yield if block_given?
  @routes = routes
end

#routes!Object



70
71
72
73
74
75
76
# File 'lib/roda/plugins/rest_api.rb', line 70

def routes!
  unless @routes
    @routes = SINGLETON_ROUTES.dup
    @routes << :index unless @singleton
  end
  @routes.each { |route| @request.send(route) }
end

#save(&block) ⇒ Object



44
45
46
47
# File 'lib/roda/plugins/rest_api.rb', line 44

def save(&block)
  @save = block if block
  @save || ->(_){raise NotImplementedError, "save"}
end

#serialize(&block) ⇒ Object



55
56
57
58
# File 'lib/roda/plugins/rest_api.rb', line 55

def serialize(&block)
  @serialize = block if block
  @serialize || ->(obj){obj.is_a?(String) ? obj : obj.send(:to_json)}
end