rest_area 1.4.3 → 2.0.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: 05a4ad520c52faac53b2cb80c941cf26a8f41c17
4
- data.tar.gz: 2ea2dbcecefc1b523c97a6b2784581dc9ff75b00
3
+ metadata.gz: 8e8eafe9476bbb11ea8d35670e7c04fa4d00fe1a
4
+ data.tar.gz: 189aca44997b83a548bacff763d2b766fe953852
5
5
  SHA512:
6
- metadata.gz: 15fd36e0dca85de276bf32575d309435f0d6546ebec91aee02d88d02a81b3305600d550120615a91028303676965d66b65b88743d7ee3e32febaafb2e617f208
7
- data.tar.gz: 69bdd5b56b66961747a8c4e0ef2844adaea89faec2c8ba9587a09959128b90e221d950e9263294e5c085eae1f534cccef673040a51227a450decd6bc43637f64
6
+ metadata.gz: a386cbaabf1f1c0afd3649372b5e1ecea5ee83774cdb7d43deb84de0e6375da643140a6cdca2eed193d7e844c291e433ea2382695847ed5c94130d9e560dc7b9
7
+ data.tar.gz: 6665be2ee5be3c856ec171e480f05435a5f67bc5c46e0e162e0df4281c86aaa651a1c89d84e1202c37af3d0cbf0591cc6456c643bd5b01c768e8d973b6b3419e
data/README.md CHANGED
@@ -1,18 +1,16 @@
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
-
3
1
  # RestArea
4
2
 
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
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
7
5
  rest api and you will get the get basic rest routes for that class
8
6
  This project rocks and uses MIT-LICENSE.
9
7
 
10
- ## Initilization
8
+ ## Versions
11
9
 
12
10
  This library supports both Rails 3.2.x and Rails 4.1.x.
13
11
 
14
12
  ### 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
13
+ If you are useing Rails 3.2.x you should use the `rails-3_2` branch and/or version/tags 1.x.x, ie
16
14
 
17
15
  gem 'rest_area', '~>1.0'
18
16
 
@@ -21,60 +19,14 @@ If you are using Rails 4.1.x you should use the master branch and/or versions/ta
21
19
 
22
20
  gem 'rest_area', '~>2.0'
23
21
 
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.configure 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
- headers({
36
- 'Cache-Control' => 'public, max-age=86400'
37
- 'Expires' => ->{Date.today + 1}
38
- })
39
- end
22
+ ## Initilization / Setup
40
23
 
41
- resource :supermarket do
42
- read_only!
43
- messages :say_hello, :ring_it_up
44
- end
45
- end
46
-
47
- Alternatively you can still just pass in a class whitelist, but this is being deprecated.
24
+ 1. Create a file called `rest_area.rb` in your initilizers file
25
+ 2. Add something the following in that file.
48
26
 
49
27
  RestArea.class_whitelist = [YourModel, ThatYouWant, ToBeInYour, RestApi]
50
28
 
51
-
52
29
  3. Add something like the following to your `config/routes.rb` file.
53
30
 
54
31
  mount RestArea::Engine => "/your_base_route"
55
32
 
56
- ### Config Settings
57
-
58
- Within the RestArea.config block, use `resource` to specify a single resource and `resources` to specify mulitiple resources
59
-
60
- With in the `resource` / `resources` block,
61
- + Use `action` to specify what actions that resource will respond to, the available actions are
62
- `:index`, `:show`, `:create`, `:create`, `:update`, and `:delete`, the default is to use all actions
63
-
64
- + Use `read_only!` to specify that the resource will only respond to the `index` and `show` actions
65
-
66
- + Use `key` to specify what column rest_area will use to look up a resource. This is the column name
67
- that is used for the `:key` in `/:resource/:key` part of the restful path. This defaults to `:id`
68
-
69
- + Use `message` or `messages` to whitelist methods for a class. Methods much be defined at
70
- launch. Methods must respond with an object that responds to `.to_json`
71
-
72
- + Use `headers` to specify the response headers hash, this will be merged with existing headers
73
-
74
- # Serializers
75
-
76
- But what if I want to customize the JSON that comes back? Simple this
77
- gem supports [active_model_serializer][1]. Go there, ignore the stuff
78
- about controllers.
79
-
80
- [1]:https://github.com/rails-api/active_model_serializers
@@ -1,15 +1,4 @@
1
1
  module RestArea
