rest_area 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6a548c1e51dfdccb2023fd1f17059ecfcbc5129
4
- data.tar.gz: 21f9801586316937ea1ed6e11983907064da50e4
3
+ metadata.gz: 5c8f8f0f8e9b81b6b69d66a82d61d091c454bfcb
4
+ data.tar.gz: 7c4da3d6e130d702badcf918bf13435b466e9d86
5
5
  SHA512:
6
- metadata.gz: 728c7c0bf7cf45e5177044688cecd04775e75c03fa52ba828b37d6d3bb2db7a94546c037e5ff00247af51c604d6d8d91b06aad114eba7d8a94700e755db70f03
7
- data.tar.gz: dcdf88ce4f36d871be43dba65bf284fb4ab19f8b29d1a68c1aa04de3d337604545ad5c232059ea67665ce62adc906c78b239d760cc277f892b66590c299cdada
6
+ metadata.gz: 6bbb1278401a35464d7272ac8be4d087c4213bcdf8b6fdda65bb4bef98055f7989542afda9bb979502bb930eb03ab4a69478e88d6f70fef1c5123309587c92ea
7
+ data.tar.gz: 3827ad996b8a7495d41a84d45b009acede693ac6d80d5ece6bb1d26a3b7ca06a267460178899a7399fb9579c8d6f93fac87f6e07781d4250a7b7e95cb6616292
data/README.md CHANGED
@@ -1,18 +1,74 @@
1
+ [![Code Climate](https://codeclimate.com/github/bguest/rest_area.png)](https://codeclimate.com/github/bguest/rest_area) [![Build Status](https://travis-ci.org/bguest/rest_area.png?branch=master)](https://travis-ci.org/bguest/rest_area) [![Coverage Status](https://coveralls.io/repos/bguest/rest_area/badge.png)](https://coveralls.io/r/bguest/rest_area) [![Gem Version](https://badge.fury.io/rb/rest_area.png)](http://badge.fury.io/rb/rest_area) [![Dependency Status](https://gemnasium.com/bguest/rest_area.png)](https://gemnasium.com/bguest/rest_area)
2
+
1
3
  # RestArea
2
4
 
3
- This gem adds a off bare bones rest api to a rails application. Simply
4
- create the initilizers file with the classes that you want to be in 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
 
8
10
  ## Initilization
9
11
 
10
- 1. Create a file called rest_area.rb in your initilizers file
11
- 2. Add something the following in that file.
12
+ This library supports both Rails 3.2.x and Rails 4.1.x.
13
+
14
+ ### Rails 3.2.x
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
16
+
17
+ gem 'rest_area', '~>1.0'
18
+
19
+ ### Rails 4.1.x
20
+ If you are using Rails 4.1.x you should use the master branch and/or versions/tags 2.x.x, ie
21
+
22
+ gem 'rest_area', '~>2.0'
23
+
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
31
+
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.
12
44
 
13
45
  RestArea.class_whitelist = [YourModel, ThatYouWant, ToBeInYour, RestApi]
14
46
 
15
- 3. Add something like the following to your routes file.
47
+
48
+ 3. Add something like the following to your `config/routes.rb` file.
16
49
 
17
50
  mount RestArea::Engine => "/your_base_route"
18
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
+
68
+ # Serializers
69
+
70
+ But what if I want to customize the JSON that comes back? Simple this
71
+ gem supports [active_model_serializer][1]. Go there, ignore the stuff
72
+ about controllers.
73
+
74
+ [1]:https://github.com/rails-api/active_model_serializers
@@ -7,7 +7,8 @@ module GetsKlass
7
7
 
8
8
  def get_klass
9
9
  rescue_uninitialized_constant do
10
- @klass = params[:klass].classify.constantize
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.class_whitelist.include?(klass)
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 NotImplementedError
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 < 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,16 @@ 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
+
67
+ def klass_params
68
+ params.require(@root.to_sym).permit!
69
+ end
70
+
60
71
  def add_query_params
61
72
  where_params = params.slice(*@klass.column_names)
62
73
  if where_params.any?
@@ -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
@@ -1,3 +1,3 @@
1
1
  module RestArea
2
- VERSION = "1.2.1"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/rest_area.rb CHANGED
@@ -1,6 +1,30 @@
1
1
  require "rest_area/engine"
2
+ require 'rest_area/resource'
3
+ require 'rest_area/configuration'
2
4
 
3
5
 
4
6
  module RestArea
5
- mattr_accessor :class_whitelist
7
+ mattr_reader :class_whitelist
8
+
9
+ class << self
10
+
11
+ def config
12
+ @config ||= RestArea::Configuration.new
13
+ end
14
+
15
+ def configure(&block)
16
+ self.config.instance_eval(&block) if block_given?
17
+ end
18
+
19
+ def resources
20
+ self.config.resources
21
+ end
22
+
23
+ def class_whitelist=(array)
24
+ resources.clear
25
+ array.each do |klass|
26
+ self.config.resource klass.name.underscore.to_sym
27
+ end
28
+ end
29
+ end
6
30
  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: 1.2.1
4
+ version: 1.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-10-09 00:00:00.000000000 Z
11
+ date: 2014-11-18 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: 0.5.1
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