Module: Roda::RodaPlugins::RestApi::RequestMethods
- Defined in:
- lib/roda/plugins/rest_api.rb
Instance Method Summary collapse
- #api(options = {}, &block) ⇒ Object
- #create(options = {}, &block) ⇒ Object
- #destroy(options = {}, &block) ⇒ Object
- #edit(options = {}, &block) ⇒ Object
- #index(options = {}, &block) ⇒ Object
- #new(options = {}, &block) ⇒ Object
- #resource(path, options = {}) ⇒ Object
- #show(options = {}, &block) ⇒ Object
- #update(options = {}, &block) ⇒ Object
- #version(version, &block) ⇒ Object
Instance Method Details
#api(options = {}, &block) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/roda/plugins/rest_api.rb', line 165 def api(={}, &block) path = .delete(:path) || 'api' subdomain = .delete(:subdomain) .merge!(host: /\A#{Regexp.escape(subdomain)}\./) if subdomain path = true if path.nil? or path.empty? on(path, , &block) end |
#create(options = {}, &block) ⇒ Object
198 199 200 201 202 203 204 |
# File 'lib/roda/plugins/rest_api.rb', line 198 def create(={}, &block) block ||= ->{@resource.perform(:save)} post(["", true], ) do response.status = 201 block.call(*captures) if block end end |
#destroy(options = {}, &block) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/roda/plugins/rest_api.rb', line 212 def destroy(={}, &block) block ||= default_block(:delete) delete(_path, ) do response.status = 204 block.call(*captures) if block end end |
#edit(options = {}, &block) ⇒ Object
220 221 222 223 |
# File 'lib/roda/plugins/rest_api.rb', line 220 def edit(={}, &block) block ||= default_block(:one) get(_path("edit"), , &block) end |
#index(options = {}, &block) ⇒ Object
188 189 190 191 |
# File 'lib/roda/plugins/rest_api.rb', line 188 def index(={}, &block) block ||= ->{ @resource.perform(:list) } get(['', true], , &block) end |
#new(options = {}, &block) ⇒ Object
225 226 227 228 |
# File 'lib/roda/plugins/rest_api.rb', line 225 def new(={}, &block) block ||= ->{@resource.perform(:one, "new")} get("new", , &block) end |
#resource(path, options = {}) ⇒ Object
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/roda/plugins/rest_api.rb', line 177 def resource(path, ={}) @resource = Resource.new(path, self, @resource, ) on(@resource.path, ) do @resource.captures = captures.dup unless captures.empty? yield @resource @resource.routes! response.status = 404 end @resource = @resource.parent end |
#show(options = {}, &block) ⇒ Object
193 194 195 196 |
# File 'lib/roda/plugins/rest_api.rb', line 193 def show(={}, &block) block ||= default_block(:one) get(_path, , &block) end |
#update(options = {}, &block) ⇒ Object
206 207 208 209 210 |
# File 'lib/roda/plugins/rest_api.rb', line 206 def update(={}, &block) block ||= default_block(:save) .merge!(method: [:put, :patch]) is(_path, , &block) end |
#version(version, &block) ⇒ Object
173 174 175 |
# File 'lib/roda/plugins/rest_api.rb', line 173 def version(version, &block) on("v#{version}", &block) end |