class Shamu::JsonApi::Response

Build a JSON API response from one or more resources.

Public Instance Methods

as_json( * ) click to toggle source

@return [Hash] the compiled resources.

# File lib/shamu/json_api/response.rb, line 103
def as_json( * )
  compile.as_json
end
collection( collection, presenter = nil, &block ) click to toggle source

Output a multiple resources as the response data.

@param [Enumerable<Object>] resources to write. @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 [self]

# File lib/shamu/json_api/response.rb, line 29
def collection( collection, presenter = nil, &block )
  output[:data] =
    collection.map do |resource|
      build_resource resource, presenter, &block
    end
  self
end
compile() click to toggle source

(see BaseBuilder#compile)

# File lib/shamu/json_api/response.rb, line 82
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[:presenter], &options[:block] )
      end
    end

    compiled
  end
end
error( error = nil ) { |builder| ... } click to toggle source

@overload error( exception )

@param (see ErrorBuilder#exception)

@overload error( &block )

@yield (builder)
@yieldparam [ErrorBuilder] builder used to describe the error.

@return [self]

# File lib/shamu/json_api/response.rb, line 44
def error( error = nil, &block )
  builder = ErrorBuilder.new

  if error.is_a?( Exception )
    builder.exception( error )
  elsif error
    builder.title error
  end

  yield builder if block_given?

  errors = ( output[:errors] ||= [] )
  errors << builder.compile

  self
end
resource( resource, presenter = nil, &block ) click to toggle source

Output a single resource as the response data. @param [Object] resource to write. @param [Presenter] presenter used to write the resource state. @yield (builder) @yieldparam [ResourceBuilder] builder used write the resources fields

and meta.

@return [self]

# File lib/shamu/json_api/response.rb, line 15
def resource( resource, presenter = nil, &block )
  output[:data] = build_resource( resource, presenter, &block )
  self
end
to_json( * ) click to toggle source

@return [String]

# File lib/shamu/json_api/response.rb, line 108
def to_json( * )
  compile.to_json
end
to_s() click to toggle source

@return [String]

# File lib/shamu/json_api/response.rb, line 113
def to_s
  to_json
end
validation_errors( errors ) { |builder, attr, message| ... } click to toggle source

Write ActiveModel validation errors to the response.

@param [Hash<Symbol,String>] errors map of attributes to errors. @yield ( builder, attr, message ) @yieldparam [ErrorBuilder] builder the builder for this error message. @yieldparam [String] attr the attribute with a validation error. @yieldparam [String] message the error message. @return [self]

# File lib/shamu/json_api/response.rb, line 69
def validation_errors( errors, &block )
  errors.each do |attr, message|
    error message do |builder|
      path = "/data"
      path << "/attributes/#{ attr }" unless attr == :base
      builder.pointer path

      yield builder, attr, message if block_given?
    end
  end
end

Private Instance Methods

build_resource( resource, presenter ) { |builder| ... } click to toggle source
# File lib/shamu/json_api/response.rb, line 119
def build_resource( resource, presenter, &block )
  presenter = context.find_presenter( resource ) if !presenter && !block_given?
  builder   = ResourceBuilder.new( context )

  if presenter
    instance = presenter.new( resource, builder )
    instance.present
  else
    yield builder
  end

  builder.compile
end