json_api_client 1.5.2 → 1.5.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
  SHA1:
3
- metadata.gz: 3b172a6b2cca062c34df683b1fcbea5a017420a5
4
- data.tar.gz: 129f0173840bafb6982f191f07fa2f815cb5dce1
3
+ metadata.gz: ce135d371a55b1a0114e61714e16f1ba25b27e73
4
+ data.tar.gz: aa8ef8446aba3641569ceced0810cba716cdffbb
5
5
  SHA512:
6
- metadata.gz: c80f324ed56f8de4575ce6feada1576c253f034bf23c6ef1cb0bce3233a49efc4719cfa740bea078134057cc740fde6f9a7cdf883cd5bfc2b2536a44984da0f9
7
- data.tar.gz: 9b8a4f4842c6045772d77757781fb76c33e10704a82754749fb344ec9a4b77f155683d64c3646b6cd2a62457f2a19f2d118eb5028f4b2656188c35da8b2baee3
6
+ metadata.gz: 3db5715ed8267149d9c7fcffd41fc3cd6c8a830585ae6cf80d5208972f2370bce819e4e9f00dbd606d535a7e879255ca423787dd6d1c3500d86d1b91c8a35554
7
+ data.tar.gz: 53a8c4c8481e60322a961e1fd7bc8ee202062b196c7695f501b1dfdacdb4e7c09de45b04efa9bf8d1ce7971f4cd558003270a2e7c4f913f279d88047e764e621
data/README.md CHANGED
@@ -502,6 +502,16 @@ end
502
502
 
