Class: Flexirest::JsonAPIProxy::Request::Params::Parameters

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/flexirest/json_api_proxy.rb

Overview

Private class for building JSON API compliant parameters

Instance Method Summary collapse

Methods included from Helpers

#singular?, #type

Constructor Details

#initialize(id, type) ⇒ Parameters

Returns a new instance of Parameters.



85
86
87
# File 'lib/flexirest/json_api_proxy.rb', line 85

def initialize(id, type)
  @params = build(id, type)
end

Instance Method Details

#add_attribute(key, value) ⇒ Object



143
144
145
146
147
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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!Object

Raises:

  • (Exception)


165
166
167
# File 'lib/flexirest/json_api_proxy.rb', line 165

def raise_params_error!
  raise Exception.new("Cannot contain different instance types!")
end

#to_hashObject



89
90
91
# File 'lib/flexirest/json_api_proxy.rb', line 89

def to_hash
  @params
end

#validate_relationships!(v) ⇒ Object



160
161
162
163
# 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