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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a63d4d0b3a9e543fbb77724003a4e00b54b4019fc4aa96effde7517ea40967bf
4
- data.tar.gz: a355d4b17fe3ae44c243073d86da4766825d2db1b52d876d7e8fe034be90ecfe
3
+ metadata.gz: ab34b7f1268256832d6e98ca5fb7961696c6fd7263355d84282dda185a0e4744
4
+ data.tar.gz: 3131e5fb56bfd504a6eabb34da5c8976227429cec6563e1c7002eff297471cfd
5
5
  SHA512:
6
- metadata.gz: 0a295acde72a3e8d5b6b5fb2161e3112041948e3979a740782b7b50df0b8a601f189e3f0fbebf11601a6f2096078acccbbf370298a24618952f4b47fe6f8cd44
7
- data.tar.gz: a6fb5a2ee5cb6c2ca9473fc3b2295c313302d58154a7378091168445df000bdf6d7344f3ecb84c75df205e1f9907aba493145ec274eff1966979beb018e88eba
6
+ metadata.gz: 878597c4caf0040464f44cb9eff1ce193d7ebdeaf00f79b727e981fb2c98d4b9a528e913bf6911560a1a5481686bb7464f1ec2f92be371b7600cc6cc46fe0825
7
+ data.tar.gz: c380f9cdbaa22f8fc524e2e17f733d27fbd05d340c8ecc485816b67a575ba113eb4607fc857180ad070d6816ab11e13a8a6259c26158d242fb767b1a692e69bc
data/.rubocop.yml CHANGED
@@ -24,3 +24,4 @@ Metrics/BlockLength:
24
24
  Exclude:
25
25
  - Guardfile
26
26
  - "**/*_spec.rb"
27
+ - json-pie.gemspec
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
- ## [Unreleased]
1
+ ## [unreleased]
2
2
 
3
- ## [0.1.0] - 2022-02-27
3
+ ## [0.0.4] - 2022-03-03
4
4
 
5
- - Initial release
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json-pie (0.0.1)
4
+ json-pie (0.0.3)
5
5
  rails (<= 7.1)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,39 +1,71 @@
1
- # Json::Pie
1
+ # JSON::Pie
2
2
 
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/json/pie`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Parse JSON:API data structures into Rails ActiveRecord structures.
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ![Tests](https://github.com/ekampp/json-pie/actions/workflows/tests.yml/badge.svg)
6
+ ![Linting](https://github.com/ekampp/json-pie/actions/workflows/linting.yml/badge.svg)
6
7
 
7
8
  ## Installation
8
9
 
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'json-pie'
10
+ ```
11
+ gem "json-pie"
13
12
  ```
14
13
 
15
- And then execute:
16
-
17
- $ bundle install
18
-
19
- Or install it yourself as:
14
+ ## Usage
20
15
 
21
- $ gem install json-pie
16
+ Your models:
22
17
 
23
- ## Usage
18
+ ```ruby
19
+ class User < ActiveRecord::Base
20
+ has_many :articles
21
+ end
24
22
 
25
- TODO: Write usage instructions here
23
+ class Article < ActiveRecord::Base
24
+ belongs_to :user
25
+ end
26
+ ```
26
27
 
27
- ## Development
28
+ In your controller:
28
29
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
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
- 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
61
+ ### Decoupling from your models
34
62
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/json-pie.
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
- ## License
65
+ ```ruby
66
+ JSON::Pie.parse(params, **options)
67
+ ```
38
68
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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 = id ? klass.find(id) : klass.new
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 = data.fetch :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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module JSON
4
4
  module Pie
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.4"
6
6
  end
7
7
  end
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.1
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-02-28 00:00:00.000000000 Z
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: Allow a Rails API to use the JSON:API interface
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 rails plugin
79
+ summary: Easily parse JSON:API structures into ActiveRecord resources
77
80
  test_files: []