2
- class ApplicationController < ::ApplicationController
3
-
4
- private
5
-
6
- def rescue_uninitialized_constant
7
- yield
8
- rescue NameError => e
9
- unless e.message =~ /uninitialized constant/
10
- throw e
11
- end
12
- end
2
+ class ApplicationController < ActionController::Base
13
3
  end
14
-
15
4
  end
@@ -1,13 +1,7 @@
1
- require 'saneitized'
2
-
3
1
  module RestArea
4
2
  class RestController < ApplicationController
5
3
  skip_before_filter :verify_authenticity_token
6
- include GetsKlass
7
- before_filter :test_action
8
- before_filter :set_class_serializer
9
- before_filter :add_headers
10
- before_filter :add_query_params, :only => [:index]
4
+ before_filter :get_class, :set_class_serializer
11
5
 
12
6
  # GET
13
7
  def index
@@ -19,13 +13,13 @@ module RestArea
19
13
  end
20
14
 
21
15
  def show
22
- render json: @resource.find(params[:id]), root: @root
16
+ render json: @klass.find(params[:id]), root: @root
23
17
  end
24
18
  alias_method :edit, :show
25
19
 
26
20
  # POST
27
21
  def create
28
- object = @klass.new(params[@root.to_sym])
22
+ object = @klass.new(klass_params)
29
23
  if object.save
30
24
  render json: object, root:@root
31
25
  else
@@ -35,8 +29,8 @@ module RestArea
35
29
 
36
30
  # PUT
37
31
  def update
38
- object = @resource.find(params[:id])
39
- if object.update_attributes(params[@root.to_sym])
32
+ object = @klass.find(params[:id])
33
+ if object.update_attributes(klass_params)
40
34
  render json: object, root:@root
41
35
  else
42
36
  render_errors(object)
@@ -45,7 +39,7 @@ module RestArea
45
39
 
46
40
  # DELETE
47
41
  def delete
48
- object = @resource.find(params[:id])
42
+ object = @klass.find(params[:id])
49
43
  if object.destroy
50
44
  render json: object, root:@root
51
45
  else
@@ -59,34 +53,33 @@ module RestArea
59
53
  render json: {errors: object.errors}, :status => :unprocessable_entity
60
54
  end
61
55
 
62
- def test_action
63
- unless @resource.can_do?(params[:action].to_sym)
64
- raise ActionController::RoutingError.new("Resource Does Not Exist")
65
- end
56
+ def query_params
57
+ params.slice('limit','order').symbolize_keys
66
58
  end
67
59
 
68
60
  def klass_params
69
- params.require(@root.to_sym).permit!
70
- end
71
-
72
- def add_headers
73
- response.headers.merge! @resource.headers
74
- end
75
-
76
- def add_query_params
77
- where_params = params.slice(*@klass.column_names)
78
- if where_params.any?
79
- @klass = @klass.where(Saneitized.convert(where_params).to_h)
80
- end
61
+ params.require(@root.to_sym).permit(@klass.column_names.map(&:to_sym))
81
62
  end
82
63
 
83
64
  def set_class_serializer
84
65
  @serializer = ( '::' + @klass.to_s + "Serializer" ).constantize
85
66
  rescue NameError => e
86
- unless e.message =~ /uninitialized constant/
67
+ if e.message =~ /uninitialized constant/
68
+ @serializer = false
69
+ else
87
70
  throw e
88
71
  end
89
72
  end
90
73
 
74
+ def get_class
75
+ @klass = params[:klass].singularize.camelize.constantize
76
+ unless RestArea.class_whitelist.include? @klass
77
+ raise ActionController::RoutingError.new("Resource Does Not Exist")
78
+ end
79
+
80
+ @roots = ActionController::Base.helpers.sanitize(params[:klass])
81
+ @root = @roots.singularize
82
+ end
83
+
91
84
  end
92
85
  end
data/config/routes.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  RestArea::Engine.routes.draw do
2
2
  get '/:klass', :to => "rest#index"
3
3
  get '/:klass/:id', :to => "rest#show"
4
- get '/:klass/:id/:message', :to => 'message#get'
5
4
  post '/:klass', :to => "rest#create"
6
5
  put '/:klass/:id', :to => "rest#update"
7
6
  delete '/:klass/:id', :to => "rest#delete"
@@ -1,3 +1,3 @@
1
1
  module RestArea
2
- VERSION = "1.4.3"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/rest_area.rb CHANGED
@@ -1,30 +1,5 @@
1
1
  require "rest_area/engine"
