restful-jsonapi 0.0.9 → 0.0.10

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
  SHA1:
3
- metadata.gz: c3d6d2a1c2d7dc8e74603ec864e2d8286b24bdbb
4
- data.tar.gz: 61319a7798e649dae4cf91fa6803ccfa786113fb
3
+ metadata.gz: f35fb3c43cd68b559563b31892b4a3929b5b7326
4
+ data.tar.gz: 1db27c7d5d17955a03c3eece8f779a2788d63e73
5
5
  SHA512:
6
- metadata.gz: 19f5f3e953ce50450ae691e9742ad7caf26a3eec2dffcf4388dc3986e88c466359f94e1516fea70ffebc83c8db504f83d69ecd689977aec53f9ead6d385d594b
7
- data.tar.gz: fd69d2b95cd4c6d2fcb30eace925d6ce97dd8415e202d2eae2701d1a5b035235494acc32ef943d2e4d4dbc373dcc5cb9f2218e90970efae261252dcba04ef410
6
+ metadata.gz: 8cc05db93720be70a6e664cf6318de16dd62feb71812cfee5366206ac7ad24a529860be1f3a63c8d181b7f606cd42ad1cb51a8ade0d1c792bdc0e86043685a11
7
+ data.tar.gz: 1a3f64688c9b18cfb3ef8a109b36a8eb14a45ced91c9666fc4cbb1c1cfd8c5e39b6c8cb1486503f0adf5836c8ad1196166b00575cff47306181922499e0bd618
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  A temporary monkeypatch for JSONAPI support, both in request payload, and serializing the type without a namespace.
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/restful-jsonapi.svg)](http://badge.fury.io/rb/restful-jsonapi)
6
+
7
+
5
8
  ## Installation
6
9
 
7
10
  Add this line to your application's Gemfile:
@@ -4,51 +4,64 @@ module Restful
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  def restify_param(param_key)
7
- ActionController::Parameters.new(param_key => restify_value(param_key))
7
+ ActionController::Parameters.new(param_key => restify_data(param_key))
8
8
  end
9
9
 
10
10
  private
11
11
 
12
- def restify_value(param_key, value = params)
12
+ def restify_data(param_key, value = params)
13
13
  if value == params
14
- value = params.clone[:data]
14
+ value = params.clone[:data] # leave params alone
15
15
  end
16
- if value.delete(:type).underscore == param_key.to_s.pluralize
17
- new_params = ActionController::Parameters.new
18
- attributes = value.has_key?(:attributes) ? value[:attributes] : value
19
- attributes.merge!(id: value[:id]) if value[:id]
20
- attributes.transform_keys!(&:underscore)
21
- new_params.merge!(attributes)
22
- if value.has_key? :relationships
23
- value[:relationships].each do |k,v|
24
- if k.pluralize != k
25
-
26
- # belongs to relationship
27
- unless v[:data].nil?
28
- new_params["#{k}_id"] = v[:data][:id]
29
- else
30
- new_params["#{k}_id"] = nil
31
- end
16
+ value.delete(:type)
17
+ new_params = ActionController::Parameters.new
18
+ # relationships
19
+ if value.has_key? :relationships
20
+ value.delete(:relationships).each do |relationship_name, relationship_data|
21
+ new_data = restify_relationship(relationship_name, relationship_data)
22
+ new_params.merge! new_data if new_data.present?
23
+ end
24
+ end
25
+ # attributes
26
+ attributes = value.has_key?(:attributes) ? value[:attributes] : value
27
+ attributes.merge!(id: value[:id]) if value[:id]
28
+ attributes.transform_keys!(&:underscore)
29
+ new_params.merge!(attributes)
30
+ new_params
31
+ end
32
32
 
33
- # has many relationship
34
- elsif v[:data].present?
35
- if v[:data].reject{|data|data.has_key? :attributes}.blank?
36
- relationship_key = k.to_s.underscore+"_attributes"
37
- new_params[relationship_key] ||= []
38
- v[:data].each do |vv|
39
- new_params[relationship_key].push restify_value(k,vv)
40
- end unless v[:data].nil?
41
- else
42
- relationship_key = k.to_s.singularize.underscore+"_ids"
43
- new_params[relationship_key] ||= []
44
- v[:data].each do |vv|
45
- new_params[relationship_key].push vv[:id]
46
- end unless v[:data].nil?
47
- end
48
- end
49
- end
33
+ def restify_relationship(relationship_name, relationship_data)
34
+ if relationship_data[:data].present?
35
+ if relationship_data[:data].is_a? Hash
36
+ restify_belongs_to(relationship_name, relationship_data)
37
+ else
38
+ restify_has_many(relationship_name, relationship_data)
50
39
  end
51
- new_params
40
+ end
41
+ end
42
+
43
+ def restify_belongs_to(relationship_name, relationship_data)
44
+ if relationship_data[:data].has_key? :attributes
45
+ relationship_key = relationship_name.to_s.underscore+"_attributes"
46
+ {relationship_key => restify_data(relationship_name,relationship_data[:data])}
47
+ else
48
+ {"#{relationship_name}_id" => relationship_data[:data][:id]}
49
+ end
50
+ end
51
+
52
+ def restify_has_many(relationship_name, relationship_data)
53
+ if relationship_data[:data].select(&:attributes).blank?
54
+ relationship_key = relationship_name.to_s.singularize.underscore+"_ids"
55
+ new_params[relationship_key] ||= []
56
+ relationship_data[:data].each do |vv|
57
+ new_params[relationship_key].push vv[:id]
58
+ end unless relationship_data[:data].nil?
59
+ else
60
+ relationship_key = relationship_name.to_s.underscore+"_attributes"
61
+ new_params[relationship_key] ||= []
62
+ relationship_data[:data].each do |vv|
63
+ new_params[relationship_key].push restify_data(relationship_name,vv[:data])
64
+ end unless relationship_data[:data].nil?
52
65
  end
53
66
  end
54
67
 
@@ -1,5 +1,5 @@
1
1
  module Restful
2
2
  module Jsonapi
3
- VERSION = "0.0.9"
3
+ VERSION = "0.0.10"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful-jsonapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Wentz
@@ -56,7 +56,6 @@ files:
56
56
  - lib/restful/jsonapi/restify_param.rb
57
57
  - lib/restful/jsonapi/serializable_errors.rb
58
58
  - lib/restful/jsonapi/version.rb
59
- - restful-jsonapi-0.0.8.gem
60
59
  - restful-jsonapi.gemspec
61
60
  homepage: https://github.com/Netflix/restful-jsonapi
62
61
  licenses:
Binary file