Class: Caprese::Adapter::JsonApi
- Inherits:
-
ActiveModelSerializers::Adapter::Base
- Object
- ActiveModelSerializers::Adapter::Base
- Caprese::Adapter::JsonApi
- Extended by:
- ActiveSupport::Autoload
- Defined in:
- lib/caprese/adapter/json_api.rb,
lib/caprese/adapter/json_api/link.rb,
lib/caprese/adapter/json_api/meta.rb,
lib/caprese/adapter/json_api/error.rb,
lib/caprese/adapter/json_api/jsonapi.rb,
lib/caprese/adapter/json_api/json_pointer.rb,
lib/caprese/adapter/json_api/relationship.rb,
lib/caprese/adapter/json_api/pagination_links.rb,
lib/caprese/adapter/json_api/resource_identifier.rb
Defined Under Namespace
Modules: Error, JsonPointer, Jsonapi Classes: Link, Meta, PaginationLinks, Relationship, ResourceIdentifier
Class Method Summary collapse
Instance Method Summary collapse
-
#failure_document ⇒ Object
JSON API Errors TODO: look into caching definition: ☑ toplevel_errors array (required) ☐ toplevel_meta ☐ toplevel_jsonapi structure: { errors: toplevel_errors, meta: toplevel_meta, jsonapi: toplevel_jsonapi }.reject! {|_,v| v.nil? } prs: github.com/rails-api/active_model_serializers/pull/1004.
- #fragment_cache(cached_hash, non_cached_hash) ⇒ Object
-
#initialize(serializer, options = {}) ⇒ JsonApi
constructor
A new instance of JsonApi.
- #serializable_hash ⇒ Object
-
#success_document ⇒ Object
Primary data definition: ☐ toplevel_data (required) ☐ toplevel_included ☑ toplevel_meta ☑ toplevel_links ☑ toplevel_jsonapi structure: { data: toplevel_data, included: toplevel_included, meta: toplevel_meta, links: toplevel_links, jsonapi: toplevel_jsonapi }.reject! {|_,v| v.nil? } rubocop:disable Metrics/CyclomaticComplexity.
Constructor Details
#initialize(serializer, options = {}) ⇒ JsonApi
Returns a new instance of JsonApi.
31 32 33 34 35 |
# File 'lib/caprese/adapter/json_api.rb', line 31 def initialize(serializer, = {}) super @include_directive = JSONAPI::IncludeDirective.new([:include], allow_wildcard: true) @fieldset = [:fieldset] || ActiveModel::Serializer::Fieldset.new(.delete(:fields)) end |
Class Method Details
.default_key_transform ⇒ Object
16 17 18 |
# File 'lib/caprese/adapter/json_api.rb', line 16 def self.default_key_transform :dash end |
.fragment_cache(cached_hash, non_cached_hash, root = true) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/caprese/adapter/json_api.rb', line 20 def self.fragment_cache(cached_hash, non_cached_hash, root = true) core_cached = cached_hash.first core_non_cached = non_cached_hash.first no_root_cache = cached_hash.delete_if { |key, _value| key == core_cached[0] } no_root_non_cache = non_cached_hash.delete_if { |key, _value| key == core_non_cached[0] } cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1] hash = root ? { root => cached_resource } : cached_resource hash.deep_merge no_root_non_cache.deep_merge no_root_cache end |
Instance Method Details
#failure_document ⇒ Object
JSON API Errors TODO: look into caching definition:
structure:
{
errors: toplevel_errors,
meta: ,
jsonapi: toplevel_jsonapi
}.reject! {|_,v| v.nil? }
prs:
https://github.com/rails-api/active_model_serializers/pull/1004
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/caprese/adapter/json_api.rb', line 152 def failure_document hash = {} # PR Please :) # Jsonapi.add!(hash) # toplevel_errors # definition: # array of unique items of type 'error' # structure: # [ # error, # error # ] if serializer.respond_to?(:each) hash[:errors] = serializer.flat_map do |error_serializer| Error.resource_errors(error_serializer, ) end else hash[:errors] = if serializer.resource_errors? Error.resource_errors(serializer, ) elsif serializer.document_errors? Error.document_errors(serializer, ) else Error.param_errors(serializer, ) end end hash end |
#fragment_cache(cached_hash, non_cached_hash) ⇒ Object
48 49 50 51 |
# File 'lib/caprese/adapter/json_api.rb', line 48 def fragment_cache(cached_hash, non_cached_hash) root = !.include?(:include) self.class.fragment_cache(cached_hash, non_cached_hash, root) end |
#serializable_hash ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/caprese/adapter/json_api.rb', line 39 def serializable_hash(*) document = if serializer.success? success_document else failure_document end self.class.transform_key_casing!(document, ) end |
#success_document ⇒ Object
Primary data definition:
structure:
{
data: toplevel_data,
included: toplevel_included,
meta: ,
links: toplevel_links,
jsonapi: toplevel_jsonapi
}.reject! {|_,v| v.nil? }
rubocop:disable Metrics/CyclomaticComplexity
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/caprese/adapter/json_api.rb', line 69 def success_document is_collection = serializer.respond_to?(:each) serializers = is_collection ? serializer : [serializer] primary_data, included = resource_objects_for(serializers) hash = {} # toplevel_data # definition: # oneOf # resource # array of unique items of type 'resource' # null # # description: # The document's "primary data" is a representation of the resource or collection of resources # targeted by a request. # # Singular: the resource object. # # Collection: one of an array of resource objects, an array of resource identifier objects, or # an empty array ([]), for requests that target resource collections. # # None: null if the request is one that might correspond to a single resource, but doesn't currently. # structure: # if serializable_resource.resource? # resource # elsif serializable_resource.collection? # [ # resource, # resource # ] # else # nil # end hash[:data] = is_collection ? primary_data : primary_data[0] # toplevel_included # alias included # definition: # array of unique items of type 'resource' # # description: # To reduce the number of HTTP requests, servers **MAY** allow # responses that include related resources along with the requested primary # resources. Such responses are called "compound documents". # structure: # [ # resource, # resource # ] hash[:included] = included if included.any? Jsonapi.add!(hash) if [:links] hash[:links] ||= {} hash[:links].update([:links]) end if is_collection && serializer.paginated? hash[:links] ||= {} hash[:links].update(pagination_links_for(serializer)) end hash[:meta] = [:meta] unless [:meta].blank? hash end |