rack-backend-api 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -294,6 +294,7 @@ CHANGE LOG
294
294
  0.2.0 Control what you send on 201 responses
295
295
  0.2.1 Have a title in forms + Only use `_method` if not POST
296
296
  0.2.2 Backend form should accept a block for populating the fields
297
+ 0.2.3 Send a 404 if the entry does not exist
297
298
 
298
299
  COPYRIGHT
299
300
  =========
@@ -1,5 +1,5 @@
1
1
  class BackendAPI
2
- VERSION = [0,2,2]
2
+ VERSION = [0,2,3]
3
3
  WRAP = <<-EOT
4
4
  <!doctype html>
5
5
  <html>
@@ -81,16 +81,21 @@ class BackendAPI
81
81
  def build_model_vars
82
82
  @model_class_name = camel_case(@model_name)
83
83
  if !@model_name.nil? && ::Object.const_defined?(@model_class_name)
84
- @model_class = eval(@model_class_name)
84
+ @model_class = Kernel.const_get(@model_class_name)
85
85
  @model_instance = @model_class.backend_get(@id.to_i) unless @id.nil?
86
86
  @req['model'] ||= {}
87
+ send_404 if @model_instance.nil?&&!@id.nil?
87
88
  else
88
- @res.status=404 # Not Found
89
- @res.headers['X-Cascade']='pass'
90
- @res.write 'Not Found'
89
+ send_404
91
90
  end
92
91
  end
93
92
 
93
+ def send_404
94
+ @res.status=404 # Not Found
95
+ @res.headers['X-Cascade']='pass'
96
+ @res.write 'Not Found'
97
+ end
98
+
94
99
  def camel_case(s)
95
100
  return if s.nil?
96
101
  c = s[0]
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rack-backend-api'
3
- s.version = "0.2.2"
3
+ s.version = "0.2.3"
4
4
  s.platform = Gem::Platform::RUBY
5
5
  s.summary = "A Rack middleware that provides a simple API for your Admin section"
6
6
  s.description = "The purpose of this Rack Middleware is to provide an API that interfaces with database actions in order to build a CMS."
@@ -94,6 +94,10 @@ describe 'API Post' do
94
94
  Haiku.filter(:title => 'Summer is not new !!!').first.id.should==4
95
95
  end
96
96
 
97
+ should "Send a 404 if the entry does not exist" do
98
+ req_lint(BackendAPI.new).post('/haiku/0', :params => {'model' => {'title' => 'Summer is not new !!!'}}).status.should==404
99
+ end
100
+
97
101
  should "Accept a new entry with no attributes as long as it is valid" do
98
102
  res = req_lint(BackendAPI.new).post('/haiku')
99
103
  res.status.should==201
@@ -162,6 +166,10 @@ describe 'API Get' do
162
166
  req_lint(BackendAPI.new).get('/haiku/3').body.should==wrap('Haiku', Haiku[3].backend_form('/haiku/3'))
163
167
  end
164
168
 
169
+ should "Send a 404 if the entry does not exist" do
170
+ req_lint(BackendAPI.new).get('/haiku/0').status.should==404
171
+ end
172
+
165
173
  should "Be able to send a form with selected set of fields" do
166
174
  req_lint(BackendAPI.new).get('/haiku', :params => {'fields' => ['title']}).body.should==wrap('Haiku', Haiku.new.backend_form('/haiku', ['title']))
167
175
  req_lint(BackendAPI.new).get('/haiku/3', :params => {'fields' => ['title']}).body.should==wrap('Haiku', Haiku[3].backend_form('/haiku/3', ['title']))
@@ -190,6 +198,10 @@ describe 'API Put' do
190
198
  haiku.title.should=='Spring'
191
199
  end
192
200
 
201
+ should "Send a 404 if the entry does not exist" do
202
+ req_lint(BackendAPI.new).put('/haiku/0', :params => {'model' => {'body' => "Maybe I have no inspiration\nBut at least\nIt should be on three lines"}}).status.should==404
203
+ end
204
+
193
205
  should "Work with MethodOverride" do
194
206
  req_lint(BackendAPI.new).post('/haiku/3', :params => {'_method' => 'PUT', 'model' => {'title' => 'Spring Wow !!!'}})
195
207
  Haiku[3].title.should=='Spring Wow !!!'
@@ -266,6 +278,10 @@ describe 'API Delete' do
266
278
  Haiku[1].should==nil
267
279
  end
268
280
 
281
+ should "Send a 404 if the entry does not exist" do
282
+ req_lint(BackendAPI.new).delete('/haiku/0').status.should==404
283
+ end
284
+
269
285
  should "Work with MethodOverride" do
270
286
  req_lint(BackendAPI.new(dummy_app)).post('/haiku/2', :params => {'_method' => 'DELETE'})
271
287
  Haiku[2].should==nil
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-backend-api
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mickael Riga
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-08 00:00:00 +01:00
18
+ date: 2011-08-19 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies: []
21
21