Class: Shamu::JsonApi::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/shamu/json_api/context.rb

Instance Method Summary collapse

Constructor Details

#initialize(fields: nil, namespaces: [], presenters: {}) ⇒ Context

Returns a new instance of Context.

Parameters:

  • fields (Hash<Symbol,Array>) (defaults to: nil)

    explicitly declare the attributes and resources that should be included in the response. The hash consists of a keys of the resource types and values as an array of fields to be included.

    A String value will be split on , to allow for easy parsing of HTTP query parameters.

  • namespaces (Array<String>) (defaults to: [])

    to look for resource presenters. See #find_presenter.

  • presenters (Hash<Class,Class>) (defaults to: {})

    a hash that maps resource classes to the presenter class to use when building responses. See #find_presenter.



19
20
21
22
23
24
25
# File 'lib/shamu/json_api/context.rb', line 19

def initialize( fields: nil, namespaces: [], presenters: {} )
  @included_resources = {}
  @all_resources = Set.new
  @fields = parse_fields( fields )
  @namespaces = Array( namespaces )
  @presenters = presenters || {}
end

Instance Method Details

#collect_included_resourcesArray<Object,Hash>

Collects all the currently included resources and resets the queue.

Returns:

  • (Array<Object,Hash>)

    returns the the resource and presentation options from each resource buffered with #include_resource.



52
53
54
55
56
# File 'lib/shamu/json_api/context.rb', line 52

def collect_included_resources
  included = included_resources.dup
  @included_resources = {}
  included
end

#find_presenter(resource) ⇒ Class

Find a Presenter that can write the resource to a ResourceBuilder.

  • First looks for any explicit presenter given to the constructor that maps the resource's class to a specific presenter.
  • Next, looks through each of the namespaces given to the constructor. For each namespace, looks for a Namespace::#{ resource.class.name }Presenter. Will also check resource.class.model_name.name if available.
  • Fails with a NoPresenter error if a presenter cannot be found.

Parameters:

  • resource (Object)

    to present.

Returns:

Raises:



89
90
91
92
93
94
95
96
# File 'lib/shamu/json_api/context.rb', line 89

def find_presenter( resource )
  presenter   = presenters[ resource.class ]
  presenter ||= presenters[ resource.class ] = find_namespace_presenter( resource )

  fail NoPresenter.new( resource, namespaces ) unless presenter

  presenter
end

#include_field?(type, name, default = true) ⇒ Boolean

Check to see if the field should be included in the JSON API output.

Parameters:

  • type (Symbol)

    the resource type in question.

  • name (Symbol)

    of the field on the resouce in question.

  • default (Boolean) (defaults to: true)

    true if the field should be included by default when no explicit fields have been selected.

Returns:

  • (Boolean)

    true if the field should be included.



70
71
72
73
74
# File 'lib/shamu/json_api/context.rb', line 70

def include_field?( type, name, default = true )
  return default unless type_fields = fields[ type ]

  type_fields.include?( name )
end

#include_resource(resource, presenter = nil) {|builder| ... } ⇒ Object

Add an included resource for a compound response.

If no presenter and no block are provided a default presenter will be obtained by calling #find_presenter.

Parameters:

  • resource (Object)

    to be serialized.

  • presenter (Class) (defaults to: nil)

    tpresenter Presenter class to use to serialize the resource. If not provided a default Presenter will be chosen.

Yields:

  • (builder)

Yield Parameters:



38
39
40
41
42
43
44
45
46
# File 'lib/shamu/json_api/context.rb', line 38

def include_resource( resource, presenter = nil, &block )
  return if all_resources.include?( resource )

  all_resources << resource
  included_resources[resource] ||= begin
    presenter ||= find_presenter( resource ) unless block
    { presenter: presenter, block: block }
  end
end

#included_resources?Boolean

Returns true if there are any pending included resources.

Returns:

  • (Boolean)

    true if there are any pending included resources.



59
60
61
# File 'lib/shamu/json_api/context.rb', line 59

def included_resources?
  included_resources.any?
end

#params_metaHash

Returns of request param options to be output in the response meta.

Returns:

  • (Hash)

    of request param options to be output in the response meta.



99
100
101
102
103
# File 'lib/shamu/json_api/context.rb', line 99

def params_meta
  return unless fields.any?

  { fields: fields }
end