api_me 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +13 -0
- data/LICENCE +20 -0
- data/README.md +18 -0
- data/Rakefile +11 -0
- data/api_me.gemspec +29 -0
- data/config.ru +7 -0
- data/lib/api_me.rb +133 -0
- data/lib/api_me/version.rb +3 -0
- data/lib/generators/api_me/controller/USAGE +8 -0
- data/lib/generators/api_me/controller/controller_generator.rb +72 -0
- data/lib/generators/api_me/controller/templates/controller.rb +7 -0
- data/lib/generators/api_me/policy/USAGE +8 -0
- data/lib/generators/api_me/policy/policy_generator.rb +58 -0
- data/lib/generators/api_me/policy/templates/policy.rb +7 -0
- data/lib/generators/api_me/resource/USAGE +11 -0
- data/lib/generators/api_me/resource/resource_generator.rb +13 -0
- data/spec/acceptance/api/v1/fails_spec.rb +12 -0
- data/spec/acceptance/api/v1/users_spec.rb +65 -0
- data/spec/internal/app/controllers/api/v1/fails_controller.rb +11 -0
- data/spec/internal/app/controllers/api/v1/users_controller.rb +3 -0
- data/spec/internal/app/controllers/application_controller.rb +12 -0
- data/spec/internal/app/models/user.rb +2 -0
- data/spec/internal/app/policies/application_policy.rb +65 -0
- data/spec/internal/app/policies/user_policy.rb +8 -0
- data/spec/internal/app/serializers/user_serializer.rb +4 -0
- data/spec/internal/config/database.yml +3 -0
- data/spec/internal/config/routes.rb +8 -0
- data/spec/internal/db/schema.rb +6 -0
- data/spec/internal/log/.gitignore +1 -0
- data/spec/internal/public/favicon.ico +0 -0
- data/spec/spec_helper.rb +25 -0
- metadata +71 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1d24dedc46539788df2d2b959d96ecf93a5e5a4
|
4
|
+
data.tar.gz: c6b2aaddefc12f77cd35ee91935b833f6f743b0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2de62f19830a20723cfd7b213867f9788b16dedecc2e6489d46c72557c98bea25f7abf82a57d59971ff698bdec49f46386d0db0b64a6fc75a3c81555410b567e
|
7
|
+
data.tar.gz: 2c29e465df95037dc21e724c343811deecc351602bcc4424029aac296debcb7280785bae9bd86cd224d43868f7f8e926ce52266ef51541ff8974d6055c2cb75f
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.1.3
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gemspec
|
4
|
+
|
5
|
+
group :test do
|
6
|
+
gem 'activerecord'
|
7
|
+
gem 'activesupport'
|
8
|
+
gem 'actionpack' # action_controller, action_view
|
9
|
+
gem 'combustion', '~> 0.5.2'
|
10
|
+
gem 'rack-test'
|
11
|
+
gem 'pundit'
|
12
|
+
gem 'active_model_serializers', '~> 0.8.0'
|
13
|
+
end
|
data/LICENCE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2014 Sam Clopton
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/api_me.png)](http://badge.fury.io/rb/api_me) [![Build Status](https://travis-ci.org/inigo-llc/api_me.png?branch=master)](https://travis-ci.org/inigo-llc/api_me) [![Code Climate](https://codeclimate.com/github/inigo-llc/api_me/badges/gpa.svg)](https://codeclimate.com/github/inigo-llc/api_me) [![Test Coverage](https://codeclimate.com/github/inigo-llc/api_me/badges/coverage.svg)](https://codeclimate.com/github/inigo-llc/api_me)
|
2
|
+
ApiMe
|
3
|
+
=========
|
4
|
+
|
5
|
+
## This gem is currently a work in progress, follows semver, and may change significantly until version 1.0
|
6
|
+
|
7
|
+
### A gem for building RESTful Api resources in Rails
|
8
|
+
ApiMe provides a set of generators and base classes to assist with building Restful API's in Ruby on Rails.
|
9
|
+
|
10
|
+
#### This gem uses the following libraries:
|
11
|
+
* Pundit
|
12
|
+
* Active Model Serializers (0.8)
|
13
|
+
|
14
|
+
#### Todo:
|
15
|
+
* Add the ability to specify resource filters
|
16
|
+
|
17
|
+
## License
|
18
|
+
Copyright (c) 2014, Api Me is developed and maintained by Sam Clopton, and is released under the open MIT Licence.
|
data/Rakefile
ADDED
data/api_me.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'api_me/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'api_me'
|
9
|
+
s.version = ApiMe::VERSION
|
10
|
+
s.authors = ['Sam Clopton']
|
11
|
+
s.email = ['samsinite@gmail.com']
|
12
|
+
s.homepage = 'https://github.com/inigo/api_me'
|
13
|
+
s.summary = 'Api Me'
|
14
|
+
s.description = "This friendly library gives you helpers and generators to assist building RESTful API's in your Rails app."
|
15
|
+
s.license = 'MIT'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
19
|
+
s.require_paths = ['lib', 'app']
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'activerecord', '>= 3.2.0'
|
22
|
+
s.add_runtime_dependency 'activesupport', '>= 3.2.0'
|
23
|
+
s.add_runtime_dependency 'pundit', '~> 0.1'
|
24
|
+
s.add_runtime_dependency 'active_model_serializers', '~> 0.8.0'
|
25
|
+
|
26
|
+
s.add_development_dependency 'combustion', '~> 0.5.1'
|
27
|
+
s.add_development_dependency 'rspec-rails', '~> 2.13'
|
28
|
+
s.add_development_dependency 'sqlite3', '~> 1.3.7'
|
29
|
+
end
|
data/config.ru
ADDED
data/lib/api_me.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
require 'pundit'
|
3
|
+
|
4
|
+
require 'api_me/version'
|
5
|
+
|
6
|
+
module ApiMe
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
include ::Pundit
|
9
|
+
|
10
|
+
included do
|
11
|
+
|
12
|
+
protect_from_forgery :with => :null_session
|
13
|
+
rescue_from Pundit::NotAuthorizedError, :with => :user_not_authorized
|
14
|
+
|
15
|
+
after_action :verify_authorized, :except => :index
|
16
|
+
after_action :verify_policy_scoped, :only => :index
|
17
|
+
end
|
18
|
+
|
19
|
+
module ClassMethods
|
20
|
+
def model(klass)
|
21
|
+
@model_klass = klass
|
22
|
+
end
|
23
|
+
|
24
|
+
def serializer(klass)
|
25
|
+
@serializer_klass = klass
|
26
|
+
end
|
27
|
+
|
28
|
+
if RUBY_VERSION >= '2.0'
|
29
|
+
def model_klass
|
30
|
+
@model_klass ||= Object.const_get(model_klass_name)
|
31
|
+
end
|
32
|
+
|
33
|
+
def serializer_klass
|
34
|
+
@serializer_klass ||= Object.const_get(serializer_klass_name)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
def model_klass
|
38
|
+
@model_klass ||= model_klass_name.safe_constantize
|
39
|
+
end
|
40
|
+
|
41
|
+
def serializer_klass
|
42
|
+
@serializer_klass ||= serializer_klass_name.safe_constantize
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def model_klass_name
|
47
|
+
@model_klass_name ||= self.name.demodulize.sub(/Controller$/, '').singularize
|
48
|
+
end
|
49
|
+
|
50
|
+
def serializer_klass_name
|
51
|
+
@serializer_klass_name ||= "#{self.name.demodulize.sub(/Controller$/, '').singularize}Serializer"
|
52
|
+
end
|
53
|
+
|
54
|
+
def params_klass_symbol
|
55
|
+
self.model_klass.name.downcase.to_sym
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def index
|
60
|
+
@scoped_objects = policy_scope(model_klass.all)
|
61
|
+
render :json => @scoped_objects, :each_serializer => serializer_klass
|
62
|
+
end
|
63
|
+
|
64
|
+
def show
|
65
|
+
@object = model_klass.find(params[:id])
|
66
|
+
authorize @object
|
67
|
+
|
68
|
+
render :json => @object, :serializer => serializer_klass
|
69
|
+
end
|
70
|
+
|
71
|
+
def create
|
72
|
+
begin
|
73
|
+
@object = model_klass.new(object_params)
|
74
|
+
authorize @object
|
75
|
+
@object.save!(object_params)
|
76
|
+
|
77
|
+
render :status => 201, :json => @object, :serializer => serializer_klass
|
78
|
+
rescue ActiveRecord::RecordInvalid => e
|
79
|
+
handle_errors(e)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def update
|
84
|
+
begin
|
85
|
+
@object = model_klass.find(params[:id])
|
86
|
+
authorize @object
|
87
|
+
@object.update!(object_params)
|
88
|
+
|
89
|
+
render :status => 204, :nothing => true
|
90
|
+
rescue ActiveRecord::RecordInvalid => e
|
91
|
+
handle_errors(e)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def destroy
|
96
|
+
@object = model_klass.find(params[:id])
|
97
|
+
authorize @object
|
98
|
+
@object.destroy()
|
99
|
+
|
100
|
+
render :status => 204, :nothing => true
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
|
105
|
+
def object_params
|
106
|
+
params.require(params_klass_symbol).permit(*policy(@object || model_klass).permitted_attributes)
|
107
|
+
end
|
108
|
+
|
109
|
+
def render_errors(errors, status = 422)
|
110
|
+
render(:json => {errors: errors}, :status => status)
|
111
|
+
end
|
112
|
+
|
113
|
+
def handle_errors(e)
|
114
|
+
render_errors(e.record.errors.messages)
|
115
|
+
end
|
116
|
+
|
117
|
+
def user_not_authorized
|
118
|
+
payload = { :message => "User is not allowed to access #{params[:action]} on this resource"}
|
119
|
+
render :json => payload, :status => 403
|
120
|
+
end
|
121
|
+
|
122
|
+
def params_klass_symbol
|
123
|
+
self.class.params_klass_symbol
|
124
|
+
end
|
125
|
+
|
126
|
+
def model_klass
|
127
|
+
self.class.model_klass
|
128
|
+
end
|
129
|
+
|
130
|
+
def serializer_klass
|
131
|
+
self.class.serializer_klass
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
Description:
|
2
|
+
This generator generates an api controller that inherits from ApiMe::BaseController
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate api_controller foo_bar foo:references{polymorphic} bar:belongs_to name description
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
app/controllers/api/v1/foo_bars_controller.rb
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module ApiMe
|
2
|
+
module Generators
|
3
|
+
class ControllerGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
check_class_collision :suffix => "Controller"
|
6
|
+
|
7
|
+
argument :attributes, :type => :array, :default => [], :banner => "field field"
|
8
|
+
|
9
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated controller"
|
10
|
+
|
11
|
+
def create_api_controller_file
|
12
|
+
template 'controller.rb', File.join('app/controllers',
|
13
|
+
controllers_namespace,
|
14
|
+
controllers_api_version,
|
15
|
+
"#{plural_name}_controller.rb")
|
16
|
+
end
|
17
|
+
|
18
|
+
def controllers_namespace
|
19
|
+
"api"
|
20
|
+
# @generators.options.fetch(:api, {}).fetch(:namespace, 'api')
|
21
|
+
end
|
22
|
+
|
23
|
+
def controllers_api_version
|
24
|
+
"v1"
|
25
|
+
# @generators.options.fetch(:api, {}).fetch(:version, 'v1')
|
26
|
+
end
|
27
|
+
|
28
|
+
def controller_class_name
|
29
|
+
"#{controllers_namespace.capitalize}::#{controllers_api_version.capitalize}::#{plural_name.camelize}Controller"
|
30
|
+
end
|
31
|
+
|
32
|
+
def attributes_names
|
33
|
+
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
|
34
|
+
end
|
35
|
+
|
36
|
+
def associations
|
37
|
+
attributes.select { |attr| attr.reference? }
|
38
|
+
end
|
39
|
+
|
40
|
+
def nonpolymorphic_attribute_names
|
41
|
+
associations.select { |attr| attr.type.in?([:belongs_to, :references]) }
|
42
|
+
.reject { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
43
|
+
.map { |attr| "#{attr.name}_id".to_sym }
|
44
|
+
end
|
45
|
+
|
46
|
+
def polymorphic_attribute_names
|
47
|
+
associations.select { |attr| attr.type.in?([:belongs_to, :references]) }
|
48
|
+
.select { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
49
|
+
.map { |attr| ["#{attr.name}_id".to_sym, "#{attr.name}_type".to_sym] }.flatten
|
50
|
+
end
|
51
|
+
|
52
|
+
def association_attribute_names
|
53
|
+
nonpolymorphic_attribute_names + (polymorphic_attribute_names)
|
54
|
+
end
|
55
|
+
|
56
|
+
def strong_parameters
|
57
|
+
(attributes_names + association_attribute_names).map(&:inspect).join(", ")
|
58
|
+
end
|
59
|
+
|
60
|
+
def parent_class_name
|
61
|
+
base_controller_name = "#{controllers_namespace.capitalize}::BaseController"
|
62
|
+
if options[:parent]
|
63
|
+
options[:parent]
|
64
|
+
else
|
65
|
+
"ApplicationController"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
hook_for :test_framework
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ApiMe
|
2
|
+
module Generators
|
3
|
+
class PolicyGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
check_class_collision :suffix => "Policy"
|
6
|
+
|
7
|
+
argument :attributes, :type => :array, :default => [], :banner => "field field"
|
8
|
+
|
9
|
+
class_option :parent, :type => :string, :desc => "The parent class for the generated policy"
|
10
|
+
|
11
|
+
def create_api_policy_file
|
12
|
+
template 'policy.rb', File.join('app/policies', "#{singular_name}_policy.rb")
|
13
|
+
end
|
14
|
+
|
15
|
+
def policy_class_name
|
16
|
+
"#{class_name}Policy"
|
17
|
+
end
|
18
|
+
|
19
|
+
def attributes_names
|
20
|
+
attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
|
21
|
+
end
|
22
|
+
|
23
|
+
def associations
|
24
|
+
attributes.select { |attr| attr.reference? }
|
25
|
+
end
|
26
|
+
|
27
|
+
def nonpolymorphic_attribute_names
|
28
|
+
associations.select { |attr| attr.type.in?([:belongs_to, :references]) }
|
29
|
+
.reject { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
30
|
+
.map { |attr| "#{attr.name}_id".to_sym }
|
31
|
+
end
|
32
|
+
|
33
|
+
def polymorphic_attribute_names
|
34
|
+
associations.select { |attr| attr.type.in?([:belongs_to, :references]) }
|
35
|
+
.select { |attr| attr.attr_options.fetch(:polymorphic, false) }
|
36
|
+
.map { |attr| ["#{attr.name}_id".to_sym, "#{attr.name}_type".to_sym] }.flatten
|
37
|
+
end
|
38
|
+
|
39
|
+
def association_attribute_names
|
40
|
+
nonpolymorphic_attribute_names + (polymorphic_attribute_names)
|
41
|
+
end
|
42
|
+
|
43
|
+
def strong_parameters
|
44
|
+
(attributes_names + association_attribute_names).map(&:inspect).join(", ")
|
45
|
+
end
|
46
|
+
|
47
|
+
def parent_class_name
|
48
|
+
if options[:parent]
|
49
|
+
options[:parent]
|
50
|
+
else
|
51
|
+
"ApplicationPolicy"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
hook_for :test_framework
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Description:
|
2
|
+
This generator generates an api resource that inherit from ApiMe::BaseController
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate api_resource foo_bar foo:references{polymorphic} bar:belongs_to name description
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
app/models/foo_bar.rb
|
9
|
+
app/serializers/foo_bar_serializer.rb
|
10
|
+
app/policy/foo_bar_policy.rb
|
11
|
+
app/controllers/api/v1/foo_bars_controller.rb
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module ApiMe
|
2
|
+
module Generators
|
3
|
+
class ResourceGenerator < Rails::Generators::Base
|
4
|
+
def run_generators
|
5
|
+
params = @_initializer[0]
|
6
|
+
invoke "model", params
|
7
|
+
invoke "serializer", params + ["created_at", "updated_at"]
|
8
|
+
invoke "api_me:policy", params
|
9
|
+
invoke "api_me:controller", params
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Users API' do
|
4
|
+
it 'sends the list of users' do
|
5
|
+
users = [
|
6
|
+
User.create(:username => "Test"),
|
7
|
+
User.create(:username => "Test 2")
|
8
|
+
]
|
9
|
+
|
10
|
+
get '/api/v1/users'
|
11
|
+
|
12
|
+
expect(last_response.status).to eq(200)
|
13
|
+
json = JSON.parse(last_response.body)
|
14
|
+
|
15
|
+
expect(json['users'].length).to eq(2)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'sends an individual user' do
|
19
|
+
user = User.create(:username => "Test")
|
20
|
+
|
21
|
+
get '/api/v1/users/' + user.id.to_s + '/'
|
22
|
+
|
23
|
+
expect(last_response.status).to eq(200)
|
24
|
+
json = JSON.parse(last_response.body)
|
25
|
+
|
26
|
+
expect(json['user']['id']).to eq(user.id)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'creates a new user' do
|
30
|
+
user_params = {
|
31
|
+
:username => "Test"
|
32
|
+
}
|
33
|
+
|
34
|
+
post '/api/v1/users/', {:user => user_params}
|
35
|
+
|
36
|
+
expect(last_response.status).to eq(201)
|
37
|
+
json = JSON.parse(last_response.body)
|
38
|
+
|
39
|
+
expect(json['user']['username']).to eq(user_params[:username])
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'updates an existing user' do
|
43
|
+
user = User.create(:username => "Foo")
|
44
|
+
|
45
|
+
expect(user.username).to eq("Foo")
|
46
|
+
|
47
|
+
put '/api/v1/users/' + user.id.to_s + '/', {:user => {:username => "Bar"}}
|
48
|
+
|
49
|
+
updated_user = User.find(user.id)
|
50
|
+
expect(last_response.status).to eq(204)
|
51
|
+
expect(updated_user.username).to eq("Bar")
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'destroys an existing user' do
|
55
|
+
user = User.create(:username => "Foo")
|
56
|
+
|
57
|
+
expect(user.id).to_not eq(nil)
|
58
|
+
|
59
|
+
delete '/api/v1/users/' + user.id.to_s + '/'
|
60
|
+
|
61
|
+
does_user_exist = User.where(:id => user.id).exists?
|
62
|
+
expect(last_response.status).to eq(204)
|
63
|
+
expect(does_user_exist).to eq(false)
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class ApplicationController < ActionController::Base
|
2
|
+
# Prevent CSRF attacks by raising an exception.
|
3
|
+
# For APIs, you may want to use :null_session instead.
|
4
|
+
protect_from_forgery :with => :exception
|
5
|
+
|
6
|
+
private
|
7
|
+
|
8
|
+
# Needed by pundit
|
9
|
+
def current_user
|
10
|
+
nil
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
class ApplicationPolicy
|
2
|
+
attr_reader :user, :record
|
3
|
+
|
4
|
+
def initialize(user, record)
|
5
|
+
@user = user
|
6
|
+
@record = record
|
7
|
+
end
|
8
|
+
|
9
|
+
def index?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
def show?
|
14
|
+
scope.where(:id => record.id).exists?
|
15
|
+
end
|
16
|
+
|
17
|
+
def create?
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
def new?
|
22
|
+
create?
|
23
|
+
end
|
24
|
+
|
25
|
+
def update?
|
26
|
+
true
|
27
|
+
end
|
28
|
+
|
29
|
+
def edit?
|
30
|
+
update?
|
31
|
+
end
|
32
|
+
|
33
|
+
def destroy?
|
34
|
+
true
|
35
|
+
end
|
36
|
+
|
37
|
+
def scope
|
38
|
+
Pundit.policy_scope!(user, record.class)
|
39
|
+
end
|
40
|
+
|
41
|
+
class Scope
|
42
|
+
attr_reader :user, :scope
|
43
|
+
|
44
|
+
def initialize(user, scope)
|
45
|
+
@user = user
|
46
|
+
@scope = scope
|
47
|
+
end
|
48
|
+
|
49
|
+
def resolve
|
50
|
+
readable
|
51
|
+
end
|
52
|
+
|
53
|
+
def readable
|
54
|
+
scope
|
55
|
+
end
|
56
|
+
|
57
|
+
def modifyable
|
58
|
+
scope
|
59
|
+
end
|
60
|
+
|
61
|
+
def destroyable
|
62
|
+
scope
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
*.log
|
File without changes
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'combustion'
|
4
|
+
|
5
|
+
Bundler.require :default, :development
|
6
|
+
|
7
|
+
Combustion.initialize! :active_record, :action_controller
|
8
|
+
|
9
|
+
require 'rspec/rails'
|
10
|
+
require 'rack/test'
|
11
|
+
|
12
|
+
module ApiHelper
|
13
|
+
include Rack::Test::Methods
|
14
|
+
|
15
|
+
def app
|
16
|
+
Rails.application
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec.configure do |config|
|
21
|
+
config.use_transactional_fixtures = true
|
22
|
+
|
23
|
+
config.include ApiHelper
|
24
|
+
|
25
|
+
end
|
metadata
CHANGED
@@ -1,108 +1,156 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_me
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Clopton
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.2.0
|
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
26
|
version: 3.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: 3.2.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.2.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: pundit
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0.1'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: active_model_serializers
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.8.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.8.0
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: combustion
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - ~>
|
73
|
+
- - "~>"
|
60
74
|
- !ruby/object:Gem::Version
|
61
75
|
version: 0.5.1
|
62
76
|
type: :development
|
63
77
|
prerelease: false
|
64
78
|
version_requirements: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
|
-
- - ~>
|
80
|
+
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: 0.5.1
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: rspec-rails
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
72
86
|
requirements:
|
73
|
-
- - ~>
|
87
|
+
- - "~>"
|
74
88
|
- !ruby/object:Gem::Version
|
75
89
|
version: '2.13'
|
76
90
|
type: :development
|
77
91
|
prerelease: false
|
78
92
|
version_requirements: !ruby/object:Gem::Requirement
|
79
93
|
requirements:
|
80
|
-
- - ~>
|
94
|
+
- - "~>"
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '2.13'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
98
|
name: sqlite3
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
|
-
- - ~>
|
101
|
+
- - "~>"
|
88
102
|
- !ruby/object:Gem::Version
|
89
103
|
version: 1.3.7
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
|
-
- - ~>
|
108
|
+
- - "~>"
|
95
109
|
- !ruby/object:Gem::Version
|
96
110
|
version: 1.3.7
|
97
|
-
description: This friendly library gives you helpers to assist building
|
98
|
-
in your Rails app.
|
111
|
+
description: This friendly library gives you helpers and generators to assist building
|
112
|
+
RESTful API's in your Rails app.
|
99
113
|
email:
|
100
114
|
- samsinite@gmail.com
|
101
115
|
executables: []
|
102
116
|
extensions: []
|
103
117
|
extra_rdoc_files: []
|
104
|
-
files:
|
105
|
-
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".ruby-version"
|
121
|
+
- ".travis.yml"
|
122
|
+
- Gemfile
|
123
|
+
- LICENCE
|
124
|
+
- README.md
|
125
|
+
- Rakefile
|
126
|
+
- api_me.gemspec
|
127
|
+
- config.ru
|
128
|
+
- lib/api_me.rb
|
129
|
+
- lib/api_me/version.rb
|
130
|
+
- lib/generators/api_me/controller/USAGE
|
131
|
+
- lib/generators/api_me/controller/controller_generator.rb
|
132
|
+
- lib/generators/api_me/controller/templates/controller.rb
|
133
|
+
- lib/generators/api_me/policy/USAGE
|
134
|
+
- lib/generators/api_me/policy/policy_generator.rb
|
135
|
+
- lib/generators/api_me/policy/templates/policy.rb
|
136
|
+
- lib/generators/api_me/resource/USAGE
|
137
|
+
- lib/generators/api_me/resource/resource_generator.rb
|
138
|
+
- spec/acceptance/api/v1/fails_spec.rb
|
139
|
+
- spec/acceptance/api/v1/users_spec.rb
|
140
|
+
- spec/internal/app/controllers/api/v1/fails_controller.rb
|
141
|
+
- spec/internal/app/controllers/api/v1/users_controller.rb
|
142
|
+
- spec/internal/app/controllers/application_controller.rb
|
143
|
+
- spec/internal/app/models/user.rb
|
144
|
+
- spec/internal/app/policies/application_policy.rb
|
145
|
+
- spec/internal/app/policies/user_policy.rb
|
146
|
+
- spec/internal/app/serializers/user_serializer.rb
|
147
|
+
- spec/internal/config/database.yml
|
148
|
+
- spec/internal/config/routes.rb
|
149
|
+
- spec/internal/db/schema.rb
|
150
|
+
- spec/internal/log/.gitignore
|
151
|
+
- spec/internal/public/favicon.ico
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
homepage: https://github.com/inigo/api_me
|
106
154
|
licenses:
|
107
155
|
- MIT
|
108
156
|
metadata: {}
|
@@ -110,19 +158,20 @@ post_install_message:
|
|
110
158
|
rdoc_options: []
|
111
159
|
require_paths:
|
112
160
|
- lib
|
161
|
+
- app
|
113
162
|
required_ruby_version: !ruby/object:Gem::Requirement
|
114
163
|
requirements:
|
115
|
-
- -
|
164
|
+
- - ">="
|
116
165
|
- !ruby/object:Gem::Version
|
117
166
|
version: '0'
|
118
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
168
|
requirements:
|
120
|
-
- -
|
169
|
+
- - ">="
|
121
170
|
- !ruby/object:Gem::Version
|
122
171
|
version: '0'
|
123
172
|
requirements: []
|
124
173
|
rubyforge_project:
|
125
|
-
rubygems_version: 2.
|
174
|
+
rubygems_version: 2.2.2
|
126
175
|
signing_key:
|
127
176
|
specification_version: 4
|
128
177
|
summary: Api Me
|