Class: Shamu::JsonApi::Response

Inherits:
BaseBuilder show all
Defined in:
lib/shamu/json_api/response.rb

Overview

Build a JSON API response from one or more resources.

Instance Method Summary collapse

Methods inherited from BaseBuilder

#initialize

Methods included from BuilderMethods::Meta

#meta

Methods included from BuilderMethods::Link

#link

Constructor Details

This class inherits a constructor from Shamu::JsonApi::BaseBuilder

Instance Method Details

#as_jsonHash

Returns the compiled resources.

Returns:

  • (Hash)

    the compiled resources.



103
104
105
# File 'lib/shamu/json_api/response.rb', line 103

def as_json( * )
  compile.as_json
end

#collection(collection, presenter = nil) {|builder, resource| ... } ⇒ self

Output a multiple resources as the response data.

Parameters:

  • resources (Enumerable<Object>)

    to write.

  • presenter (Presenter) (defaults to: nil)

    used to write the resource state.

Yields:

Yield Parameters:

  • builder (ResourceBuilder)

    used write the resources fields and meta.

  • resource (Object)

    being written.

Returns:

  • (self)


29
30
31
32
33
34
35
# 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

#compileHash

Returns the results output as JSON safe hash.

Returns:

  • (Hash)

    the results output as JSON safe hash.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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(exception) ⇒ self #error {|builder| ... } ⇒ self

Overloads:

  • #error {|builder| ... } ⇒ self

    Yields:

    • (builder)

    Yield Parameters:

Yields:

  • (builder)

Returns:

  • (self)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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) {|builder| ... } ⇒ self

Output a single resource as the response data.

Parameters:

  • resource (Object)

    to write.

  • presenter (Presenter) (defaults to: nil)

    used to write the resource state.

Yields:

  • (builder)

Yield Parameters:

Returns:

  • (self)


15
16
17
18
# 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_jsonString

Returns:

  • (String)


108
109
110
# File 'lib/shamu/json_api/response.rb', line 108

def to_json( * )
  compile.to_json
end

#to_sString

Returns:

  • (String)


113
114
115
# File 'lib/shamu/json_api/response.rb', line 113

def to_s
  to_json
end

#validation_errors(errors) {|builder, attr, message| ... } ⇒ self

Write ActiveModel validation errors to the response.

Parameters:

  • errors (Hash<Symbol,String>)

    map of attributes to errors.

Yields:

  • (builder, attr, message)

Yield Parameters:

  • builder (ErrorBuilder)

    the builder for this error message.

  • attr (String)

    the attribute with a validation error.

  • message (String)

    the error message.

Returns:

  • (self)


69
70
71
72
73
74
75
76
77
78
79
# 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