rest_area 2.2.1 → 2.3.0
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.
- checksums.yaml +4 -4
- data/README.md +41 -6
- data/app/controllers/concerns/gets_klass.rb +3 -2
- data/app/controllers/rest_area/message_controller.rb +3 -1
- data/app/controllers/rest_area/rest_controller.rb +7 -0
- data/lib/rest_area/configuration.rb +37 -0
- data/lib/rest_area/resource.rb +63 -0
- data/lib/rest_area/version.rb +1 -1
- data/lib/rest_area.rb +25 -1
- metadata +32 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0274aeb1e1e15d33636be9f1feb5126170e4d46f
|
4
|
+
data.tar.gz: e0717d08f0861bbfb70fb3fe21eb4da6fb2ba267
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddbb98f4f8ada3c7548e3b8d56a7862f50fb320fe2921dd9673049391b32c1664e3e3d40f3b9fa60be7ea7f2c9772462c089447dccbb64761575dc0961503562
|
7
|
+
data.tar.gz: d54160d2367c66ea6b852a49216ec90def9ab1aa6316318adb8e73b323e0fc5b0691f2b848e430e8f0ccf47d7662d1a3751c9523d4f7d515b9c208ddc501bebf
|
data/README.md
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
[](https://codeclimate.com/github/bguest/rest_area) [](https://travis-ci.org/bguest/rest_area) [](https://coveralls.io/r/bguest/rest_area) [](http://badge.fury.io/rb/rest_area) [](https://gemnasium.com/bguest/rest_area)
|
2
|
+
|
1
3
|
# RestArea
|
2
4
|
|
3
|
-
This gem adds a
|
4
|
-
create the
|
5
|
+
This gem adds a bare bones rest api to a rails application. Simply
|
6
|
+
create the initilizer file with the classes that you want to be in the
|
5
7
|
rest api and you will get the get basic rest routes for that class
|
6
8
|
This project rocks and uses MIT-LICENSE.
|
7
9
|
|
@@ -10,7 +12,7 @@ This project rocks and uses MIT-LICENSE.
|
|
10
12
|
This library supports both Rails 3.2.x and Rails 4.1.x.
|
11
13
|
|
12
14
|
### Rails 3.2.x
|
13
|
-
If you are
|
15
|
+
If you are using Rails 3.2.x you should use the `rails-3_2` branch and/or version/tags 1.x.x, ie
|
14
16
|
|
15
17
|
gem 'rest_area', '~>1.0'
|
16
18
|
|
@@ -19,17 +21,50 @@ If you are using Rails 4.1.x you should use the master branch and/or versions/ta
|
|
19
21
|
|
20
22
|
gem 'rest_area', '~>2.0'
|
21
23
|
|
22
|
-
##
|
24
|
+
## Initialization / Setup
|
25
|
+
|
26
|
+
1. Create a file called `rest_area.rb` in your initializers file
|
27
|
+
2. Add configuration like the following in that file.
|
28
|
+
|
29
|
+
RestArea.config do
|
30
|
+
resources :thing_one, :thing_two # Defaults to all actions
|
23
31
|
|
24
|
-
|
25
|
-
|
32
|
+
resources :cereal, :thing do
|
33
|
+
action :index, :show, :create, :update, :delete
|
34
|
+
key :name
|
35
|
+
end
|
36
|
+
|
37
|
+
resource :supermarket do
|
38
|
+
read_only!
|
39
|
+
messages :say_hello, :ring_it_up
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Alternatively you can still just pass in a class whitelist, but this is being deprecated.
|
26
44
|
|
27
45
|
RestArea.class_whitelist = [YourModel, ThatYouWant, ToBeInYour, RestApi]
|
28
46
|
|
47
|
+
|
29
48
|
3. Add something like the following to your `config/routes.rb` file.
|
30
49
|
|
31
50
|
mount RestArea::Engine => "/your_base_route"
|
32
51
|
|
52
|
+
### Config Settings
|
53
|
+
|
54
|
+
Within the RestArea.config block, use `resource` to specify a single resource and `resources` to specify mulitiple resources
|
55
|
+
|
56
|
+
With in the `resource` / `resources` block,
|
57
|
+
+ Use `action` to specify what actions that resource will respond to, the available actions are
|
58
|
+
`:index`, `:show`, `:create`, `:create`, `:update`, and `:delete`, the default is to use all actions
|
59
|
+
|
60
|
+
+ Use `read_only!` to specify that the resource will only respond to the `index` and `show` actions
|
61
|
+
|
62
|
+
+ Use `key` to specify what column rest_area will use to look up a resource. This is the column name
|
63
|
+
that is used for the `:key` in `/:resource/:key` part of the restful path. This defaults to `:id`
|
64
|
+
|
65
|
+
+ Use `message` or `messages` to whitelist methods for a class. Methods much be defined at
|
66
|
+
launch. Methods must respond with an object that responds to `.to_json`
|
67
|
+
|
33
68
|
# Serializers
|
34
69
|
|
35
70
|
But what if I want to customize the JSON that comes back? Simple this
|
@@ -7,7 +7,8 @@ module GetsKlass
|
|
7
7
|
|
8
8
|
def get_klass
|
9
9
|
rescue_uninitialized_constant do
|
10
|
-
|
10
|
+
klass = params[:klass].classify.constantize
|
11
|
+
@klass = RestArea.resources[klass.name.underscore.to_sym]
|
11
12
|
end
|
12
13
|
test_class(@klass)
|
13
14
|
|
@@ -16,7 +17,7 @@ module GetsKlass
|
|
16
17
|
end
|
17
18
|
|
18
19
|
def test_class(klass)
|
19
|
-
if klass.nil? || !RestArea.
|
20
|
+
if klass.nil? || !RestArea.resources.include?(klass.name.underscore.to_sym)
|
20
21
|
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
21
22
|
end
|
22
23
|
end
|
@@ -8,8 +8,10 @@ module RestArea
|
|
8
8
|
render json: @klass.find(params[:id]).send(@message).all, each_serializer: @message_serializer, root:@message
|
9
9
|
elsif @message_class
|
10
10
|
render json: { @message => @klass.find(params[:id]).send(@message).all }.to_json(root:false)
|
11
|
+
elsif @klass.can_send?(@message)
|
12
|
+
render json: @klass.find(params[:id]).send(@message).to_json(root:false)
|
11
13
|
else
|
12
|
-
raise
|
14
|
+
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
@@ -4,6 +4,7 @@ module RestArea
|
|
4
4
|
class RestController < ::RestArea::ApplicationController
|
5
5
|
skip_before_filter :verify_authenticity_token
|
6
6
|
include GetsKlass
|
7
|
+
before_filter :test_action
|
7
8
|
before_filter :set_class_serializer
|
8
9
|
before_filter :add_query_params, :only => [:index]
|
9
10
|
|
@@ -57,6 +58,12 @@ module RestArea
|
|
57
58
|
render json: {errors: object.errors}, :status => :unprocessable_entity
|
58
59
|
end
|
59
60
|
|
61
|
+
def test_action
|
62
|
+
unless @klass.can_do?(params[:action].to_sym)
|
63
|
+
raise ActionController::RoutingError.new("Resource Does Not Exist")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
60
67
|
def klass_params
|
61
68
|
params.require(@root.to_sym).permit!
|
62
69
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
##
|
2
|
+
# Used for configuring rest_area
|
3
|
+
#
|
4
|
+
# RestArea.config do
|
5
|
+
# resources :cereal, :thing do
|
6
|
+
# action :index, :show, :create, :update, :delete
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# resource :supermarket do
|
10
|
+
# read_only!
|
11
|
+
# key :name
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
module RestArea
|
16
|
+
class Configuration
|
17
|
+
|
18
|
+
def initialize()
|
19
|
+
@resources = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def resources(*args, &block)
|
23
|
+
if args.any?
|
24
|
+
args.each do |klass| resource(klass, &block) end
|
25
|
+
else
|
26
|
+
@resources
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def resource(klass, &block)
|
31
|
+
resource = Resource.new(klass)
|
32
|
+
resource.instance_eval(&block) if block_given?
|
33
|
+
@resources[klass] = resource
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module RestArea
|
2
|
+
class Resource < SimpleDelegator
|
3
|
+
|
4
|
+
attr_reader :actions
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
@actions = []
|
8
|
+
@klass = klass.to_s.classify.constantize
|
9
|
+
super @klass
|
10
|
+
end
|
11
|
+
|
12
|
+
def action(*args)
|
13
|
+
@actions ||= []
|
14
|
+
@actions += args
|
15
|
+
@actions.uniq!
|
16
|
+
end
|
17
|
+
|
18
|
+
def messages(*args)
|
19
|
+
@messages ||= []
|
20
|
+
if args.any?
|
21
|
+
args.each do |m| message(m) end
|
22
|
+
else
|
23
|
+
@messages
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def message(msg)
|
28
|
+
@messages ||= []
|
29
|
+
if @klass.method_defined? msg
|
30
|
+
@messages << msg
|
31
|
+
@messages.uniq!
|
32
|
+
else
|
33
|
+
raise NoMethodError.new("#{@klass} will not respond to #{msg}")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def read_only!
|
38
|
+
@actions = [:index, :show]
|
39
|
+
end
|
40
|
+
|
41
|
+
def can_do?(act)
|
42
|
+
self.actions.empty? || self.actions.include?(act)
|
43
|
+
end
|
44
|
+
|
45
|
+
def can_send?(msg)
|
46
|
+
self.messages.include?(msg.to_sym)
|
47
|
+
end
|
48
|
+
|
49
|
+
def key(key = nil)
|
50
|
+
key ? @key = key : (@key || :id)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Wrapped Methods
|
54
|
+
def find(*args)
|
55
|
+
if key == :id
|
56
|
+
super *args
|
57
|
+
else
|
58
|
+
@klass.where(key => args[0]).first!
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
data/lib/rest_area/version.rb
CHANGED
data/lib/rest_area.rb
CHANGED
@@ -1,5 +1,29 @@
|
|
1
1
|
require "rest_area/engine"
|
2
|
+
require 'rest_area/resource'
|
3
|
+
require 'rest_area/configuration'
|
2
4
|
|
3
5
|
module RestArea
|
4
|
-
|
6
|
+
mattr_reader :class_whitelist
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def config
|
11
|
+
@config ||= RestArea::Configuration.new
|
12
|
+
end
|
13
|
+
|
14
|
+
def configure(&block)
|
15
|
+
self.config.instance_eval(&block) if block_given?
|
16
|
+
end
|
17
|
+
|
18
|
+
def resources
|
19
|
+
self.config.resources
|
20
|
+
end
|
21
|
+
|
22
|
+
def class_whitelist=(array)
|
23
|
+
resources.clear
|
24
|
+
array.each do |klass|
|
25
|
+
self.config.resource klass.name.underscore.to_sym
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
5
29
|
end
|
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: 2.
|
4
|
+
version: 2.3.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: 2014-
|
11
|
+
date: 2014-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -94,6 +94,34 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '1.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.9'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.9'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: coveralls
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.7'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.7'
|
97
125
|
description: RestArea adds a restfull controller and api to any Rails Application,
|
98
126
|
simply add the gem and whitelist of available models
|
99
127
|
email:
|
@@ -115,7 +143,9 @@ files:
|
|
115
143
|
- app/views/layouts/rest_area/application.html.erb
|
116
144
|
- config/routes.rb
|
117
145
|
- lib/rest_area.rb
|
146
|
+
- lib/rest_area/configuration.rb
|
118
147
|
- lib/rest_area/engine.rb
|
148
|
+
- lib/rest_area/resource.rb
|
119
149
|
- lib/rest_area/version.rb
|
120
150
|
- lib/tasks/rest_area_tasks.rake
|
121
151
|
homepage: https://github.com/bguest/rest_area
|