503
503
  ```
504
504
 
505
+ ## Contributing
506
+
507
+ Contributions are welcome! Please fork this repo and send a pull request. Your pull request should have:
508
+
509
+ * a description about what's broken or what the desired functionality is
510
+ * a test illustrating the bug or new feature
511
+ * the code to fix the bug
512
+
513
+ Ideally, the PR has 2 commits - the first showing the failed test and the second with the fix - although this is not
514
+ required. The commits will be squashed into master once accepted.
505
515
 
506
516
  ## Changelog
507
517
 
@@ -33,18 +33,27 @@ module JsonApiClient
33
33
  end
34
34
 
35
35
  def source_parameter
36
- source.fetch(:parameter) do
37
- source[:pointer] ?
38
- source[:pointer].split("/").last :
39
- nil
40
- end
36
+ source[:parameter]
41
37
  end
42
38
 
43
39
  def source_pointer
44
- source.fetch(:pointer) do
45
- source[:parameter] ?
46
- "/data/attributes/#{source[:parameter]}" :
47
- nil
40
+ source[:pointer]
41
+ end
42
+
43
+ def error_key
44
+ if source_pointer && source_pointer != "/data"
45
+ source_pointer.split("/").last
46
+ else
47
+ "base"
48
+ end
49
+ end
50
+
51
+ def error_msg
52
+ msg = title || detail || "invalid"
53
+ if source_parameter
54
+ "#{source_parameter} #{msg}"
55
+ else
56
+ msg
48
57
  end
49
58
  end
50
59
 
@@ -74,7 +83,7 @@ module JsonApiClient
74
83
 
75
84
  def [](source)
76
85
  map do |error|
77
- error.source_parameter == source
86
+ error.error_key == source
78
87
  end
79
88
  end
80
89
 
@@ -2,10 +2,25 @@ module JsonApiClient
2
2
  module Middleware
3
3
  class JsonRequest < Faraday::Middleware
4
4
  def call(environment)
5
+ accept_header = update_accept_header(environment[:request_headers])
6
+
5
7
  environment[:request_headers]["Content-Type"] = 'application/vnd.api+json'
6
- environment[:request_headers]["Accept"] = 'application/vnd.api+json'
8
+ environment[:request_headers]["Accept"] = accept_header
7
9
  @app.call(environment)
8
10
  end
11
+
12
+ private
13
+
14
+ def update_accept_header(headers)
15
+ return 'application/vnd.api+json' if headers["Accept"].nil?
16
+ accept_params = headers["Accept"].split(",")
17
+
18
+ unless accept_params.include?('application/vnd.api+json')
19
+ accept_params.unshift('application/vnd.api+json')
20
+ end
21
+
22
+ accept_params.join(",")
23
+ end
9
24
  end
10
25
  end
11
26
  end
@@ -47,12 +47,12 @@ module JsonApiClient
47
47
  end
48
48
 
49
49
  def page(number)
50
- @pagination_params[:number] = number
50
+ @pagination_params[ klass.paginator.page_param ] = number
51
51
  self
52
52
  end
53
53
 
54
54
  def per(size)
55
- @pagination_params[:size] = size
55
+ @pagination_params[ klass.paginator.per_page_param ] = size
56
56
  self
57
57
  end
58
58
 
@@ -167,7 +167,7 @@ module JsonApiClient
167
167
  end
168
168
 
169
169
  # Default attributes that every instance of this resource should be
170
- # intialized with. Optionally, override this method in a subclass.
170
+ # initialized with. Optionally, override this method in a subclass.
171
171
  #
172
172
  # @return [Hash] Default attributes
173
173
  def default_attributes
@@ -272,7 +272,7 @@ module JsonApiClient
272
272
 
273
273
  def _set_prefix_path(attrs)
274
274
  paths = _belongs_to_associations.map do |a|
275
- a.set_prefix_path(attrs, route_formatter)
275
+ a.set_prefix_path(attrs, route_formatter)
276
276
  end
277
277
 
278
278
  paths.join("/")
@@ -305,7 +305,7 @@ module JsonApiClient
305
305
  @persisted = nil
306
306
  self.links = self.class.linker.new(params.delete("links") || {})
307
307
  self.relationships = self.class.relationship_linker.new(self.class, params.delete("relationships") || {})
308
- self.attributes = params.merge(self.class.default_attributes)
308
+ self.attributes = self.class.default_attributes.merge(params)
309
309
 
310
310
  self.class.schema.each_property do |property|
311
311
  attributes[property.name] = property.default unless attributes.has_key?(property.name) || property.default.nil?
@@ -410,13 +410,7 @@ module JsonApiClient
410
410
  end
411
411
 
412
412
  if last_result_set.has_errors?
413
- last_result_set.errors.each do |error|
414
- if error.source_parameter
415
- errors.add(self.class.key_formatter.unformat(error.source_parameter), error.title || error.detail)
416
- else
417
- errors.add(:base, error.title || error.detail)
418
- end
419
- end
413
+ fill_errors
420
414
  false
421
415
  else
422
416
  self.errors.clear if self.errors
@@ -436,11 +430,12 @@ module JsonApiClient
436
430
  # @return [Boolean] Whether or not the destroy succeeded
437
431
  def destroy
438
432
  self.last_result_set = self.class.requestor.destroy(self)
439
- if !last_result_set.has_errors?
433
+ if last_result_set.has_errors?
434
+ fill_errors
435
+ false
436
+ else
440
437
  self.attributes.clear
441
438
  true
442
- else
443
- false
444
439
  end
445
440
  end
446
441
 
@@ -504,5 +499,12 @@ module JsonApiClient
504
499
  def relationships_for_serialization
505
500
  relationships.as_json_api
506
501
  end
502
+
503
+ def fill_errors
504
+ last_result_set.errors.each do |error|
505
+ key = self.class.key_formatter.unformat(error.error_key)
506
+ errors.add(key, error.error_msg)
507
+ end
508
+ end
507
509
  end
508
510
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.5.2"
2
+ VERSION = "1.5.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.5.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-26 00:00:00.000000000 Z
11
+ date: 2017-11-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -177,7 +177,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  requirements: []
179
179
  rubyforge_project:
180
- rubygems_version: 2.5.2
180
+ rubygems_version: 2.6.8
181
181
  signing_key:
182
182
  specification_version: 4
183
183
  summary: Build client libraries compliant with specification defined by jsonapi.org