json-pie 0.0.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +7 -3
- data/Gemfile.lock +1 -1
- data/README.md +54 -22
- data/lib/json/pie/resource_identity.rb +1 -2
- data/lib/json/pie/resource_object.rb +8 -1
- data/lib/json/pie/version.rb +1 -1
- metadata +7 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ab34b7f1268256832d6e98ca5fb7961696c6fd7263355d84282dda185a0e4744
|
|
4
|
+
data.tar.gz: 3131e5fb56bfd504a6eabb34da5c8976227429cec6563e1c7002eff297471cfd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 878597c4caf0040464f44cb9eff1ce193d7ebdeaf00f79b727e981fb2c98d4b9a528e913bf6911560a1a5481686bb7464f1ec2f92be371b7600cc6cc46fe0825
|
|
7
|
+
data.tar.gz: c380f9cdbaa22f8fc524e2e17f733d27fbd05d340c8ecc485816b67a575ba113eb4607fc857180ad070d6816ab11e13a8a6259c26158d242fb767b1a692e69bc
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
## [
|
|
1
|
+
## [unreleased]
|
|
2
2
|
|
|
3
|
-
## [0.
|
|
3
|
+
## [0.0.4] - 2022-03-03
|
|
4
4
|
|
|
5
|
-
-
|
|
5
|
+
- Imporved public description of the gem
|
|
6
|
+
|
|
7
|
+
## [0.0.3] - 2022-03-03
|
|
8
|
+
|
|
9
|
+
- Allow data structure to define resource ID when creating a new resource.
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -1,39 +1,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# JSON::Pie
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Parse JSON:API data structures into Rails ActiveRecord structures.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+

|
|
6
|
+

|
|
6
7
|
|
|
7
8
|
## Installation
|
|
8
9
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
```ruby
|
|
12
|
-
gem 'json-pie'
|
|
10
|
+
```
|
|
11
|
+
gem "json-pie"
|
|
13
12
|
```
|
|
14
13
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
$ bundle install
|
|
18
|
-
|
|
19
|
-
Or install it yourself as:
|
|
14
|
+
## Usage
|
|
20
15
|
|
|
21
|
-
|
|
16
|
+
Your models:
|
|
22
17
|
|
|
23
|
-
|
|
18
|
+
```ruby
|
|
19
|
+
class User < ActiveRecord::Base
|
|
20
|
+
has_many :articles
|
|
21
|
+
end
|
|
24
22
|
|
|
25
|
-
|
|
23
|
+
class Article < ActiveRecord::Base
|
|
24
|
+
belongs_to :user
|
|
25
|
+
end
|
|
26
|
+
```
|
|
26
27
|
|
|
27
|
-
|
|
28
|
+
In your controller:
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
```ruby
|
|
31
|
+
class ArticleController
|
|
32
|
+
def create
|
|
33
|
+
article = JSON::Pie.parse params
|
|
34
|
+
article.save!
|
|
35
|
+
render json: article, status: :created
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
```
|
|
30
39
|
|
|
31
|
-
|
|
40
|
+
Then send this JSON structure to your application will create a new article with `#<User @id=1 ...>` as the author.
|
|
41
|
+
|
|
42
|
+
```json
|
|
43
|
+
{
|
|
44
|
+
"data": {
|
|
45
|
+
"type": "article",
|
|
46
|
+
"attributes": {
|
|
47
|
+
"title": "New article"
|
|
48
|
+
},
|
|
49
|
+
"relationships": {
|
|
50
|
+
"user": {
|
|
51
|
+
"data": {
|
|
52
|
+
"type": "user",
|
|
53
|
+
"id": 1
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
```
|
|
32
60
|
|
|
33
|
-
|
|
61
|
+
### Decoupling from your models
|
|
34
62
|
|
|
35
|
-
|
|
63
|
+
It's fairly easy to decouple the publis JSON:API that you parse from the actual data structure beneath by using the options.
|
|
36
64
|
|
|
37
|
-
|
|
65
|
+
```ruby
|
|
66
|
+
JSON::Pie.parse(params, **options)
|
|
67
|
+
```
|
|
38
68
|
|
|
39
|
-
|
|
69
|
+
| option | description |
|
|
70
|
+
| ------ | ----------- |
|
|
71
|
+
| `type_map` | A hash that maps JSON:API types to the actual models in your system. E.g. `{ author: :user }` |
|
|
@@ -14,9 +14,8 @@ module JSON
|
|
|
14
14
|
def initialize(type:, id: nil, **options)
|
|
15
15
|
@options = options
|
|
16
16
|
klass = determine_type(type).to_s.classify.constantize
|
|
17
|
-
@instance =
|
|
17
|
+
@instance = klass.find_or_initialize_by klass.primary_key => id
|
|
18
18
|
rescue NameError
|
|
19
|
-
pp options
|
|
20
19
|
raise JSON::Pie::InvalidType, "#{type}(#{id})"
|
|
21
20
|
end
|
|
22
21
|
|
|
@@ -41,7 +41,7 @@ module JSON
|
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
def assign_attributes!
|
|
44
|
-
instance.attributes =
|
|
44
|
+
instance.attributes = attributes_tuple.to_h
|
|
45
45
|
rescue ActiveModel::UnknownAttributeError
|
|
46
46
|
raise JSON::Pie::InvalidAttribute
|
|
47
47
|
end
|
|
@@ -51,6 +51,13 @@ module JSON
|
|
|
51
51
|
instance: instance,
|
|
52
52
|
relationships: data.fetch(:relationships, {})
|
|
53
53
|
end
|
|
54
|
+
|
|
55
|
+
def attributes_tuple
|
|
56
|
+
data.fetch(:attributes, {}).collect do |key, value|
|
|
57
|
+
key = options.dig(:attributes_map, data.fetch(:type), key).presence || key
|
|
58
|
+
[key, value]
|
|
59
|
+
end
|
|
60
|
+
end
|
|
54
61
|
end
|
|
55
62
|
end
|
|
56
63
|
end
|
data/lib/json/pie/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json-pie
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Emil Kampp
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-
|
|
11
|
+
date: 2022-03-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -24,7 +24,10 @@ dependencies:
|
|
|
24
24
|
- - "<="
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '7.1'
|
|
27
|
-
description:
|
|
27
|
+
description: |
|
|
28
|
+
Easily parse JSON:API data structures into ActiveRecord resources.
|
|
29
|
+
|
|
30
|
+
This will parse deeply nested relationships as well as attributes.
|
|
28
31
|
email:
|
|
29
32
|
- emil@kampp.me
|
|
30
33
|
executables: []
|
|
@@ -73,5 +76,5 @@ requirements: []
|
|
|
73
76
|
rubygems_version: 3.2.32
|
|
74
77
|
signing_key:
|
|
75
78
|
specification_version: 4
|
|
76
|
-
summary: JSON:API
|
|
79
|
+
summary: Easily parse JSON:API structures into ActiveRecord resources
|
|
77
80
|
test_files: []
|