caprese 0.3.6 → 0.3.7

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: 700367fd0c3a1b6334342c39e957f925c807fb76
4
- data.tar.gz: cb81d72150d9ed56af418d447b72069dd49d09aa
3
+ metadata.gz: 1411527e1ecd7ef00676128c958c74414fb50b30
4
+ data.tar.gz: c96f3f3938d48b99c885107f7670f886d5b81dcb
5
5
  SHA512:
6
- metadata.gz: f7b66a6904741cbea9943b0897582663599efc01100608f8e7138c6b917377ee07300f2dacce5dc3ef24ba839b51560bd2fab33e87192e233df2916c23d5bdee
7
- data.tar.gz: 3146086e5e4627a0326ad1ed380ca18bb90b84f40564d77b8d28ffb1899096bd35d95766518112cda6dcf44a8f433a7c09ec83332bdd0531f3ab086f8d9e2452
6
+ metadata.gz: 9d4417c5faa6901736c30076beaee8ab27db4f0139021515ceb45736516a3fe3823b846d198729b6509e2230b7ed0401a59ab71bb2d6015947f72be1cd506435
7
+ data.tar.gz: d5a26e58f9619aad4e473161bb4653a07c2ee727ebc87ed961a03bd90a39c4fee50d40b9a697d348ea83aae208a029038d5c84ff951cbdad32c7b4956e141a09
data/CHANGELOG.md CHANGED
@@ -31,3 +31,7 @@
31
31
  ## 0.3.6
32
32
 
33
33
  * Only allow resource identifiers in request documents to use `id` for primary key (JSON API adherent) (93c90eb)
34
+
35
+ ## 0.3.7
36
+
37
+ * Completes error source pointer determination to include primary data items and relationship primary data items
@@ -7,6 +7,17 @@ module Caprese
7
7
  # rubocop:disable Style/AsciiComments
8
8
  UnknownSourceTypeError = Class.new(ArgumentError)
9
9
 
10
+ def self.param_errors(error_serializer, options)
11
+ error_attributes = error_serializer.as_json
12
+ [
13
+ {
14
+ code: error_attributes[:code],
15
+ detail: error_attributes[:message],
16
+ source: error_source(:parameter, nil, error_attributes[:field])
17
+ }
18
+ ]
19
+ end
20
+
10
21
  # Builds a JSON API Errors Object
11
22
  # {http://jsonapi.org/format/#errors JSON API Errors}
12
23
  #
@@ -20,20 +31,6 @@ module Caprese
20
31
  end
21
32
  end
22
33
 
23
- # FIXME: param_errors implies multiple errors, but since we use a fail first
24
- # strategy, there will only be one param error ever, as reflected by this method's
25
- # definition
26
- def self.param_errors(error_serializer, options)
27
- error_attributes = error_serializer.as_json
28
- [
29
- {
30
- code: error_attributes[:code],
31
- detail: error_attributes[:message],
32
- source: error_source(:parameter, nil, error_attributes[:field])
33
- }
34
- ]
35
- end
36
-
37
34
  # definition:
38
35
  # JSON Object
39
36
  #
@@ -92,20 +89,32 @@ module Caprese
92
89
  # parameter: 'pres'
93
90
  # }
94
91
  # end
92
+ RESERVED_ATTRIBUTES = %w(type)
95
93
  def self.error_source(source_type, record, attribute_name)
96
94
  case source_type
97
95
  when :pointer
98
- if record.has_attribute?(attribute_name)
96
+ # [type ...] and other primary data variables
97
+ if RESERVED_ATTRIBUTES.include?(attribute_name.to_s)
99
98
  {
100
- pointer: JsonApi::JsonPointer.new(:attribute, record, attribute_name)
99
+ pointer: JsonApi::JsonPointer.new(:primary_data, record, attribute_name)
101
100
  }
102
- elsif attribute_name.to_s.split('.').size > 1
101
+ elsif record.has_attribute?(attribute_name)
103
102
  {
104
- pointer: JsonApi::JsonPointer.new(:relationship_attribute, record, attribute_name)
103
+ pointer: JsonApi::JsonPointer.new(:attribute, record, attribute_name)
105
104
  }