2
- require 'rest_area/resource'
3
- require 'rest_area/configuration'
4
-
5
2
 
6
3
  module RestArea
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
4
+ mattr_accessor :class_whitelist
30
5
  end
metadata CHANGED
@@ -1,141 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_area
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.3
4
+ version: 2.0.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: 2015-01-31 00:00:00.000000000 Z
11
+ date: 2014-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.17
19
+ version: '4.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 3.2.17
27
- - !ruby/object:Gem::Dependency
28
- name: saneitized
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ~>
32
- - !ruby/object:Gem::Version
33
- version: '1.2'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ~>
39
- - !ruby/object:Gem::Version
40
- version: '1.2'
26
+ version: '4.1'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rspec-rails
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - ~>
31
+ - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: '3.0'
33
+ version: 3.0.0.beta
48
34
  type: :development
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - ~>
38
+ - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: '3.0'
40
+ version: 3.0.0.beta
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: mocha
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
- - - ~>
45
+ - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: '1.0'
47
+ version: 1.0.0
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
- - - ~>
52
+ - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: '1.0'
69
- - !ruby/object:Gem::Dependency
70
- name: sqlite3
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ~>
74
- - !ruby/object:Gem::Version
75
- version: '1.3'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ~>
81
- - !ruby/object:Gem::Version
82
- version: '1.3'
54
+ version: 1.0.0
83
55
  - !ruby/object:Gem::Dependency
84
56
  name: combustion
85
57
  requirement: !ruby/object:Gem::Requirement
86
58
  requirements:
87
- - - ~>
59
+ - - "~>"
88
60
  - !ruby/object:Gem::Version
89
61
  version: 0.5.1
90
62
  type: :development
91
63
  prerelease: false
92
64
  version_requirements: !ruby/object:Gem::Requirement
93
65
  requirements:
94
- - - ~>
66
+ - - "~>"
95
67
  - !ruby/object:Gem::Version
96
68
  version: 0.5.1
97
69
  - !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'
125
- - !ruby/object:Gem::Dependency
126
- name: timecop
70
+ name: sqlite3
127
71
  requirement: !ruby/object:Gem::Requirement
128
72
  requirements:
129
- - - ~>
73
+ - - "~>"
130
74
  - !ruby/object:Gem::Version
131
- version: 0.7.1
75
+ version: '1.3'
132
76
  type: :development
133
77
  prerelease: false
134
78
  version_requirements: !ruby/object:Gem::Requirement
135
79
  requirements:
136
- - - ~>
80
+ - - "~>"
137
81
  - !ruby/object:Gem::Version
138
- version: 0.7.1
82
+ version: '1.3'
139
83
  description: RestArea adds a restfull controller and api to any Rails Application,
140
84
  simply add the gem and whitelist of available models
141
85
  email:
@@ -149,17 +93,13 @@ files:
149
93
  - Rakefile
150
94
  - app/assets/javascripts/rest_area/application.js
151
95
  - app/assets/stylesheets/rest_area/application.css
152
- - app/controllers/concerns/gets_klass.rb
153
96
  - app/controllers/rest_area/application_controller.rb
154
- - app/controllers/rest_area/message_controller.rb
155
97
  - app/controllers/rest_area/rest_controller.rb
156
98
  - app/helpers/rest_area/application_helper.rb
157
99
  - app/views/layouts/rest_area/application.html.erb
158
100
  - config/routes.rb
159
101
  - lib/rest_area.rb
160
- - lib/rest_area/configuration.rb
161
102
  - lib/rest_area/engine.rb
162
- - lib/rest_area/resource.rb
163
103
  - lib/rest_area/version.rb
164
104
  - lib/tasks/rest_area_tasks.rake
165
105
  homepage: https://github.com/bguest/rest_area
@@ -171,17 +111,17 @@ require_paths:
171
111
  - lib
172
112
  required_ruby_version: !ruby/object:Gem::Requirement
173
113
  requirements:
174
- - - '>='
114
+ - - ">="
175
115
  - !ruby/object:Gem::Version
176
116
  version: '0'
177
117
  required_rubygems_version: !ruby/object:Gem::Requirement
178
118
  requirements:
179
- - - '>='
119
+ - - ">="
180
120
  - !ruby/object:Gem::Version
181
121
  version: '0'
182
122
  requirements: []
183
123
  rubyforge_project:
