rest_area 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -0
- data/app/controllers/concerns/gets_klass.rb +2 -1
- data/app/controllers/rest_area/message_controller.rb +4 -4
- data/app/controllers/rest_area/rest_controller.rb +9 -4
- data/lib/rest_area/configuration.rb +8 -0
- data/lib/rest_area/resource.rb +21 -9
- data/lib/rest_area/version.rb +1 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e2a0fb366b1d63f0237015c0caf879b90d820d6a
|
4
|
+
data.tar.gz: 6075713f04ee8bed1eb86d998b0ecb6fdc83e9e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bed81e4903dad18c62e3764437ffd55c6a3702661cab8e7796678228d837799e9ae16bc988f5b7cf8936cd9aa67b6d68c69b6ff2e02986fd95dc3fba1dc9497
|
7
|
+
data.tar.gz: 15bcbc3497b30f460ca5a175fc93d361f5a7723ebcdca287b77dc456c19f29c38c36f44d2eb5bb63d8e02a11347bf551a401ec3ae16bbc3b34dabef6b5e0ba0f
|
data/README.md
CHANGED
@@ -32,6 +32,10 @@ If you are using Rails 4.1.x you should use the master branch and/or versions/ta
|
|
32
32
|
resources :cereal, :thing do
|
33
33
|
action :index, :show, :create, :update, :delete
|
34
34
|
key :name
|
35
|
+
headers({
|
36
|
+
'Cache-Control' => 'public, max-age=86400'
|
37
|
+
'Expires' => ->{Date.today + 1}
|
38
|
+
})
|
35
39
|
end
|
36
40
|
|
37
41
|
resource :supermarket do
|
@@ -65,6 +69,8 @@ With in the `resource` / `resources` block,
|
|
65
69
|
+ Use `message` or `messages` to whitelist methods for a class. Methods much be defined at
|
66
70
|
launch. Methods must respond with an object that responds to `.to_json`
|
67
71
|
|
72
|
+
+ Use `headers` to specify the response headers hash, this will be merged with existing headers
|
73
|
+
|
68
74
|
# Serializers
|
69
75
|
|
70
76
|
But what if I want to customize the JSON that comes back? Simple this
|
@@ -8,7 +8,8 @@ module GetsKlass
|
|
8
8
|
def get_klass
|
9
9
|
rescue_uninitialized_constant do
|
10
10
|
klass = params[:klass].classify.constantize
|
11
|
-
@
|
11
|
+
@resource = RestArea.resources[klass.name.underscore.to_sym]
|
12
|
+
@klass = @resource.klass
|
12
13
|
end
|
13
14
|
test_class(@klass)
|
14
15
|
|
@@ -5,11 +5,11 @@ module RestArea
|
|
5
5
|
|
6
6
|
def get
|
7
7
|
if @message_serializer
|
8
|
-
render json: @
|
8
|
+
render json: @resource.find(params[:id]).send(@message).all, each_serializer: @message_serializer, root:@message
|
9
9
|
elsif @message_class
|
10
|
-
render json: { @message => @
|
11
|
-
elsif @
|
12
|
-
render json: @
|
10
|
+
render json: { @message => @resource.find(params[:id]).send(@message).all }.to_json(root:false)
|
11
|
+
elsif @resource.can_send?(@message)
|
12
|
+
render json: @resource.find(params[:id]).send(@message).to_json(root:false)
|
13
13
|
else
|
14
14
|
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
15
15
|
end
|
@@ -6,6 +6,7 @@ module RestArea
|
|
6
6
|
include GetsKlass
|
7
7
|
before_filter :test_action
|
8
8
|
before_filter :set_class_serializer
|
9
|
+
before_filter :add_headers
|
9
10
|
before_filter :add_query_params, :only => [:index]
|
10
11
|
|
11
12
|
# GET
|
@@ -18,7 +19,7 @@ module RestArea
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def show
|
21
|
-
render json: @
|
22
|
+
render json: @resource.find(params[:id]), root: @root
|
22
23
|
end
|
23
24
|
alias_method :edit, :show
|
24
25
|
|
@@ -34,7 +35,7 @@ module RestArea
|
|
34
35
|
|
35
36
|
# PUT
|
36
37
|
def update
|
37
|
-
object = @
|
38
|
+
object = @resource.find(params[:id])
|
38
39
|
if object.update_attributes(params[@root.to_sym])
|
39
40
|
render json: object, root:@root
|
40
41
|
else
|
@@ -44,7 +45,7 @@ module RestArea
|
|
44
45
|
|
45
46
|
# DELETE
|
46
47
|
def delete
|
47
|
-
object = @
|
48
|
+
object = @resource.find(params[:id])
|
48
49
|
if object.destroy
|
49
50
|
render json: object, root:@root
|
50
51
|
else
|
@@ -59,7 +60,7 @@ module RestArea
|
|
59
60
|
end
|
60
61
|
|
61
62
|
def test_action
|
62
|
-
unless @
|
63
|
+
unless @resource.can_do?(params[:action].to_sym)
|
63
64
|
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
64
65
|
end
|
65
66
|
end
|
@@ -68,6 +69,10 @@ module RestArea
|
|
68
69
|
params.require(@root.to_sym).permit!
|
69
70
|
end
|
70
71
|
|
72
|
+
def add_headers
|
73
|
+
response.headers.merge! @resource.headers
|
74
|
+
end
|
75
|
+
|
71
76
|
def add_query_params
|
72
77
|
where_params = params.slice(*@klass.column_names)
|
73
78
|
if where_params.any?
|
@@ -1,14 +1,22 @@
|
|
1
1
|
##
|
2
2
|
# Used for configuring rest_area
|
3
3
|
#
|
4
|
+
# Example:
|
5
|
+
#
|
4
6
|
# RestArea.config do
|
5
7
|
# resources :cereal, :thing do
|
6
8
|
# action :index, :show, :create, :update, :delete
|
9
|
+
# messages :say_hello, :say_goodbye
|
10
|
+
# headers ({
|
11
|
+
# 'Cache-Control' => 'public, max-age=86400'
|
12
|
+
# 'Expires' => ->{Date.today + 1}
|
13
|
+
# })
|
7
14
|
# end
|
8
15
|
#
|
9
16
|
# resource :supermarket do
|
10
17
|
# read_only!
|
11
18
|
# key :name
|
19
|
+
# message :ring_it_up
|
12
20
|
# end
|
13
21
|
# end
|
14
22
|
#
|
data/lib/rest_area/resource.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
1
|
module RestArea
|
2
|
-
class Resource
|
2
|
+
class Resource
|
3
3
|
|
4
|
-
attr_reader :actions
|
4
|
+
attr_reader :actions, :klass
|
5
5
|
|
6
|
-
def initialize(
|
6
|
+
def initialize(klss)
|
7
7
|
@actions = []
|
8
|
-
@klass =
|
9
|
-
super @klass
|
8
|
+
@klass = klss.to_s.classify.constantize
|
10
9
|
end
|
11
10
|
|
12
11
|
def action(*args)
|
@@ -26,14 +25,27 @@ module RestArea
|
|
26
25
|
|
27
26
|
def message(msg)
|
28
27
|
@messages ||= []
|
29
|
-
if
|
28
|
+
if klass.method_defined? msg
|
30
29
|
@messages << msg
|
31
30
|
@messages.uniq!
|
32
31
|
else
|
33
|
-
raise NoMethodError.new("#{
|
32
|
+
raise NoMethodError.new("#{klass} will not respond to #{msg}")
|
34
33
|
end
|
35
34
|
end
|
36
35
|
|
36
|
+
def headers(hdrs = nil)
|
37
|
+
@headers ||= {}
|
38
|
+
if hdrs
|
39
|
+
@headers.merge! hdrs
|
40
|
+
else
|
41
|
+
@headers.inject({}){ |hash, (key, value)|
|
42
|
+
value = value.call if value.kind_of? Proc
|
43
|
+
hash.merge(key => value)
|
44
|
+
}
|
45
|
+
end
|
46
|
+
end
|
47
|
+
alias_method :headers=, :headers
|
48
|
+
|
37
49
|
def read_only!
|
38
50
|
@actions = [:index, :show]
|
39
51
|
end
|
@@ -53,9 +65,9 @@ module RestArea
|
|
53
65
|
# Wrapped Methods
|
54
66
|
def find(*args)
|
55
67
|
if key == :id
|
56
|
-
|
68
|
+
klass.find(*args)
|
57
69
|
else
|
58
|
-
|
70
|
+
klass.where(key => args[0]).first!
|
59
71
|
end
|
60
72
|
end
|
61
73
|
|
data/lib/rest_area/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest_area
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Guest
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -122,6 +122,20 @@ dependencies:
|
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '0.7'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: timecop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.7.1
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.7.1
|
125
139
|
description: RestArea adds a restfull controller and api to any Rails Application,
|
126
140
|
simply add the gem and whitelist of available models
|
127
141
|
email:
|
@@ -167,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
181
|
version: '0'
|
168
182
|
requirements: []
|
169
183
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.
|
184
|
+
rubygems_version: 2.4.3
|
171
185
|
signing_key:
|
172
186
|
specification_version: 4
|
173
187
|
summary: Adds a restfull controller and api to a Rails Application
|