class Flexirest::JsonAPIProxy::Request::Params::Parameters
Private class for building JSON API compliant parameters
Public Class Methods
new(id, type)
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 85 def initialize(id, type) @params = build(id, type) end
Public Instance Methods
add_attribute(key, value)
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 143 def add_attribute(key, value) # Add a resource attribute to the attributes object # within the resource object @params[:data][:attributes][key] = value end
add_relationship(name, type, id)
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 116 def add_relationship(name, type, id) # Use the `name` parameter to determine the type of relationship if singular?(name) # If `name` is a singular word (one-to-one relationship), # add or overwrite the data object for the given `name`, # containing a type and id value to the relationships object @params[:data][:relationships][name] = { data: { type: type, id: id } } elsif @params[:data][:relationships][name] # If `name` is a plural word (one-to-many relationship), # and the `name` object already exists in the relationships object, # assume a nested data array exists, and add a new data object # containing a type and id value to the data array @params[:data][:relationships][name][:data] << { type: type, id: id } else # If `name` is a plural word, but the `name` object does not exist, # add a new `name` object containing a data array, # which consists of exactly one data object with the type and id @params[:data][:relationships][name] = { data: [{ type: type, id: id }] } end end
build(id, type)
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 149 def build(id, type) # Build the standard resource object pp = {} pp[:data] = {} pp[:data][:id] = id if id pp[:data][:type] = type pp[:data][:attributes] = {} pp[:data][:relationships] = {} pp end
create_from_hash(hash)
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 93 def create_from_hash(hash) hash.each do |k, v| # Build JSON API compliant parameters from each key and value # in the standard-style parameters hash if v.is_a?(Array) # This is a one-to-many relationship validate_relationships!(v) # Add a relationship object for all related resources v.each { |el| add_relationship(k, type(el), el.id) } elsif v.is_a?(Flexirest::Base) # This is a one-to-one relationship add_relationship(k, type(v), v.id) else # This is a normal attribute add_attribute(k, v) end end end
raise_params_error!()
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 165 def raise_params_error! raise Exception.new("Cannot contain different instance types!") end
to_hash()
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 89 def to_hash @params end
validate_relationships!(v)
click to toggle source
# File lib/flexirest/json_api_proxy.rb, line 160 def validate_relationships!(v) # Should always contain the same class in entire relationships array raise_params_error! if v.map(&:class).count > 1 end