lib/shamu/json_api/response.rb in shamu-0.0.3 vs lib/shamu/json_api/response.rb in shamu-0.0.4

- old
+ new

@@ -4,90 +4,109 @@ # Build a JSON API response from one or more resources. class Response < BaseBuilder # Output a single resource as the response data. # @param [Object] resource to write. - # @param [Serializer] serializer used to write the resource state. + # @param [Presenter] presenter used to write the resource state. # @yield (builder) # @yieldparam [ResourceBuilder] builder used write the resources fields # and meta. # - # @return [void] - def resource( resource, serializer = nil, &block ) - output[:data] = build_resource( resource, serializer, &block ) + # @return [self] + def resource( resource, presenter = nil, &block ) + output[:data] = build_resource( resource, presenter, &block ) + self end - # Output a single resource as the response data. + # Output a multiple resources as the response data. # # @param [Enumerable<Object>] resources to write. - # @param [Serializer] serializer used to write the resource state. + # @param [Presenter] presenter used to write the resource state. # @yield (builder, resource) # @yieldparam [ResourceBuilder] builder used write the resources fields # and meta. # @yieldparam [Object] resource being written. - # @return [void] - def resources( collection, serializer = nil, &block ) + # @return [self] + def collection( collection, presenter = nil, &block ) output[:data] = collection.map do |resource| - build_resource resource, serializer, &block + build_resource resource, presenter, &block end + self end # @overload error( exception, http_status = nil ) # @param (see ErrorBuilder#exception) # @overload error( &block ) # @yield (builder) # @yieldparam [ErrorBuilder] builder used to describe the error. # - # @return [void] + # @return [self] def error( exception = nil, http_status = nil, &block ) builder = ErrorBuilder.new if block_given? yield builder - else + elsif exception.is_a?( Exception ) builder.exception( exception, http_status ) + else + http_status ||= 500 + builder.summary http_status, http_status.to_s, exception end errors = ( output[:errors] ||= [] ) errors << builder.compile + + self end # (see BaseBuilder#compile) def compile @compiled ||= begin compiled = output.dup compiled[:jsonapi] = { version: "1.0" } + if params_meta = context.params_meta + compiled[:meta] ||= {} + compiled[:meta].reverse_merge!( params_meta ) + end while context.included_resources? included = ( compiled[ :included ] ||= [] ) context.collect_included_resources.each do |resource, options| - included << build_resource( resource, options[:serializer], &options[:block] ) + included << build_resource( resource, options[:presenter], &options[:block] ) end end compiled end end - def to_json + # @return [Hash] the compiled resources. + def as_json( * ) + compile.as_json + end + + # @return [String] + def to_json( * ) compile.to_json end + # @return [String] def to_s - compile.to_s + to_json end - private :identifier + # Responses don't have identifiers + undef :identifier private - def build_resource( resource, serializer, &block ) - fail "A block is required if no serializer is given" if !serializer && !block_given? + def build_resource( resource, presenter, &block ) + presenter = context.find_presenter( resource ) if !presenter && !block_given? builder = ResourceBuilder.new( context ) - if serializer - serializer.serialize( builder ) + if presenter + presenter.present( resource, builder ) else yield builder end builder.compile \ No newline at end of file