Class: JsonapiCompliable::Util::IncludeParams

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi_compliable/util/include_params.rb

Overview

Utility class for dealing with Include Directives

Class Method Summary collapse

Class Method Details

.scrub(requested_includes, allowed_includes) ⇒ Hash

Let's say the user requested these sideloads:

GET /posts?include=comments.author

But our resource had this code:

sideload_whitelist({ index: [:comments] })

We should drop the 'author' sideload from the request.

Hashes become 'include directive hashes' within the library. ie

[:tags, { comments: :author }]

Becomes

{ tags: {}, comments: { author: {} } }

Parameters:

  • requested_includes (Hash)

    the nested hash the user requested

  • allowed_includes (Hash)

    the nested hash configured via DSL

Returns:

  • (Hash)

    the scrubbed hash



27
28
29
30
31
32
33
34
35
# File 'lib/jsonapi_compliable/util/include_params.rb', line 27

def scrub(requested_includes, allowed_includes)
  {}.tap do |valid|
    requested_includes.each_pair do |key, sub_hash|
      if allowed_includes[key]
        valid[key] = scrub(sub_hash, allowed_includes[key])
      end
    end
  end
end