105
+ elsif (relationship_data_items = attribute_name.to_s.split('.')).size > 1
106
+ if RESERVED_ATTRIBUTES.include?(relationship_data_items.last)
107
+ {
108
+ pointer: JsonApi::JsonPointer.new(:relationship_primary_data, record, relationship_data_items)
109
+ }
110
+ else
111
+ {
112
+ pointer: JsonApi::JsonPointer.new(:relationship_attribute, record, attribute_name)
113
+ }
114
+ end
106
115
  else
107
116
  {
108
- pointer: JsonApi::JsonPointer.new(:relationship, record, attribute_name)
117
+ pointer: JsonApi::JsonPointer.new(:relationship_base, record, attribute_name)
109
118
  }
110
119
  end
111
120
  when :parameter
@@ -6,8 +6,10 @@ module Caprese
6
6
 
7
7
  POINTERS = {
8
8
  attribute: '/data/attributes/%s'.freeze,
9
- relationship: '/data/relationships/%s'.freeze,
10
- primary_data: '/data%s'.freeze
9
+ relationship_attribute: '/data/relationships/%s'.freeze,
10
+ relationship_base: '/data/relationships/%s/data'.freeze,
11
+ relationship_primary_data: '/data/relationships/%s/data/%s'.freeze,
12
+ primary_data: '/data/%s'.freeze
11
13
  }.freeze
12
14
 
13
15
  # Iterates over the field of an error and converts it to a pointer in JSON API format
@@ -25,6 +27,8 @@ module Caprese
25
27
  # => '/data/relationships/post/data/relationships/user/data/attributes/name'
26
28
  #
27
29
  # @param [Symbol] pointer_type the type of pointer: :attribute, :relationship, :primary_data
30
+ # @param [Record] the record that owns the errors
31
+ # @param [Object,Array<Object>]
28
32
  def new(pointer_type, record, value)
29
33
  if pointer_type == :relationship_attribute
30
34
  value.to_s.split('.').inject('') do |pointer, v|
@@ -37,13 +41,13 @@ module Caprese
37
41
  record.send(v)
38
42
  end
39
43
 
40
- format(POINTERS[:relationship], v)
44
+ format(POINTERS[:relationship_attribute], v)
41
45
  else
42
46
  format(POINTERS[:attribute], v)
43
47
  end
44
48
  end
45
49
  else
46
- format(POINTERS[pointer_type], value)
50
+ format(POINTERS[pointer_type], *[value].flatten)
47
51
  end
48
52
  end
49
53
  end
@@ -292,7 +292,7 @@ module Caprese
292
292
  end
293
293
  else
294
294
  # { id: '...' } && { attributes: { ... } }
295
- owner.errors.add(relationship_name)
295
+ owner.errors.add("#{relationship_name}.type")
296
296
  nil
297
297
  end
298
298
  end
@@ -27,12 +27,22 @@ module Caprese
27
27
  end
28
28
 
29
29
  # Checks if a given type mismatches the controller type
30
- # @note Throws :invalid_type error if true
31
30
  #
32
31
  # @param [String] type the pluralized type to check ('products','orders',etc.)
33
32
  def fail_on_type_mismatch(type)
34
- unless record_class(type) == controller_record_class
35
- fail InvalidTypeError.new(type)
33
+ failed = false
34
+
35
+ begin
36
+ failed = record_class(type) != controller_record_class
37
+ rescue NameError
38
+ failed = true
39
+ end
40
+
41
+ if failed
42
+ invalid_typed_record = controller_record_class.new
43
+ invalid_typed_record.errors.add(:type)
44
+
45
+ fail RecordInvalidError.new(invalid_typed_record)
36
46
  end
37
47
  end
38
48
  end
@@ -58,12 +58,4 @@ module Caprese
58
58
  @header = { status: :forbidden }
59
59
  end
60
60
  end
61
-
62
- # Thrown when an attempt was made to create a record of type that is different than the type
63
- # of the controller that the data was sent to
64
- class InvalidTypeError < Error
65
- def initialize(type)
66
- super code: :invalid_type, t: { type: type }
67
- end
68
- end
69
61
  end
@@ -1,3 +1,3 @@
1
1
  module Caprese
2
- VERSION = '0.3.6'
2
+ VERSION = '0.3.7'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caprese
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Landgrebe
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2017-02-08 00:00:00.000000000 Z
13
+ date: 2017-02-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: active_model_serializers