json-pie 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +52 -23
- data/lib/json/pie/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8bc688a815ec0c446b67a8f9a05bb742ff0ae19e9f3207b9241630b9d13acbd9
|
4
|
+
data.tar.gz: ee0357b235fabfdc886ad9821cd8d4d096a59d0016b4acbc6cfc32c88ca8bf96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c9f9617599a2ff972f648d986436bbc645a605fc5e2ac396ffadb4527cb189ac5a0af0dae1c10cafbcab69424c2f951e0e9a204b9823bdd90eaea68dee56e840
|
7
|
+
data.tar.gz: 471dd338d3caa975998bad5d77f03ee4c474801126825f13fb72a876d40e974583b9bba0dd9695c6d5cb96cd1533a3756aaa082e39bb8de287854d27ef6c288d
|
data/README.md
CHANGED
@@ -1,39 +1,68 @@
|
|
1
|
-
#
|
1
|
+
# JSON::Pie
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Parse JSON:API data structures into Rails ActiveRecord structures.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
```ruby
|
12
|
-
gem 'json-pie'
|
7
|
+
```
|
8
|
+
gem "json-pie"
|
13
9
|
```
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
$ bundle install
|
18
|
-
|
19
|
-
Or install it yourself as:
|
11
|
+
## Usage
|
20
12
|
|
21
|
-
|
13
|
+
Your models:
|
22
14
|
|
23
|
-
|
15
|
+
```ruby
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
has_many :articles
|
18
|
+
end
|
24
19
|
|
25
|
-
|
20
|
+
class Article < ActiveRecord::Base
|
21
|
+
belongs_to :user
|
22
|
+
end
|
23
|
+
```
|
26
24
|
|
27
|
-
|
25
|
+
In your controller:
|
28
26
|
|
29
|
-
|
27
|
+
```ruby
|
28
|
+
class ArticleController
|
29
|
+
def create
|
30
|
+
article = JSON::Pie.parse params
|
31
|
+
article.save!
|
32
|
+
render json: article, status: :created
|
33
|
+
end
|
34
|
+
end
|
35
|
+
```
|
30
36
|
|
31
|
-
|
37
|
+
Then send this JSON structure to your application will create a new article with `#<User @id=1 ...>` as the author.
|
38
|
+
|
39
|
+
```json
|
40
|
+
{
|
41
|
+
"data": {
|
42
|
+
"type": "article",
|
43
|
+
"attributes": {
|
44
|
+
"title": "New article"
|
45
|
+
},
|
46
|
+
"relationships": {
|
47
|
+
"user": {
|
48
|
+
"data": {
|
49
|
+
"type": "user",
|
50
|
+
"id": 1
|
51
|
+
}
|
52
|
+
}
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
```
|
32
57
|
|
33
|
-
|
58
|
+
### Decoupling from your models
|
34
59
|
|
35
|
-
|
60
|
+
It's fairly easy to decouple the publis JSON:API that you parse from the actual data structure beneath by using the options.
|
36
61
|
|
37
|
-
|
62
|
+
```ruby
|
63
|
+
JSON::Pie.parse(params, **options)
|
64
|
+
```
|
38
65
|
|
39
|
-
|
66
|
+
| option | description |
|
67
|
+
| ------ | ----------- |
|
68
|
+
| `type_map` | A hash that maps JSON:API types to the actual models in your system. E.g. `{ author: :user }` |
|
data/lib/json/pie/version.rb
CHANGED