1+ require_relative "link_builder"
2+
13module JSONAPI
24 class ResourceSerializer
35
@@ -12,16 +14,15 @@ class ResourceSerializer
1214 # key_formatter: KeyFormatter class to override the default configuration
1315 # base_url: a string to prepend to generated resource links
1416
15- def initialize ( primary_resource_klass , options = { } )
16- @primary_resource_klass = primary_resource_klass
17- @primary_class_name = @primary_resource_klass . _type
17+ attr_reader :url_generator
1818
19- @fields = options . fetch ( :fields , { } )
20- @include = options . fetch ( :include , [ ] )
19+ def initialize ( primary_resource_klass , options = { } )
20+ @primary_class_name = primary_resource_klass . _type
21+ @fields = options . fetch ( :fields , { } )
22+ @include = options . fetch ( :include , [ ] )
2123 @include_directives = options [ :include_directives ]
22- @key_formatter = options . fetch ( :key_formatter , JSONAPI . configuration . key_formatter )
23- @route_formatter = options . fetch ( :route_formatter , JSONAPI . configuration . route_formatter )
24- @base_url = options . fetch ( :base_url , '' )
24+ @key_formatter = options . fetch ( :key_formatter , JSONAPI . configuration . key_formatter )
25+ @url_generator = generate_link_builder ( primary_resource_klass , options )
2526 end
2627
2728 # Converts a single resource, or an array of resources to a hash, conforming to the JSONAPI structure
@@ -68,7 +69,7 @@ def serialize_to_links_hash(source, requested_association)
6869 end
6970
7071 def find_link ( query_params )
71- " #{ @base_url } / #{ formatted_module_path_from_klass ( @primary_resource_klass ) } #{ @route_formatter . format ( @primary_resource_klass . _type . to_s ) } ? #{ query_params . to_query } "
72+ url_generator . query_link ( query_params )
7273 end
7374
7475 private
@@ -196,38 +197,22 @@ def relationship_data(source, include_directives)
196197
197198 def relationship_links ( source )
198199 links = { }
199- links [ :self ] = self_href ( source )
200+ links [ :self ] = url_generator . self_link ( source )
200201
201202 links
202203 end
203204
204- def formatted_module_path ( source )
205- formatted_module_path_from_klass ( source . class )
206- end
207-
208- def formatted_module_path_from_klass ( klass )
209- klass . name =~ /::[^:]+\Z / ? ( @route_formatter . format ( $`) . freeze . gsub ( '::' , '/' ) + '/' ) . downcase : ''
210- end
211-
212- def self_href ( source )
213- "#{ @base_url } /#{ formatted_module_path ( source ) } #{ @route_formatter . format ( source . class . _type . to_s ) } /#{ source . id } "
214- end
215-
216205 def already_serialized? ( type , id )
217206 type = format_key ( type )
218207 @included_objects . key? ( type ) && @included_objects [ type ] . key? ( id )
219208 end
220209
221- def format_route ( route )
222- @route_formatter . format ( route . to_s )
223- end
224-
225210 def self_link ( source , association )
226- " #{ self_href ( source ) } /relationships/ #{ format_route ( association . name ) } "
211+ url_generator . relationships_self_link ( source , association )
227212 end
228213
229214 def related_link ( source , association )
230- " #{ self_href ( source ) } / #{ format_route ( association . name ) } "
215+ url_generator . relationships_related_link ( source , association )
231216 end
232217
233218 def has_one_linkage ( source , association )
@@ -327,5 +312,13 @@ def format_value(value, format)
327312 value_formatter = JSONAPI ::ValueFormatter . value_formatter_for ( format )
328313 value_formatter . format ( value )
329314 end
315+
316+ def generate_link_builder ( primary_resource_klass , options )
317+ LinkBuilder . new (
318+ base_url : options . fetch ( :base_url , '' ) ,
319+ route_formatter : options . fetch ( :route_formatter , JSONAPI . configuration . route_formatter ) ,
320+ primary_resource_klass : primary_resource_klass ,
321+ )
322+ end
330323 end
331324end
0 commit comments