api_me 0.9.4 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +16 -39
  3. data/lib/api_me.rb +12 -10
  4. data/lib/api_me/base_filter.rb +2 -0
  5. data/lib/api_me/engine.rb +5 -0
  6. data/lib/api_me/model.rb +2 -0
  7. data/lib/api_me/pagination.rb +28 -11
  8. data/lib/api_me/sorting.rb +24 -11
  9. data/lib/api_me/version.rb +3 -1
  10. data/lib/generators/api_me/controller/controller_generator.rb +8 -6
  11. data/lib/generators/api_me/filter/filter_generator.rb +2 -0
  12. data/lib/generators/api_me/install_generator.rb +2 -0
  13. data/lib/generators/api_me/policy/policy_generator.rb +2 -0
  14. data/lib/generators/api_me/resource/resource_generator.rb +2 -0
  15. metadata +25 -92
  16. data/.editorconfig +0 -41
  17. data/.gitignore +0 -3
  18. data/.rubocop.yml +0 -8
  19. data/.ruby-version +0 -1
  20. data/.travis.yml +0 -4
  21. data/Gemfile +0 -11
  22. data/Gemfile.lock +0 -153
  23. data/LICENSE +0 -20
  24. data/README.md +0 -194
  25. data/api_me.gemspec +0 -33
  26. data/config.ru +0 -7
  27. data/spec/acceptance/api/v1/fails_spec.rb +0 -12
  28. data/spec/acceptance/api/v1/posts_spec.rb +0 -154
  29. data/spec/acceptance/api/v1/users_spec.rb +0 -96
  30. data/spec/acceptance/multi_word_resource_spec.rb +0 -12
  31. data/spec/internal/app/controllers/api/v1/fails_controller.rb +0 -11
  32. data/spec/internal/app/controllers/api/v1/multi_word_resources_controller.rb +0 -6
  33. data/spec/internal/app/controllers/api/v1/posts_controller.rb +0 -3
  34. data/spec/internal/app/controllers/api/v1/users_controller.rb +0 -3
  35. data/spec/internal/app/controllers/application_controller.rb +0 -12
  36. data/spec/internal/app/filters/user_filter.rb +0 -7
  37. data/spec/internal/app/models/post.rb +0 -3
  38. data/spec/internal/app/models/test_model.rb +0 -21
  39. data/spec/internal/app/models/user.rb +0 -3
  40. data/spec/internal/app/policies/application_policy.rb +0 -65
  41. data/spec/internal/app/policies/post_policy.rb +0 -4
  42. data/spec/internal/app/policies/test_model_policy.rb +0 -5
  43. data/spec/internal/app/policies/user_policy.rb +0 -8
  44. data/spec/internal/app/serializers/post_serializer.rb +0 -5
  45. data/spec/internal/app/serializers/test_model_serializer.rb +0 -5
  46. data/spec/internal/app/serializers/user_serializer.rb +0 -3
  47. data/spec/internal/config/database.yml +0 -3
  48. data/spec/internal/config/initializers/active_model_serializers.rb +0 -1
  49. data/spec/internal/config/routes.rb +0 -10
  50. data/spec/internal/db/schema.rb +0 -12
  51. data/spec/internal/log/.gitignore +0 -1
  52. data/spec/internal/public/favicon.ico +0 -0
  53. data/spec/spec_helper.rb +0 -24
