module Flexirest::JsonAPIProxy::Request::Params

Creating and formatting JSON API parameters

Public Instance Methods

create(params, object) click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 37
def create(params, object)
  # Create a parameters object with the resource's type value and id
  parameters = Parameters.new(object.id, type(object))

  # Remove id attribute from top-level hash, this will be included
  # in the resource object
  params.delete(:id)

  # Build the JSON API compliant parameters
  parameters.create_from_hash(params)

  # Return the parameters as a hash, so it can be used elsewhere
  parameters.to_hash
end
translate(params, include_associations) click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 52
def translate(params, include_associations)
  # Format the linked resources array, and assign to include key
  params[:include] = format_include_params(include_associations) if include_associations.present?
end

Private Instance Methods

format_include_params(associations) click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 59
def format_include_params(associations)
  includes = []

  associations.each do |key|
    # Format each association name
    # if the key is a nested hash, format each nested association too
    # e.g. [author, comments.likes]

    if key.is_a?(Hash)
      # Create a link from each association to nested association
      key.each { |k, val| val.each { |v| includes << "#{k}.#{v}" } }

    else
      # Just convert the association to string, in case it is a Symbol
      includes << key.to_s
    end
  end

  # Join the includes array with comma separator
  includes.join(',')
end