rack-autocrud 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.md +14 -0
  2. data/lib/rack/autocrud.rb +10 -1
  3. metadata +1 -1
data/README.md CHANGED
@@ -22,6 +22,7 @@ Requirements
22
22
 
23
23
  * sinatra
24
24
  * datamapper
25
+ * json
25
26
 
26
27
  Installation
27
28
  ============
@@ -117,4 +118,17 @@ Parameters:
117
118
  If any of these hooks returns anything other than _nil_, it is assumed to be a response object, which
118
119
  is returned immediately, and no further processing is performed.
119
120
 
121
+ Helper Functions
122
+ ================
123
+
124
+ This middleware also adds a helper function to the endpoints, *set_request_body*, to allow
125
+ you to replace the request body from the aforementioned hooks,
126
+ namely *pre_create* and *pre_update*.
127
+
128
+ This function is defined as:
129
+ ```ruby
130
+ def set_request_body(new_body,content_type='text/json') ; end
131
+ ```
132
+
133
+ where *new_body* is expected to be a string.
120
134
 
data/lib/rack/autocrud.rb CHANGED
@@ -56,6 +56,13 @@ module Rack
56
56
 
57
57
  # Patch in the routes
58
58
  endpoint_klass.class_exec(model_klass,endpoint,env) { |model,endpoint,env|
59
+ def set_request_body(new_body,content_type='text/json')
60
+ env['rack.input'] = StringIO.new(new_body)
61
+ env['CONTENT_LENGTH'] = new_body.length
62
+ env['CONTENT_TYPE'] = content_type
63
+ return nil
64
+ end
65
+
59
66
  get '/' do
60
67
  halt [403, '{ "error": "Access Denied" }']
61
68
  end
@@ -63,10 +70,11 @@ module Rack
63
70
  post '/' do
64
71
  # Call the pre-create hook
65
72
  if self.respond_to?(:pre_create)
66
- ret = pre_create(env,request,obj)
73
+ ret = pre_create(env,request)
67
74
  return ret unless ret.nil?
68
75
  end
69
76
 
77
+ request.body.rewind if request.body.respond_to?(:rewind)
70
78
  obj = model.create(JSON.parse(request.body.read))
71
79
  halt [402, '{ "error": "Failed to save ' + endpoint + '" }'] unless obj && obj.saved?
72
80
 
@@ -104,6 +112,7 @@ module Rack
104
112
  return ret unless ret.nil?
105
113
  end
106
114
 
115
+ request.body.rewind if request.body.respond_to?(:rewind)
107
116
  saved = model.update(JSON.parse(request.body.read))
108
117
  halt [403, 'Access Denied'] unless saved
109
118
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-autocrud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: