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
166 167 168 169 170 171 172 |
# File 'lib/roda/plugins/rest_api.rb', line 166 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
199 200 201 202 203 204 205 |
# File 'lib/roda/plugins/rest_api.rb', line 199 def create(={}, &block) block ||= ->{@resource.perform(:save)} post(["", true], ) do response.status = 201 block.call(*captures) if block end end |
#destroy(options = {}, &block) ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/roda/plugins/rest_api.rb', line 213 def destroy(={}, &block) block ||= default_block(:delete) delete(_path, ) do response.status = 204 block.call(*captures) if block end end |
#edit(options = {}, &block) ⇒ Object
221 222 223 224 |
# File 'lib/roda/plugins/rest_api.rb', line 221 def edit(={}, &block) block ||= default_block(:one) get(_path("edit"), , &block) end |
#index(options = {}, &block) ⇒ Object
189 190 191 192 |
# File 'lib/roda/plugins/rest_api.rb', line 189 def index(={}, &block) block ||= ->{ @resource.perform(:list) } get(['', true], , &block) end |
#new(options = {}, &block) ⇒ Object
226 227 228 229 |
# File 'lib/roda/plugins/rest_api.rb', line 226 def new(={}, &block) block ||= ->{@resource.perform(:one, "new")} get("new", , &block) end |
#resource(path, options = {}) ⇒ Object
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/roda/plugins/rest_api.rb', line 178 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
194 195 196 197 |
# File 'lib/roda/plugins/rest_api.rb', line 194 def show(={}, &block) block ||= default_block(:one) get(_path, , &block) end |
#update(options = {}, &block) ⇒ Object
207 208 209 210 211 |
# File 'lib/roda/plugins/rest_api.rb', line 207 def update(={}, &block) block ||= default_block(:save) .merge!(method: [:put, :patch]) is(_path, , &block) end |
#version(version, &block) ⇒ Object
174 175 176 |
# File 'lib/roda/plugins/rest_api.rb', line 174 def version(version, &block) on("v#{version}", &block) end |