class JSONAPI::Request::QueryParamCollection::IncludeParam

The include query param

Public Class Methods

new(includes_arr) click to toggle source

@param includes_arr [Array<String>] An array with each individual query include

Ex: incude=author,people => ['author', 'people']
Calls superclass method
# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 15
def initialize(includes_arr)
  includes_hash_structure = store_includes(includes_arr)
  super('includes', includes_hash_structure)
end

Public Instance Methods

to_s() click to toggle source

to string

# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 21
def to_s
  "include=#{stringify_includes_hash(value)}"
end

Private Instance Methods

add_member(loc_in_h, res_name, included:) click to toggle source

@param loc_in_h [Hash] The location within the main hash @param res_name [Symbol] The name of the resource @param included [TrueClass | FalseClass] Whether or not a resource

is being requested or not
# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 106
def add_member(loc_in_h, res_name, included:)
  if loc_in_h.key?(res_name)
    loc_in_h[res_name][:included] = included unless included == false
  else
    loc_in_h[res_name] = {
      included: included,
      relationships: {}
    }
  end
end
res_included?(i_arr) click to toggle source

@param (see store_include)

# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 90
def res_included?(i_arr)
  delim = i_arr[1]
  case delim
  when '.'
    true
  when '-'
    false
  else
    raise 'Syntax Error in include query string query param'
  end
end
store_include(loc_in_h, i_arr) click to toggle source

@param loc_in_h [Hash] The location within the main hash @param i_arr [Array<String>] The array of include strings

# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 79
def store_include(loc_in_h, i_arr)
  res_name = i_arr[0].to_sym
  if i_arr.length == 1
    add_member(loc_in_h, res_name, included: true)
  else
    add_member(loc_in_h, res_name, included: res_included?(i_arr))
    store_include(loc_in_h[res_name][:relationships], i_arr[2..-1])
  end
end
store_includes(includes_arr) click to toggle source

Helper for initialize @param includes_arr [Array<String>] The array of includes to store

# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 68
def store_includes(includes_arr)
  incl_hash = {}
  includes_arr.each do |include_str|
    include_str_arr = include_str.scan(/\w+|-|\./) # split into array (word, -, or .)
    store_include(incl_hash, include_str_arr)
  end
  incl_hash
end
stringify_includes_hash(includes_hash) click to toggle source

Represent include internal hash as query string @param includes_hash [Hash] The internal structure

# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 29
def stringify_includes_hash(includes_hash)
  to_return = ''
  first = true
  includes_hash.each do |mem_name, mem_hash|
    if first
      to_return += to_s_mem(mem_name, mem_hash)
      first = false
    else
      to_return += ",#{to_s_mem(mem_name, mem_hash)}"
    end
  end
  to_return
end
to_s_mem(mem_name, mem_hash) click to toggle source

Depending on the delimiter stringify differently. @param mem_name [Symbol] The name of the member to stringify @param mem_hash [Hash] The information about that member

# File lib/easy/jsonapi/request/query_param_collection/include_param.rb, line 46
def to_s_mem(mem_name, mem_hash)
  if mem_hash[:relationships] == {}
    mem_name.to_s
  else
    delimiter = mem_hash[:included] == true ? '.' : '-'
    prefix = "#{mem_name}#{delimiter}"
    to_return = ''
    first = true
    mem_hash[:relationships].each do |m_name, m_hash|
      if first
        to_return += "#{prefix}#{to_s_mem(m_name, m_hash)}"
        first = false
      else
        to_return += ",#{prefix}#{to_s_mem(m_name, m_hash)}"
      end
    end
    to_return
  end
end