platanus 0.0.8 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/platanus/api_base.rb +117 -0
- data/lib/platanus/stacked.rb +1 -1
- data/lib/platanus/version.rb +1 -1
- metadata +3 -2
@@ -0,0 +1,117 @@
|
|
1
|
+
# api_boilerplate.rb : ActiveRecord Activable mod.
|
2
|
+
#
|
3
|
+
# Copyright July 2012, Ignacio Baixas +mailto:ignacio@platan.us+.
|
4
|
+
|
5
|
+
module Platanus
|
6
|
+
|
7
|
+
# # Boilerplate for platanus json api controllers
|
8
|
+
#
|
9
|
+
# Provides base error handling and rendering methods.
|
10
|
+
# Also provides a couple of opt-in behaviours..
|
11
|
+
#
|
12
|
+
module ApiBoilerplate
|
13
|
+
|
14
|
+
def self.included(base)
|
15
|
+
|
16
|
+
base.respond_to :json # Respond to json by default.
|
17
|
+
|
18
|
+
## EXCEPTION HANDLING
|
19
|
+
|
20
|
+
base.rescue_from 'Exception' do |exc|
|
21
|
+
logger.error exc.message
|
22
|
+
logger.error exc.backtrace.join("\n")
|
23
|
+
api_respond_error(:internal_server_error, { type: exc.class.to_s, msg: exc.message } )
|
24
|
+
end
|
25
|
+
|
26
|
+
base.rescue_from 'ActiveRecord::RecordNotFound' do |exc|
|
27
|
+
api_respond_error(:not_found)
|
28
|
+
end
|
29
|
+
|
30
|
+
base.rescue_from 'ActiveRecord::RecordInvalid' do |exc|
|
31
|
+
api_respond_error(:unprocessable_entity, { why: 'invalid', errors: exc.record.errors } )
|
32
|
+
end
|
33
|
+
|
34
|
+
base.rescue_from 'ActiveModel::MassAssignmentSecurity::Error' do |exc|
|
35
|
+
api_respond_error(:bad_request, { why: 'attribute_protected' } )
|
36
|
+
end
|
37
|
+
|
38
|
+
# Platanus error support (of course)
|
39
|
+
base.rescue_from 'Platanus::StatusError' do |exc|
|
40
|
+
api_respond_error(exc.status, exc)
|
41
|
+
end
|
42
|
+
|
43
|
+
base.extend ClassMethods
|
44
|
+
base.send :include, InstanceMethods
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
|
50
|
+
# # Enables dumb object wrapping
|
51
|
+
#
|
52
|
+
# When enabled, every request parameter is wrapped in a user defined key.
|
53
|
+
# You should disable default parameter wrapping when using this.
|
54
|
+
# (look at wrap_parameter.rb file for more information on how to disable parameter wrapping)
|
55
|
+
#
|
56
|
+
# @param [Symbol] _key Params key to store parameters in (defaults to :object)
|
57
|
+
#
|
58
|
+
def dumb_wrapping(_key=:object)
|
59
|
+
self.before_filter do
|
60
|
+
# Disable magic parameter wrapping (at env) and use 'object' key to hold
|
61
|
+
# incomming request parameters.
|
62
|
+
return if request.headers['HTTP_USER_AGENT'] == 'Rails Testing'
|
63
|
+
params[:object] = request.request_parameters
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
# # Enables cors
|
68
|
+
def enable_cors
|
69
|
+
# TODO: maybe cors support should be implemented as a middleware.
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
module InstanceMethods
|
75
|
+
|
76
|
+
# # Renders an empty response
|
77
|
+
def api_respond_empty(_options={})
|
78
|
+
_options[:json] = {}
|
79
|
+
api_respond_with(_options)
|
80
|
+
render_headers
|
81
|
+
end
|
82
|
+
|
83
|
+
# # Renders a regular response
|
84
|
+
def api_respond_with(*_params)
|
85
|
+
if _params.last.is_a? Hash
|
86
|
+
_options = _params.last
|
87
|
+
_params = _params[0...-1]
|
88
|
+
else
|
89
|
+
_options = {}
|
90
|
+
end
|
91
|
+
|
92
|
+
respond_with(*_params) do |format|
|
93
|
+
format.json { render _options }
|
94
|
+
end
|
95
|
+
render_headers
|
96
|
+
end
|
97
|
+
|
98
|
+
# # Renders an error response
|
99
|
+
#
|
100
|
+
# @param [String, Fixnum] _status Response error code.
|
101
|
+
# @param [object] _error_obj Error object to serialize in response.
|
102
|
+
#
|
103
|
+
def api_respond_error(_status, _error_obj={})
|
104
|
+
respond_with do |format|
|
105
|
+
format.json { render :status => _status, :json => _error_obj }
|
106
|
+
end
|
107
|
+
render_headers
|
108
|
+
end
|
109
|
+
|
110
|
+
def render_headers
|
111
|
+
response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With,Content-Type'
|
112
|
+
response.headers['Access-Control-Allow-Methods'] = 'OPTIONS,GET,HEAD,POST,PUT,DELETE'
|
113
|
+
response.headers['Access-Control-Allow-Origin'] = '*'
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
117
|
+
end
|
data/lib/platanus/stacked.rb
CHANGED
@@ -73,7 +73,7 @@ module Platanus
|
|
73
73
|
# Storing the last stacked value will not prevent race conditions
|
74
74
|
# when simultaneous updates occur.
|
75
75
|
return @_stacked_last unless @_stacked_last.nil?
|
76
|
-
self.send(_name).first
|
76
|
+
@_stacked_last = self.send(_name).first
|
77
77
|
end
|
78
78
|
|
79
79
|
# Generate shorcut properties for cached attributes.
|
data/lib/platanus/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: platanus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.14
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Platan.us utility gem
|
15
15
|
email:
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- lib/platanus.rb
|
28
28
|
- lib/platanus/activable.rb
|
29
|
+
- lib/platanus/api_base.rb
|
29
30
|
- lib/platanus/canned.rb
|
30
31
|
- lib/platanus/cmap.rb
|
31
32
|
- lib/platanus/gcontroller.rb
|