elocal_api_support 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/elocal_api_support.gemspec +29 -0
- data/lib/elocal_api_support/actions/create.rb +16 -0
- data/lib/elocal_api_support/actions/destroy.rb +15 -0
- data/lib/elocal_api_support/actions/index.rb +16 -0
- data/lib/elocal_api_support/actions/show.rb +9 -0
- data/lib/elocal_api_support/actions/update.rb +14 -0
- data/lib/elocal_api_support/actions.rb +54 -0
- data/lib/elocal_api_support/authorization.rb +36 -0
- data/lib/elocal_api_support/default_authorizer.rb +30 -0
- data/lib/elocal_api_support/enable_cors.rb +23 -0
- data/lib/elocal_api_support/model_from_params.rb +97 -0
- data/lib/elocal_api_support/version.rb +3 -0
- data/lib/elocal_api_support.rb +20 -0
- data/spec/authorization_spec.rb +68 -0
- data/spec/fixtures/application.rb +35 -0
- data/spec/fixtures/controllers.rb +8 -0
- data/spec/spec_helper.rb +87 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cdeb546097909b97e80efce4c8312735a3b17ea1
|
4
|
+
data.tar.gz: eda8b70eecc679af927b2bf35f9e0f78344dc008
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fb7e49521e3468b881e9b63891571641c6e2facd1abc9dcd1dd5ca06c18403ecb760f97783d7a0d2e4c0939ea0728fa572deccce927ca635a52ad67c67d9d474
|
7
|
+
data.tar.gz: e9fb097ea3ae497a5f5dcc373119aa2fef05d3237555024b9e30759276c7f897a6849ad888576fd1871ca87e812ebafed4f05e8b3debf5259cb6983cd9d4eff1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Rob Di Marco
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# ElocalApiSupport
|
2
|
+
|
3
|
+
Some useful utilities for building controllers to be used in JSON APIs
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'elocal_api_support'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install elocal_api_support
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Authorization
|
22
|
+
|
23
|
+
include ElocalApiSupport::Authorization
|
24
|
+
|
25
|
+
### Authorization
|
26
|
+
|
27
|
+
include ElocalApiSupport::EnableCors
|
28
|
+
|
29
|
+
### Authorization
|
30
|
+
|
31
|
+
include ElocalApiSupport::ModelFromParams
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'elocal_api_support/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "elocal_api_support"
|
8
|
+
spec.version = ElocalApiSupport::VERSION
|
9
|
+
spec.authors = ["Rob Di Marco"]
|
10
|
+
spec.email = ["rob@elocal.com"]
|
11
|
+
spec.description = %q{Utilities for controllers when creating a JSON API}
|
12
|
+
spec.summary = %q{Utilities for controllers when creating a JSON API}
|
13
|
+
spec.homepage = "http://github.com/elocal/elocal_api_support"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'activesupport', '>= 4.0.0'
|
22
|
+
spec.add_dependency 'actionpack', '>= 4.0.0'
|
23
|
+
spec.add_dependency 'activerecord', '>= 4.0.0'
|
24
|
+
spec.add_dependency 'responders', '~> 2.0'
|
25
|
+
spec.add_dependency 'kaminari'
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rspec-rails"
|
28
|
+
spec.add_development_dependency "rake"
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ElocalApiSupport
|
2
|
+
module Actions
|
3
|
+
module Create
|
4
|
+
def create
|
5
|
+
params.permit!
|
6
|
+
obj = associated_model.new(params[@model_name.to_sym])
|
7
|
+
|
8
|
+
if obj.save
|
9
|
+
render json: obj
|
10
|
+
else
|
11
|
+
render json: {errors: obj.errors}, status: 422
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ElocalApiSupport
|
2
|
+
module Actions
|
3
|
+
module Destroy
|
4
|
+
def destroy
|
5
|
+
if lookup_object.destroy
|
6
|
+
render json: lookup_object
|
7
|
+
else
|
8
|
+
render json: {errors: "Failed to destroy #{lookup_object}"}, status: 500
|
9
|
+
end
|
10
|
+
rescue ActiveRecord::RecordNotFound
|
11
|
+
render json: {} # record not found is ok for destroy
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module ElocalApiSupport
|
2
|
+
module Actions
|
3
|
+
module Index
|
4
|
+
def index
|
5
|
+
render json: {
|
6
|
+
current_page: current_page,
|
7
|
+
per_page: (params[:page].present? || params[:per_page].present?) ? per_page : filtered_objects.total_count,
|
8
|
+
total_entries: filtered_objects.total_count,
|
9
|
+
total_pages: (params[:page].present? || params[:per_page].present?) ? filtered_objects.total_pages : 1,
|
10
|
+
records: filtered_objects_for_json
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module ElocalApiSupport
|
2
|
+
module Actions
|
3
|
+
module Update
|
4
|
+
def update
|
5
|
+
params.permit!
|
6
|
+
if lookup_object.update_attributes(params[associated_model_name])
|
7
|
+
render json: lookup_object
|
8
|
+
else
|
9
|
+
render json: {errors: lookup_object.errors}, status: 422
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'elocal_api_support/actions/create'
|
2
|
+
require 'elocal_api_support/actions/destroy'
|
3
|
+
require 'elocal_api_support/actions/index'
|
4
|
+
require 'elocal_api_support/actions/show'
|
5
|
+
require 'elocal_api_support/actions/update'
|
6
|
+
|
7
|
+
module ElocalApiSupport
|
8
|
+
module Actions
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
included do
|
12
|
+
include ElocalApiSupport::Actions::Common
|
13
|
+
include ElocalApiSupport::Actions::Create
|
14
|
+
include ElocalApiSupport::Actions::Destroy
|
15
|
+
include ElocalApiSupport::Actions::Index
|
16
|
+
include ElocalApiSupport::Actions::Show
|
17
|
+
include ElocalApiSupport::Actions::Update
|
18
|
+
end
|
19
|
+
|
20
|
+
module Common
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def filtered_objects_for_json
|
25
|
+
|
26
|
+
if associated_model_serializer
|
27
|
+
filtered_objects.map{|r| associated_model_serializer.new(r)}
|
28
|
+
else
|
29
|
+
filtered_objects
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def associated_model_serializer
|
34
|
+
unless @associated_model_serializer_lookup_complete
|
35
|
+
c = "#{associated_model}Serializer"
|
36
|
+
@associated_model_serializer = if Object.const_defined?(c)
|
37
|
+
Rails.logger.debug("Using #{c}")
|
38
|
+
c.constantize
|
39
|
+
else
|
40
|
+
Rails.logger.debug("No serializer #{c}")
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
@associated_model_serializer_lookup_complete = true
|
44
|
+
end
|
45
|
+
@associated_model_serializer
|
46
|
+
end
|
47
|
+
|
48
|
+
def associated_model_name
|
49
|
+
@model_name ||= controller_name.singularize
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module ElocalApiSupport::Authorization
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
before_filter :authorize!
|
6
|
+
end
|
7
|
+
|
8
|
+
protected
|
9
|
+
|
10
|
+
def authorized?
|
11
|
+
find_authorizer.authorize(authorize_request_token)
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_authorizer
|
15
|
+
if respond_to?(:authorizer, true)
|
16
|
+
send(:authorizer)
|
17
|
+
else
|
18
|
+
DefaultAuthorizer.new(self)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def error_response_hash
|
23
|
+
{ error: 'You are not an authorized user!' }.to_json
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorize!
|
27
|
+
unless authorized?
|
28
|
+
Rails.logger.warn("Somebody else tried to access our internal API! Value: #{authorize_request_token} Params: #{params}, Headers: #{request.headers.map{ |k, _v| k }}")
|
29
|
+
render json: error_response_hash, status: 401
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def authorize_request_token
|
34
|
+
[params[:request_token], request.headers["HTTP_X_REQUEST_TOKEN"]].detect(&:present?)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ElocalApiSupport
|
2
|
+
class Authorization::DefaultAuthorizer
|
3
|
+
attr_reader :caller
|
4
|
+
|
5
|
+
FAIL_MESSAGE = <<-EOL.strip
|
6
|
+
No token could be found for ElocalApiSupport to use. Please resolve this by either
|
7
|
+
- Define a method required_token which provides a token to check
|
8
|
+
- Set the configuration token in your config/application.rb by setting a value for config.elocal_api_support_token
|
9
|
+
- Define a method authorizer return a custom Authorization object which responds to authorize(token)
|
10
|
+
EOL
|
11
|
+
|
12
|
+
def initialize(caller)
|
13
|
+
@caller = caller
|
14
|
+
end
|
15
|
+
|
16
|
+
def authorize(token)
|
17
|
+
find_required_token == token
|
18
|
+
end
|
19
|
+
|
20
|
+
def find_required_token
|
21
|
+
if caller.respond_to?(:required_token, true)
|
22
|
+
caller.send(:required_token)
|
23
|
+
elsif Rails.application.config.elocal_api_support_token.present?
|
24
|
+
Rails.application.config.elocal_api_support_token
|
25
|
+
else
|
26
|
+
fail FAIL_MESSAGE
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ElocalApiSupport::EnableCors
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
included do
|
4
|
+
cors_allow_all
|
5
|
+
before_filter :enable_cors
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
attr_accessor :cors_allow_origin, :cors_allow_methods, :cors_allow_headers
|
10
|
+
def cors_allow_all
|
11
|
+
self.cors_allow_origin = "*"
|
12
|
+
self.cors_allow_methods = %w{GET POST PUT DELETE}.join(",")
|
13
|
+
self.cors_allow_headers = %w{Origin Accept Content-Type X-Requested-With X-XSRF-Token}.join(",")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def enable_cors
|
18
|
+
response.headers['Access-Control-Allow-Origin'] = self.class.cors_allow_origin
|
19
|
+
response.headers['Access-Control-Allow-Methods'] = self.class.cors_allow_methods
|
20
|
+
response.headers['Access-Control-Allow-Headers'] = self.class.cors_allow_headers
|
21
|
+
head(:ok) if request.request_method == "OPTIONS"
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module ElocalApiSupport::ModelFromParams
|
2
|
+
#
|
3
|
+
# Including classes need to define methods
|
4
|
+
# - params
|
5
|
+
# - associated_model_name
|
6
|
+
#
|
7
|
+
protected
|
8
|
+
|
9
|
+
def lookup_object
|
10
|
+
@lookup_object ||= with_includes(associated_model.where(id: params[:id])).first
|
11
|
+
end
|
12
|
+
|
13
|
+
def filtered_objects
|
14
|
+
@filtered_objects ||= with_pagination(with_sorting(with_filters(with_includes(associated_model))))
|
15
|
+
end
|
16
|
+
|
17
|
+
def with_pagination(rel)
|
18
|
+
if params[:page] || params[:per_page]
|
19
|
+
rel.page(current_page).per(per_page)
|
20
|
+
else
|
21
|
+
rel.page(current_page).limit(false)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_filters(rel)
|
26
|
+
allowed_filter_columns.each do |param_name|
|
27
|
+
if params[param_name].present?
|
28
|
+
if respond_to?(:"with_#{associated_model.to_s.downcase}_by_#{param_name}", true)
|
29
|
+
rel = send(:"with_#{associated_model.to_s.downcase}_by_#{param_name}", params[param_name])
|
30
|
+
elsif associated_model.respond_to?(:"with_#{param_name}", true)
|
31
|
+
rel = rel.send(:"with_#{param_name}", params[param_name])
|
32
|
+
else
|
33
|
+
rel = rel.where({param_name.to_sym => params[param_name]})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
rel
|
39
|
+
end
|
40
|
+
|
41
|
+
def with_sorting(rel)
|
42
|
+
if filter_sort_col.present?
|
43
|
+
if respond_to?(:"order_#{associated_model.to_s.downcase}_by_#{filter_sort_col}", true)
|
44
|
+
rel = send(:"order_#{associated_model.to_s.downcase}_by_#{filter_sort_col}", rel)
|
45
|
+
elsif associated_model.respond_to?(:"order_by_#{filter_sort_col}", true)
|
46
|
+
rel = rel.send(:"order_by_#{filter_sort_col}", params[filter_sort_col], filter_sort_direction)
|
47
|
+
else
|
48
|
+
rel = rel.order("#{associated_model.table_name}.#{filter_sort_col} #{filter_sort_direction}")
|
49
|
+
end
|
50
|
+
end
|
51
|
+
rel
|
52
|
+
end
|
53
|
+
|
54
|
+
def with_includes(rel)
|
55
|
+
rel
|
56
|
+
end
|
57
|
+
|
58
|
+
def per_page
|
59
|
+
@per_page ||= (params[:per_page] || 10).to_i
|
60
|
+
end
|
61
|
+
|
62
|
+
def current_page
|
63
|
+
@current_page ||= (params[:page] || 1).to_i
|
64
|
+
end
|
65
|
+
|
66
|
+
def allowed_sort_columns
|
67
|
+
associated_model_columns
|
68
|
+
end
|
69
|
+
|
70
|
+
def allowed_filter_columns
|
71
|
+
associated_model_columns
|
72
|
+
end
|
73
|
+
|
74
|
+
def associated_model
|
75
|
+
@associated_model ||= associated_model_name.camelize.constantize
|
76
|
+
end
|
77
|
+
|
78
|
+
def associated_model_columns
|
79
|
+
@associated_model_columns ||= associated_model.columns.map(&:name)
|
80
|
+
end
|
81
|
+
|
82
|
+
def filter_sort_col
|
83
|
+
if params[:sort] && params[:sort][:key] && allowed_sort_columns.include?(params[:sort][:key])
|
84
|
+
params[:sort][:key]
|
85
|
+
else
|
86
|
+
nil
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def filter_sort_direction
|
91
|
+
if params[:sort] && params[:sort][:direction] && ['asc','desc'].include?(params[:sort][:direction])
|
92
|
+
params[:sort][:direction]
|
93
|
+
else
|
94
|
+
''
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'elocal_api_support/version'
|
2
|
+
require 'active_support/concern.rb'
|
3
|
+
require 'elocal_api_support/authorization'
|
4
|
+
require 'elocal_api_support/default_authorizer'
|
5
|
+
require 'elocal_api_support/enable_cors'
|
6
|
+
require 'elocal_api_support/model_from_params'
|
7
|
+
require 'elocal_api_support/actions'
|
8
|
+
|
9
|
+
module ElocalApiSupport
|
10
|
+
module All
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
included do
|
14
|
+
include ElocalApiSupport::Actions
|
15
|
+
include ElocalApiSupport::Authorization
|
16
|
+
include ElocalApiSupport::EnableCors
|
17
|
+
include ElocalApiSupport::ModelFromParams
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class AuthController < FakesController
|
4
|
+
include ElocalApiSupport::Authorization
|
5
|
+
|
6
|
+
protected
|
7
|
+
|
8
|
+
def required_token
|
9
|
+
'mysecretkey'
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class FakeAuthorizer
|
14
|
+
def authorize(token)
|
15
|
+
token == 'FakeAuthorizerKey'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe AuthController, type: :controller do
|
20
|
+
let(:error_response) { { 'error' => 'You are not an authorized user!' }.to_json }
|
21
|
+
|
22
|
+
describe 'With Default Authorizer' do
|
23
|
+
it 'uses default authorizer' do
|
24
|
+
expect(controller.send(:find_authorizer)).to be_a(ElocalApiSupport::Authorization::DefaultAuthorizer)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'requires a token' do
|
28
|
+
get :index
|
29
|
+
expect(response).to have_http_status(401)
|
30
|
+
expect(response.body).to eq(error_response)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'does not allow wrong token' do
|
34
|
+
get :index, request_token: 'ThisIsNotTheKey'
|
35
|
+
expect(response).to have_http_status(401)
|
36
|
+
expect(response.body).to eq(error_response)
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'allows the right token' do
|
40
|
+
get :index, request_token: 'mysecretkey'
|
41
|
+
expect(response).to have_http_status(200)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe 'With Custom Authorizer' do
|
46
|
+
controller do
|
47
|
+
protected
|
48
|
+
|
49
|
+
def authorizer
|
50
|
+
FakeAuthorizer.new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'uses the custom authorizer' do
|
55
|
+
expect(controller.send(:find_authorizer)).to be_a(FakeAuthorizer)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'does not use default behavior' do
|
59
|
+
get :index, request_token: 'mysecretkey'
|
60
|
+
expect(response).to have_http_status(401)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'authorizes appropriately' do
|
64
|
+
get :index, request_token: 'FakeAuthorizerKey'
|
65
|
+
expect(response).to have_http_status(200)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_dispatch'
|
4
|
+
|
5
|
+
module Rails
|
6
|
+
|
7
|
+
class App
|
8
|
+
def env_config; {} end
|
9
|
+
def config
|
10
|
+
OpenStruct.new
|
11
|
+
end
|
12
|
+
def routes
|
13
|
+
return @routes if defined?(@routes)
|
14
|
+
@routes = ActionDispatch::Routing::RouteSet.new
|
15
|
+
@routes.draw do
|
16
|
+
resources :auth
|
17
|
+
end
|
18
|
+
@routes
|
19
|
+
end
|
20
|
+
def logger
|
21
|
+
@logger ||= begin
|
22
|
+
Dir.mkdir('log') unless Dir.exists?('log')
|
23
|
+
Logger.new('log/test.log')
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.logger
|
29
|
+
self.application.logger
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.application
|
33
|
+
@app ||= App.new
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, make a
|
10
|
+
# separate helper file that requires this one and then use it only in the specs
|
11
|
+
# that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
require 'rubygems'
|
18
|
+
require 'bundler/setup'
|
19
|
+
require 'responders'
|
20
|
+
require 'fixtures/application'
|
21
|
+
require 'fixtures/controllers'
|
22
|
+
require 'rspec/rails'
|
23
|
+
|
24
|
+
require 'elocal_api_support'
|
25
|
+
|
26
|
+
RSpec.configure do |config|
|
27
|
+
# The settings below are suggested to provide a good initial experience
|
28
|
+
# with RSpec, but feel free to customize to your heart's content.
|
29
|
+
=begin
|
30
|
+
# These two settings work together to allow you to limit a spec run
|
31
|
+
# to individual examples or groups you care about by tagging them with
|
32
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
33
|
+
# get run.
|
34
|
+
config.filter_run :focus
|
35
|
+
config.run_all_when_everything_filtered = true
|
36
|
+
|
37
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
38
|
+
# file, and it's useful to allow more verbose output when running an
|
39
|
+
# individual spec file.
|
40
|
+
if config.files_to_run.one?
|
41
|
+
# Use the documentation formatter for detailed output,
|
42
|
+
# unless a formatter has already been configured
|
43
|
+
# (e.g. via a command-line flag).
|
44
|
+
config.default_formatter = 'doc'
|
45
|
+
end
|
46
|
+
|
47
|
+
# Print the 10 slowest examples and example groups at the
|
48
|
+
# end of the spec run, to help surface which specs are running
|
49
|
+
# particularly slow.
|
50
|
+
config.profile_examples = 10
|
51
|
+
|
52
|
+
# Run specs in random order to surface order dependencies. If you find an
|
53
|
+
# order dependency and want to debug it, you can fix the order by providing
|
54
|
+
# the seed, which is printed after each run.
|
55
|
+
# --seed 1234
|
56
|
+
config.order = :random
|
57
|
+
|
58
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
59
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
60
|
+
# test failures related to randomization by passing the same `--seed` value
|
61
|
+
# as the one that triggered the failure.
|
62
|
+
Kernel.srand config.seed
|
63
|
+
|
64
|
+
# rspec-expectations config goes here. You can use an alternate
|
65
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
66
|
+
# assertions if you prefer.
|
67
|
+
config.expect_with :rspec do |expectations|
|
68
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
69
|
+
# For more details, see:
|
70
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
71
|
+
expectations.syntax = :expect
|
72
|
+
end
|
73
|
+
|
74
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
75
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
76
|
+
config.mock_with :rspec do |mocks|
|
77
|
+
# Enable only the newer, non-monkey-patching expect syntax.
|
78
|
+
# For more details, see:
|
79
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
80
|
+
mocks.syntax = :expect
|
81
|
+
|
82
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
83
|
+
# a real object. This is generally recommended.
|
84
|
+
mocks.verify_partial_doubles = true
|
85
|
+
end
|
86
|
+
=end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: elocal_api_support
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rob Di Marco
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.0.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.0.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: responders
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: kaminari
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: bundler
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1.3'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec-rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rake
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: Utilities for controllers when creating a JSON API
|
126
|
+
email:
|
127
|
+
- rob@elocal.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- Gemfile
|
135
|
+
- LICENSE.txt
|
136
|
+
- README.md
|
137
|
+
- Rakefile
|
138
|
+
- elocal_api_support.gemspec
|
139
|
+
- lib/elocal_api_support.rb
|
140
|
+
- lib/elocal_api_support/actions.rb
|
141
|
+
- lib/elocal_api_support/actions/create.rb
|
142
|
+
- lib/elocal_api_support/actions/destroy.rb
|
143
|
+
- lib/elocal_api_support/actions/index.rb
|
144
|
+
- lib/elocal_api_support/actions/show.rb
|
145
|
+
- lib/elocal_api_support/actions/update.rb
|
146
|
+
- lib/elocal_api_support/authorization.rb
|
147
|
+
- lib/elocal_api_support/default_authorizer.rb
|
148
|
+
- lib/elocal_api_support/enable_cors.rb
|
149
|
+
- lib/elocal_api_support/model_from_params.rb
|
150
|
+
- lib/elocal_api_support/version.rb
|
151
|
+
- spec/authorization_spec.rb
|
152
|
+
- spec/fixtures/application.rb
|
153
|
+
- spec/fixtures/controllers.rb
|
154
|
+
- spec/spec_helper.rb
|
155
|
+
homepage: http://github.com/elocal/elocal_api_support
|
156
|
+
licenses:
|
157
|
+
- MIT
|
158
|
+
metadata: {}
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
requirements: []
|
174
|
+
rubyforge_project:
|
175
|
+
rubygems_version: 2.4.3
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: Utilities for controllers when creating a JSON API
|
179
|
+
test_files:
|
180
|
+
- spec/authorization_spec.rb
|
181
|
+
- spec/fixtures/application.rb
|
182
|
+
- spec/fixtures/controllers.rb
|
183
|
+
- spec/spec_helper.rb
|