Skip to content

Commit 46708bf

Browse files
committed
Merge pull request #1 from spasupul/master
Updated documentation
2 parents 60fe884 + a14417d commit 46708bf

File tree

1 file changed

+105
-22
lines changed

1 file changed

+105
-22
lines changed

README.md

Lines changed: 105 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,124 @@
1-
# JsonapiCompliable
1+
[official documentation](https://bbgithub.dev.bloomberg.com/pages/InfrastructureExperience/jsonapi_compliable)
22

3-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsonapi_compliable`. To experiment with that code, run `bin/console` for an interactive prompt.
3+
### Getting Started
4+
Please read below documentation related to [jsonapi](http://jsonapi.org/format/#document-resource-objects)
45

5-
TODO: Delete this and the text above, and describe your gem
66

7-
## Installation
8-
9-
Add this line to your application's Gemfile:
10-
11-
```ruby
12-
gem 'jsonapi_compliable'
7+
### Adding JSONAPICompliable
138
```
9+
class ApplicationController < ActionController::Base
10+
include JSONAPICompliable
11+
end
1412
15-
And then execute:
13+
```
14+
### Define whitelist includes
15+
```
16+
class AuthorsController < ApplicationController
17+
jsonapi do
18+
includes whitelist: { index: [{ books: :genre }, :state] }
1619
17-
$ bundle
20+
allow_filter :last_name
1821
19-
Or install it yourself as:
22+
allow_filter :first_name, aliases: [:title], if: :can_filter_first_name?
2023
21-
$ gem install jsonapi_compliable
24+
allow_filter :first_name_prefix do |scope, filter|
25+
scope.where('first_name like ?', "#{filter}%")
26+
end
27+
end
28+
end
29+
```
30+
Below url requesting state/foo as includes.
31+
But foo include is ignored as it is not whitelist.
32+
>authors?include=state,foo
2233
23-
## Usage
34+
### Defining filter field
2435

25-
TODO: Write usage instructions here
36+
```
37+
class AuthorsController < ApplicationController
38+
jsonapi do
39+
allow_filter :last_name
40+
end
41+
end
42+
```
43+
will allow us make request as below
44+
>/authors?filter[first_name]=john
2645
27-
## Development
46+
### Defining filter field alias
2847

29-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48+
```
49+
class AuthorsController < ApplicationController
50+
jsonapi do
51+
allow_filter :last_name, aliases: [:name]
52+
end
53+
end
54+
```
55+
will allow us make request as below
56+
>/authors?filter[name]=john
3057
31-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58+
### Adding guard to filter
3259

33-
## Contributing
60+
```
61+
class AuthorsController < ApplicationController
62+
jsonapi do
63+
allow_filter :last_name if: :can_filter?
64+
end
65+
66+
def can_filter?
67+
true
68+
end
69+
end
70+
```
3471

35-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jsonapi_compliable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
72+
### Defining default filter
3673

74+
```
75+
class AuthorsController < ApplicationController
76+
jsonapi do
77+
default_filter :first_name do |scope|
78+
scope.where(first_name: 'Willaim')
79+
end
80+
end
81+
end
82+
```
83+
if no filter provided below request will filter authors first_name='Willaim'
3784

38-
## License
85+
>/authors
3986
40-
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
87+
### Deserialize requests params which are coming jsonapi format
4188

89+
```
90+
class AuthorsController < ApplicationController
91+
before_action :deserialize_jsonapi!, only: [:update, :create]
92+
end
93+
```
94+
incoming parameters
95+
```
96+
{
97+
data: {
98+
type: 'authors',
99+
attributes: {
100+
first_name: 'Stephen',
101+
last_name: 'King'
102+
},
103+
relationships: {
104+
books: {
105+
data: [
106+
{ type: 'books', attributes: { title: 'The Shining' } }
107+
]
108+
}
109+
}
110+
}
111+
}
112+
```
113+
will be deserialized to
114+
```
115+
{
116+
authors: {
117+
first_name: 'Stephen',
118+
last_name: 'King'
119+
books_attributes: [{
120+
title: 'The Shingin'
121+
}]
122+
}
123+
}
124+
```

0 commit comments

Comments
 (0)