184
- rubygems_version: 2.4.4
124
+ rubygems_version: 2.2.2
185
125
  signing_key:
186
126
  specification_version: 4
187
127
  summary: Adds a restfull controller and api to a Rails Application
@@ -1,26 +0,0 @@
1
- module GetsKlass
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- before_filter :get_klass
6
- end
7
-
8
- def get_klass
9
- rescue_uninitialized_constant do
10
- klass = params[:klass].classify.constantize
11
- @resource = RestArea.resources[klass.name.underscore.to_sym]
12
- @klass = @resource.klass
13
- end
14
- test_class(@klass)
15
-
16
- @roots = ActionController::Base.helpers.sanitize(params[:klass]).pluralize
17
- @root = @roots.singularize
18
- end
19
-
20
- def test_class(klass)
21
- if klass.nil? || !RestArea.resources.include?(klass.name.underscore.to_sym)
22
- raise ActionController::RoutingError.new("Resource Does Not Exist")
23
- end
24
- end
25
-
26
- end
@@ -1,38 +0,0 @@
1
- module RestArea
2
- class MessageController < ApplicationController
3
- include GetsKlass
4
- before_filter :get_message, :set_message_class, :set_message_serializer
5
-
6
- def get
7
- if @message_serializer
8
- render json: @resource.find(params[:id]).send(@message).all, each_serializer: @message_serializer, root:@message
9
- elsif @message_class
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
- else
14
- raise ActionController::RoutingError.new("Resource Does Not Exist")
15
- end
16
- end
17
-
18
- private
19
-
20
- def get_message
21
- @message = params[:message]
22
- end
23
-
24
- def set_message_class
25
- rescue_uninitialized_constant do
26
- @message_class = @message.classify.constantize
27
- end
28
- test_class(@message_class) if @message_class
29
- end
30
-
31
- def set_message_serializer
32
- return unless @message_class
33
- rescue_uninitialized_constant do
34
- @message_serializer = ( '::' + @message.classify + "Serializer" ).constantize
35
- end
36
- end
37
- end
38
- end
@@ -1,45 +0,0 @@
1
- ##
2
- # Used for configuring rest_area
3
- #
4
- # Example:
5
- #
6
- # RestArea.configure do
7
- # resources :cereal, :thing do
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
- # })
14
- # end
15
- #
16
- # resource :supermarket do
17
- # read_only!
18
- # key :name
19
- # message :ring_it_up
20
- # end
21
- # end
22
- #
23
- module RestArea
24
- class Configuration
25
-
26
- def initialize()
27
- @resources = {}
28
- end
29
-
30
- def resources(*args, &block)
31
- if args.any?
32
- args.each do |klass| resource(klass, &block) end
33
- else
34
- @resources
35
- end
36
- end
37
-
38
- def resource(klass, &block)
39
- resource = @resources[klass] || Resource.new(klass)
40
- resource.instance_eval(&block) if block_given?
41
- @resources[klass] = resource
42
- end
43
- end
44
-
45
- end
@@ -1,75 +0,0 @@
1
- module RestArea
2
- class Resource
3
-
4
- attr_reader :actions, :klass
5
-
6
- def initialize(klss)
7
- @actions = []
8
- @klass = klss.to_s.classify.constantize
9
- end
10
-
11
- def action(*args)
12
- @actions ||= []
13
- @actions += args
14
- @actions.uniq!
15
- end
16
-
17
- def messages(*args)
18
- @messages ||= []
19
- if args.any?
20
- args.each do |m| message(m) end
21
- else
22
- @messages
23
- end
24
- end
25
-
26
- def message(msg)
27
- @messages ||= []
28
- if klass.method_defined? msg
29
- @messages << msg
30
- @messages.uniq!
31
- else
32
- raise NoMethodError.new("#{klass} will not respond to #{msg}")
33
- end
34
- end
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
-
49
- def read_only!
50
- @actions = [:index, :show]
51
- end
52
-
53
- def can_do?(act)
54
- self.actions.empty? || self.actions.include?(act)
55
- end
56
-
57
- def can_send?(msg)
58
- self.messages.include?(msg.to_sym)
59
- end
60
-
61
- def key(key = nil)
62
- key ? @key = key : (@key || :id)
63
- end
64
-
65
- # Wrapped Methods
66
- def find(*args)
67
- if key == :id
68
- klass.find(*args)
69
- else
70
- klass.where(key => args[0]).first!
71
- end
72
- end
73
-
74
- end
75
- end