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.



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

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

Instance Method Details

#add_attribute(key, value) ⇒ Object



146
147
148
149
150
# File 'lib/flexirest/json_api_proxy.rb', line 146

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/flexirest/json_api_proxy.rb', line 119

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



152
153
154
155
156
157
158
159
160
161
# File 'lib/flexirest/json_api_proxy.rb', line 152

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/flexirest/json_api_proxy.rb', line 96

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)


168
169
170
# File 'lib/flexirest/json_api_proxy.rb', line 168

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

#to_hashObject



92
93
94
# File 'lib/flexirest/json_api_proxy.rb', line 92

def to_hash
  @params
end

#validate_relationships!(v) ⇒ Object



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

def validate_relationships!(v)
  # Should always contain the same class in entire relationships array
  raise_params_error! if v.map(&:class).count > 1
end