json_apiable 0.2.1 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2730ce86ac44faba4e0e0259559e346efe33442f474f9ba715aa2601045a02ac
4
- data.tar.gz: f98a96c78028431660ac200e568b857133d581618594277a25829135a1a40cb1
3
+ metadata.gz: 1d67de4927cb04df83deea484a0890bdfdc36cbf48d6bda986000ffabcda8f3b
4
+ data.tar.gz: a90a940bc16d451c698355f1e18c74a5e183135ed3315f7aa272ad9a37e345f5
5
5
  SHA512:
6
- metadata.gz: 9646248670873df706251afa484cfc14cff8ac9f8fd3e3eea7465659b52fac3a1f4ffa5b5312ed3d4c570e3178d05354bb1d7991eaf7dbac3e27861bd510ec9b
7
- data.tar.gz: 872d84af6b05e4396be7aece31fc8fff11c34690dc2f17cb5908b0d2c08d9cf9a2f06bc811e9de0e2b9a78e7edb9d12dd5c2a96365c46d95d1cb84bb5f5f1800
6
+ metadata.gz: ee5e99fac5548996d4222e3b281ca8d3d03c78e95b57414c08b8f6f624497140c1eff43d0f5e5d54a3c2c2fec9501e92b09d37deb2e5c688c132161ca88f031c
7
+ data.tar.gz: 8d36927df5f8684188cc4e6f4ef263640429ddff9a84f9824860fe1ff1519c94fdb1b90642291681d5612478e9913ca818539a1f2c8d642657dba2d24d0f6a48
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_apiable (0.2.1)
4
+ json_apiable (0.3)
5
5
  activerecord (>= 4.2)
6
6
  activesupport (>= 4.2)
7
7
  fast_jsonapi (~> 1.5)
data/README.md CHANGED
@@ -103,8 +103,8 @@ class API::PostsController < API::BaseController
103
103
  def create
104
104
  # use jsonapi_attribute_present? to quickly test presence of specific attributes
105
105
  raise UnprocessableEntityError, 'No title!' unless jsonapi_attribute_present?(:title)
106
- # use jsonapi_attribute_value to get attribute values. If non-existent, nil would be returned
107
- @title = jsonapi_attribute_value(:title)
106
+ # use jsonapi_attribute to get attribute values. If non-existent, nil would be returned
107
+ @title = jsonapi_attribute(:title)
108
108
  # exclude 'author' attribute from assign params, for example because it's a separate table on the DB level)
109
109
  @author_name = jsonapi_exclude_attribute(:author_name)
110
110
  # exclude 'comments' relationship from assign params, for example because we want to filter which ones are added to post
@@ -42,12 +42,29 @@ module JsonApiable
42
42
  yield(configuration)
43
43
  end
44
44
 
45
+ def jsonapi_attribute(attrib_key)
46
+ jsonapi_build_params.dig(:data, :attributes, attrib_key)
47
+ end
48
+
45
49
  def jsonapi_attribute_present?(attrib_key)
46
- jsonapi_build_params.dig(:data, :attributes, attrib_key).present?
50
+ jsonapi_attribute(attrib_key).present?
47
51
  end
48
52
 
49
- def jsonapi_attribute_value(attrib_key)
50
- jsonapi_build_params.dig(:data, :attributes, attrib_key)
53
+ def jsonapi_relationship(attrib_key)
54
+ jsonapi_build_params.dig(:data, :relationships, attrib_key)
55
+ end
56
+
57
+ def jsonapi_relationship_data(attrib_key)
58
+ jsonapi_build_params.dig(:data, :relationships, attrib_key, :data)
59
+ end
60
+
61
+ def jsonapi_relationship_present?(attrib_key)
62
+ jsonapi_relationship(attrib_key).present?
63
+ end
64
+
65
+ def jsonapi_relationship_attribute(relationship, attribute)
66
+ [:id, :type].include?(attribute.to_sym) ? jsonapi_relationship_data(relationship)&.dig(attribute) :
67
+ jsonapi_relationship_data(relationship)&.dig(:attributes, attribute)
51
68
  end
52
69
 
53
70
  def jsonapi_assign_params
@@ -71,7 +88,8 @@ module JsonApiable
71
88
  jsonapi_build_params.dig(:data, :relationships, rel_key)
72
89
  end
73
90
 
74
- # Should be overwritten in specific controllers
91
+ # Should be overwritten in specific controllers. If you need to manipulate params before they are parsed,
92
+ # that's the place to do it
75
93
  def jsonapi_build_params
76
94
  params
77
95
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiable
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_apiable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Polischuk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-18 00:00:00.000000000 Z
11
+ date: 2020-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord