@@ -74,105 +74,22 @@ module Deserialization
7474 # # }
7575 #
7676 def parse! ( document , options = { } )
77- parse ( document , options ) do |invalid_payload , reason |
78- fail JSONAPI :: Parser :: InvalidDocument , "Invalid payload ( #{ reason } ): #{ invalid_payload } "
77+ parse ( document , options ) do |exception |
78+ raise exception
7979 end
8080 end
8181
8282 # Same as parse!, but returns an empty hash instead of raising InvalidDocument
8383 # on invalid payloads.
8484 def parse ( document , options = { } )
85- JSONAPI . parse_response! ( document )
86- document = document . to_h
87- puts document
88- return JSONAPI ::Deserializable ::Resource . new ( document )
89- #
90- # primary_data = document['data']
91- #
92- # # null data is allowed, as per the JSON API Schema
93- # return {} unless primary_data
94- #
95- # attributes = primary_data['attributes'] || {}
96- # attributes['id'] = primary_data['id'] if primary_data['id']
97- # relationships = primary_data['relationships'] || {}
98- #
99- # filter_fields(attributes, options)
100- # filter_fields(relationships, options)
101- #
102- # hash = {}
103- # hash.merge!(parse_attributes(attributes, options))
104- # hash.merge!(parse_relationships(relationships, options))
105- #
106- # hash
85+ # TODO: change to jsonapi-ralis to have default conversion to flat hashes
86+ JSONAPI ::Deserializable ::Resource . new ( document ) . to_h
10787 rescue JSONAPI ::Parser ::InvalidDocument => e
108- puts e . message
10988 return { } unless block_given?
110- yield
111- end
112-
113- # @api private
114- def filter_fields ( fields , options )
115- if ( only = options [ :only ] )
116- fields . slice! ( *Array ( only ) . map ( &:to_s ) )
117- elsif ( except = options [ :except ] )
118- fields . except! ( *Array ( except ) . map ( &:to_s ) )
119- end
120- end
121-
122- # @api private
123- def field_key ( field , options )
124- ( options [ :keys ] || { } ) . fetch ( field . to_sym , field ) . to_sym
125- end
126-
127- # @api private
128- def parse_attributes ( attributes , options )
129- transform_keys ( attributes , options )
130- . map { |( k , v ) | { field_key ( k , options ) => v } }
131- . reduce ( { } , :merge )
132- end
133-
134- # Given an association name, and a relationship data attribute, build a hash
135- # mapping the corresponding ActiveRecord attribute to the corresponding value.
136- #
137- # @example
138- # parse_relationship(:comments, [{ 'id' => '1', 'type' => 'comments' },
139- # { 'id' => '2', 'type' => 'comments' }],
140- # {})
141- # # => { :comment_ids => ['1', '2'] }
142- # parse_relationship(:author, { 'id' => '1', 'type' => 'users' }, {})
143- # # => { :author_id => '1' }
144- # parse_relationship(:author, nil, {})
145- # # => { :author_id => nil }
146- # @param [Symbol] assoc_name
147- # @param [Hash] assoc_data
148- # @param [Hash] options
149- # @return [Hash{Symbol, Object}]
150- #
151- # @api private
152- def parse_relationship ( assoc_name , assoc_data , options )
153- prefix_key = field_key ( assoc_name , options ) . to_s . singularize
154- hash =
155- if assoc_data . is_a? ( Array )
156- { "#{ prefix_key } _ids" . to_sym => assoc_data . map { |ri | ri [ 'id' ] } }
157- else
158- { "#{ prefix_key } _id" . to_sym => assoc_data ? assoc_data [ 'id' ] : nil }
159- end
160-
161- polymorphic = ( options [ :polymorphic ] || [ ] ) . include? ( assoc_name . to_sym )
162- if polymorphic
163- hash [ "#{ prefix_key } _type" . to_sym ] = assoc_data . present? ? assoc_data [ 'type' ] : nil
164- end
165-
166- hash
167- end
168-
169- # @api private
170- def parse_relationships ( relationships , options )
171- transform_keys ( relationships , options )
172- . map { |( k , v ) | parse_relationship ( k , v [ 'data' ] , options ) }
173- . reduce ( { } , :merge )
89+ yield e
17490 end
17591
92+ # TODO: transform the keys after parsing
17693 # @api private
17794 def transform_keys ( hash , options )
17895 transform = options [ :key_transform ] || :underscore
0 commit comments