data/.editorconfig DELETED
@@ -1,41 +0,0 @@
1
- # EditorConfig helps developers define and maintain consistent
2
- # coding styles between different editors and IDEs
3
- # editorconfig.org
4
-
5
- root = true
6
-
7
-
8
- [*]
9
- end_of_line = lf
10
- charset = utf-8
11
- trim_trailing_whitespace = true
12
- insert_final_newline = true
13
- indent_style = space
14
- indent_size = 2
15
-
16
- [*.rb]
17
- indent_style = space
18
- indent_size = 2
19
-
20
- [*.yml]
21
- indent_style = space
22
- indent_size = 2
23
-
24
- [*.js]
25
- indent_style = space
26
- indent_size = 2
27
-
28
- [*.hbs]
29
- indent_style = space
30
- indent_size = 2
31
-
32
- [*.css]
33
- indent_style = space
34
- indent_size = 2
35
-
36
- [*.html]
37
- indent_style = space
38
- indent_size = 2
39
-
40
- [*.md]
41
- trim_trailing_whitespace = false
data/.gitignore DELETED
@@ -1,3 +0,0 @@
1
- spec/internal/db/*sqlite
2
- pkg/
3
- *.swp
data/.rubocop.yml DELETED
@@ -1,8 +0,0 @@
1
- AllCops:
2
- Exclude:
3
- - '**/templates/*'
4
- - '*.gemspec'
5
- LineLength:
6
- Max: 100
7
- Documentation:
8
- Enabled: false
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.5.1
data/.travis.yml DELETED
@@ -1,4 +0,0 @@
1
- language: ruby
2
- script: bundle exec rspec spec
3
- rvm:
4
- - 2.2.2
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gemspec
4
-
5
- group :test do
6
- gem 'actionpack'
7
- gem 'active_model_serializers', '~> 0.10.0'
8
- gem 'combustion', '~> 0.5.2'
9
- gem 'pry'
10
- gem 'rack-test', '~> 0.6.2'
11
- end
data/Gemfile.lock DELETED
@@ -1,153 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- api_me (0.9.4)
5
- active_model_serializers (~> 0.10.0)
6
- activerecord (>= 4.1.16)
7
- activesupport (>= 4.1.16)
8
- kaminari (~> 0.16.3)
9
- pundit (~> 1.1.0)
10
- search_object (~> 1.0)
11
-
12
- GEM
13
- remote: https://rubygems.org/
14
- specs:
15
- actionpack (4.2.10)
16
- actionview (= 4.2.10)
17
- activesupport (= 4.2.10)
18
- rack (~> 1.6)
19
- rack-test (~> 0.6.2)
20
- rails-dom-testing (~> 1.0, >= 1.0.5)
21
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
22
- actionview (4.2.10)
23
- activesupport (= 4.2.10)
24
- builder (~> 3.1)
25
- erubis (~> 2.7.0)
26
- rails-dom-testing (~> 1.0, >= 1.0.5)
27
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
28
- active_model_serializers (0.10.7)
29
- actionpack (>= 4.1, < 6)
30
- activemodel (>= 4.1, < 6)
31
- case_transform (>= 0.2)
32
- jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
33
- activemodel (4.2.10)
34
- activesupport (= 4.2.10)
35
- builder (~> 3.1)
36
- activerecord (4.2.10)
37
- activemodel (= 4.2.10)
38
- activesupport (= 4.2.10)
39
- arel (~> 6.0)
40
- activesupport (4.2.10)
41
- i18n (~> 0.7)
42
- minitest (~> 5.1)
43
- thread_safe (~> 0.3, >= 0.3.4)
44
- tzinfo (~> 1.1)
45
- arel (6.0.4)
46
- ast (2.4.0)
47
- builder (3.2.3)
48
- case_transform (0.2)
49
- activesupport
50
- coderay (1.1.2)
51
- colored (1.2)
52
- combustion (0.5.5)
53
- activesupport (>= 3.0.0)
54
- railties (>= 3.0.0)
55
- thor (>= 0.14.6)
56
- concurrent-ruby (1.0.5)
57
- crass (1.0.4)
58
- diff-lcs (1.3)
59
- erubis (2.7.0)
60
- i18n (0.9.5)
61
- concurrent-ruby (~> 1.0)
62
- jsonapi-renderer (0.2.0)
63
- kaminari (0.16.3)
64
- actionpack (>= 3.0.0)
65
- activesupport (>= 3.0.0)
66
- loofah (2.2.2)
67
- crass (~> 1.0.2)
68
- nokogiri (>= 1.5.9)
69
- method_source (0.9.0)
70
- mini_portile2 (2.3.0)
71
- minitest (5.11.3)
72
- nokogiri (1.8.2)
73
- mini_portile2 (~> 2.3.0)
74
- parallel (1.12.1)
75
- parser (2.5.1.0)
76
- ast (~> 2.4.0)
77
- powerpack (0.1.1)
78
- pry (0.11.3)
79
- coderay (~> 1.1.0)
80
- method_source (~> 0.9.0)
81
- pundit (1.1.0)
82
- activesupport (>= 3.0.0)
83
- rack (1.6.9)
84
- rack-test (0.6.3)
85
- rack (>= 1.0)
86
- rails-deprecated_sanitizer (1.0.3)
87
- activesupport (>= 4.2.0.alpha)
88
- rails-dom-testing (1.0.9)
89
- activesupport (>= 4.2.0, < 5.0)
90
- nokogiri (~> 1.6)
91
- rails-deprecated_sanitizer (>= 1.0.1)
92
- rails-html-sanitizer (1.0.4)
93
- loofah (~> 2.2, >= 2.2.2)
94
- railties (4.2.10)
95
- actionpack (= 4.2.10)
96
- activesupport (= 4.2.10)
97
- rake (>= 0.8.7)
98
- thor (>= 0.18.1, < 2.0)
99
- rainbow (3.0.0)
100
- rake (12.3.1)
101
- rake-notes (0.2.0)
102
- colored
103
- rake
104
- rspec-core (3.7.1)
105
- rspec-support (~> 3.7.0)
106
- rspec-expectations (3.7.0)
107
- diff-lcs (>= 1.2.0, < 2.0)
108
- rspec-support (~> 3.7.0)
109
- rspec-mocks (3.7.0)
110
- diff-lcs (>= 1.2.0, < 2.0)
111
- rspec-support (~> 3.7.0)
112
- rspec-rails (3.7.2)
113
- actionpack (>= 3.0)
114
- activesupport (>= 3.0)
115
- railties (>= 3.0)
116
- rspec-core (~> 3.7.0)
117
- rspec-expectations (~> 3.7.0)
118
- rspec-mocks (~> 3.7.0)
119
- rspec-support (~> 3.7.0)
120
- rspec-support (3.7.1)
121
- rubocop (0.54.0)
122
- parallel (~> 1.10)
123
- parser (>= 2.5)
124
- powerpack (~> 0.1)
125
- rainbow (>= 2.2.2, < 4.0)
126
- ruby-progressbar (~> 1.7)
127
- unicode-display_width (~> 1.0, >= 1.0.1)
128
- ruby-progressbar (1.9.0)
129
- search_object (1.2.1)
130
- sqlite3 (1.3.13)
131
- thor (0.20.0)
132
- thread_safe (0.3.6)
133
- tzinfo (1.2.5)
134
- thread_safe (~> 0.1)
135
- unicode-display_width (1.3.0)
136
-
137
- PLATFORMS
138
- ruby
139
-
140
- DEPENDENCIES
141
- actionpack
142
- active_model_serializers (~> 0.10.0)
143
- api_me!
144
- combustion (~> 0.5.2)
145
- pry
146
- rack-test (~> 0.6.2)
147
- rake-notes (>= 0.2.0)
148
- rspec-rails (~> 3)
149
- rubocop (~> 0.49)
150
- sqlite3 (~> 1.3.7)
151
-
152
- BUNDLED WITH
153
- 1.16.3
data/LICENSE DELETED
@@ -1,20 +0,0 @@
1
- Copyright (c) 2018 Wildland Labs Inc.
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 DELETED
@@ -1,194 +0,0 @@
1
- [![Gem Version](https://badge.fury.io/rb/api_me.png)](http://badge.fury.io/rb/api_me) [![Build Status](https://travis-ci.org/wildland/api_me.svg?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) [![Dependency Status](https://gemnasium.com/inigo-llc/api_me.svg)](https://gemnasium.com/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
- ### Details
11
- Api controllers use the fantastic [Pundit](https://github.com/elabs/pundit) gem for authorization and parameter whitelisting, [Active Model Serializers ver 0.8](https://github.com/rails-api/active_model_serializers/tree/0-8-stable) for resource serialization, and [SearchObject](https://github.com/RStankov/SearchObject) for list filtering. The model, filter, serializer, and policy that the controller uses by default can all be overriden, along with other optional parameters.
12
-
13
- The primary goal of this gem was to keep things simple so that customization is fairly straight forward by separating concerns and providing overrides. Reusing existing libraries was a primary goal during the design, hence the overall simplicity of this gem. We currently use this gem internally at [Inigo](inigo.io) and are committed to its ongoing maintenance.
14
-
15
- ### Upgrade from 0.7.X to 0.8.X
16
- - Upgrade to the latest version of 0.7.X. Run the app/tests and check/fix all deprecations
17
- - Upgrade to the latest version of 0.8.X, update active_model_serializers to 0.10.X.
18
- - Add the following initializer:
19
- `config/initializers/active_model_serializer.rb`
20
- ```rb
21
- ActiveModelSerializers.config.adapter = :json
22
- ```
23
- - Remove all `embed ...` methods from the serializers
24
- - Update all belongs-to relationships to have FKs, serialize the FK id in all active model serializers instead of embeding the relationship (I.E. `has_one :foo` becomes `attributes :foo_id`)
25
- - Update all has-many relationships to serialize the ids instead of embeding the relationship. (I.E. `has_many :foos` becomes `attributes :foo_ids`)
26
- - Remove all has-one relationships and manually build a method to serialize the id. Ex:
27
- ```rb
28
- attributes :foo_id
29
-
30
- def foo_id
31
- object.foo ? object.foo.id : nil
32
- end
33
- ```
34
- - Embeded has-one and belongs-to relationships look the same on the serializer, so look at the model to identify the differences.
35
-
36
- ### Installation
37
- Add the gem to your Gemfile: `gem api_me`.
38
-
39
- Run `bundle install` to install it.
40
-
41
- Run `rails generate api_me:install` to install api_me.
42
-
43
- You are now setup!
44
-
45
- ### Usage
46
- `rails generate api_me:resource user organization:belongs_to name:string ...`
47
-
48
- this generates the following:
49
-
50
- * app/controllers/api/v1/users_controller.rb
51
- * app/policies/user_policy.rb
52
- * app/serializers/user_serializer.rb
53
-
54
- and also essentially calls:
55
- * `rails generate model user organization:belongs_to name:string ...`
56
- Which generates the model et al as specified.
57
-
58
- users_controller.rb:
59
- ````rb
60
- class UsersController < ApplicationController
61
- include ApiMe
62
-
63
- end
64
- ````
65
- POST (create) and PUT (update) requests are expected to post parameters to the singular underscored name of the model by default (I.E. `{"user": {"name": "Test"}}` for a user model), but this can be overriden by overriding `def params_klass_symbol`, or more in-depth by overriding `def object_params`. If `def object_params` is overriden, parameters are also expected be whitelisted inside of this method.
66
-
67
- models/user.rb:
68
- ````rb
69
- # Standard Rails generator used
70
- class User < ActiveRecord::Base
71
- belongs_to :organization
72
- end
73
- ````
74
-
75
- policies/user_policy.rb (See [Pundit](https://github.com/elabs/pundit) for details):
76
- ````rb
77
- class UserPolicy < ApplicationPolicy
78
- # Authorizes what parameters will be whitelisted, see [Pundit](https://github.com/elabs/pundit) for details
79
- def permitted_attributes
80
- [:id, :organization_id, :name]
81
- end
82
-
83
- end
84
- ````
85
-
86
- serializers/user_serializer.rb (See [Active Model Serializers ver 0.8](https://github.com/rails-api/active_model_serializers/tree/0-8-stable) for details):
87
- ````rb
88
- class UserSerializer < ActiveModel::Serializer
89
- attributes :id, :name, :organization_id
90
- end
91
- ````
92
-
93
- filters/user_filter.rb (See [SearchObject](https://github.com/RStankov/SearchObject) for details):
94
- ````rb
95
- require 'search_object'
96
-
97
- class UserFilter < ApiMe::BaseFilter
98
- include ::SearchObject.module #required
99
-
100
- # Add custom filter logic here
101
- # Ex:
102
- # option(:search) { |scope, value| scope.where("username LIKE ?", "%#{value}%") }
103
- end
104
- ````
105
- The ApiMe::BaseFilter is called if no filter exists for the resource, by default the base filter provides filtering by ids for convenience. I.E a GET to `/api/v1/users?ids%5B%5D=1&ids%5B%5D=3` would return users filtered by ids of 1 and 3. All other filters are expected by default to be located at `params[:filters]` and not at the base level.
106
-
107
- ### Sorting
108
-
109
- To enable sorting just pass `sort` in your request with `sortCiteria` and `sortReverse`.
110
-
111
- Ember Example
112
- ````js
113
- return this.store.query('some-model', {
114
- filters: {
115
- cool_models_only: true
116
- },
117
- sort: {
118
- criteria: 'createdAt', // Property to sort on.
119
- reverse: false, // True reverses the sort.
120
- }
121
- });
122
- ````
123
-
124
- ### Pagination
125
-
126
- To enable pagination just pass `page` in your request with `size` and `offset`.
127
-
128
- Also see [ember-bootstrap-controls](http://wildland.github.io/ember-bootstrap-controls/?s=Bootstrap%20Controls&ss=Pagination) for a handy control for easy pagination with ember.
129
-
130
- Ember Example
131
- ````js
132
- return this.store.query('some-model', {
133
- filters: {
134
- cool_models_only: true
135
- },
136
- sort: {
137
- criteria: 'createdAt', // Property to sort on.
138
- reverse: false, // True reverses the sort.
139
- },
140
- page: {
141
- size: 50, // Size of a page. Number of results per page.
142
- offset: false, /* The page number you want to see.
143
- Imagine 103 records are available.
144
- If the size of 50 is given as a parameter
145
- then valid inputs to offset would be 1, 2, and 3. */
146
- }
147
- });
148
- ````
149
-
150
-
151
- Your response will contain a meta object with the following data:
152
- ```js
153
- meta: {
154
- size: n // Size input above
155
- offset: m // Offset input above
156
- record_count: i /* The number of records in the page.
157
- In the `offset` example above pages 1 and 2
158
- would have a record_count of 50 while page 3
159
- would have a record count of 3. */
160
- total_records: j // Total number of records accessible
161
- total_pages: k // Total number of pages accessible
162
- }
163
- ```
164
-
165
- ### Overrides
166
- Overriding the default model class, serializer class, filter class, and filter parameter can be done like so:
167
-
168
- users_controller.rb:
169
- ````rb
170
- class UsersController < ApplicationController
171
- include ApiMe
172
-
173
- model FakeUser
174
- serialzier RealUserSerialzier
175
-
176
- def filter_klass
177
- FancyUserFilter
178
- end
179
-
180
- def filter_params
181
- params[:meta][:filters]
182
- end
183
- end
184
- ````
185
-
186
- #### Todo:
187
- - [ ] Add the ability to specify the api controller path (I.E. app/controllers/api/v2)
188
- - [ ] Add the ability to inject the resource route into the routes file in the resource generators
189
-
190
- ## Code Of Conduct
191
- Wildland Open Source [Code Of Conduct](https://github.com/wildland/code-of-conduct)
192
-
193
- ## License
194
- Copyright (c) 2014, Api Me is developed and maintained by Sam Clopton, and is released under the open MIT Licence.