Class: JSONAPI::RequestParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/request_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = nil, options = {}) ⇒ RequestParser

Returns a new instance of RequestParser.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jsonapi/request_parser.rb', line 11

def initialize(params = nil, options = {})
  @params = params
  @context = options[:context]
  @key_formatter = options.fetch(:key_formatter, JSONAPI.configuration.key_formatter)
  @errors = []
  @warnings = []
  @operations = []
  @fields = {}
  @filters = {}
  @sort_criteria = nil
  @source_klass = nil
  @source_id = nil
  @include_directives = nil
  @paginator = nil
  @id = nil
  @server_error_callbacks = options.fetch(:server_error_callbacks, [])

  setup_action(@params)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def context
  @context
end

#errorsObject

Returns the value of attribute errors.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def errors
  @errors
end

#fieldsObject

Returns the value of attribute fields.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def fields
  @fields
end

#filtersObject

Returns the value of attribute filters.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def filters
  @filters
end

#includeObject

Returns the value of attribute include.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def include
  @include
end

#include_directivesObject

Returns the value of attribute include_directives.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def include_directives
  @include_directives
end

#operationsObject

Returns the value of attribute operations.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def operations
  @operations
end

#paginatorObject

Returns the value of attribute paginator.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def paginator
  @paginator
end

#paramsObject

Returns the value of attribute params.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def params
  @params
end

#resource_klassObject

Returns the value of attribute resource_klass.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def resource_klass
  @resource_klass
end

#server_error_callbacksObject

Returns the value of attribute server_error_callbacks.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def server_error_callbacks
  @server_error_callbacks
end

#sort_criteriaObject

Returns the value of attribute sort_criteria.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def sort_criteria
  @sort_criteria
end

#source_idObject

Returns the value of attribute source_id.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def source_id
  @source_id
end

#source_klassObject

Returns the value of attribute source_klass.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def source_klass
  @source_klass
end

#warningsObject

Returns the value of attribute warnings.



7
8
9
# File 'lib/jsonapi/request_parser.rb', line 7

def warnings
  @warnings
end

Instance Method Details

#add_find_operationObject



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/jsonapi/request_parser.rb', line 306

def add_find_operation
  @operations.push JSONAPI::Operation.new(:find,
    @resource_klass,
    context: @context,
    filters: @filters,
    include_directives: @include_directives,
    sort_criteria: @sort_criteria,
    paginator: @paginator,
    fields: @fields
  )
end

#add_show_operationObject



318
319
320
321
322
323
324
325
326
# File 'lib/jsonapi/request_parser.rb', line 318

def add_show_operation
  @operations.push JSONAPI::Operation.new(:show,
    @resource_klass,
    context: @context,
    id: @id,
    include_directives: @include_directives,
    fields: @fields
  )
end


337
338
339
340
341
342
343
344
345
346
347
# File 'lib/jsonapi/request_parser.rb', line 337

def add_show_related_resource_operation(relationship_type)
  @operations.push JSONAPI::Operation.new(:show_related_resource,
    @resource_klass,
    context: @context,
    relationship_type: relationship_type,
    source_klass: @source_klass,
    source_id: @source_id,
    fields: @fields,
    include_directives: @include_directives
  )
end


349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/jsonapi/request_parser.rb', line 349

def add_show_related_resources_operation(relationship_type)
  @operations.push JSONAPI::Operation.new(:show_related_resources,
    @resource_klass,
    context: @context,
    relationship_type: relationship_type,
    source_klass: @source_klass,
    source_id: @source_id,
    filters: @source_klass.verify_filters(@filters, @context),
    sort_criteria: @sort_criteria,
    paginator: @paginator,
    fields: @fields,
    include_directives: @include_directives
  )
end

#add_show_relationship_operation(relationship_type, parent_key) ⇒ Object



328
329
330
331
332
333
334
335
# File 'lib/jsonapi/request_parser.rb', line 328

def add_show_relationship_operation(relationship_type, parent_key)
  @operations.push JSONAPI::Operation.new(:show_relationship,
    @resource_klass,
    context: @context,
    relationship_type: relationship_type,
    parent_key: @resource_klass.verify_key(parent_key)
  )
end

#check_include(resource_klass, include_parts) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/jsonapi/request_parser.rb', line 200

def check_include(resource_klass, include_parts)
  relationship_name = unformat_key(include_parts.first)

  relationship = resource_klass._relationship(relationship_name)
  if relationship && format_key(relationship_name) == include_parts.first
    unless include_parts.last.empty?
      check_include(Resource.resource_for(resource_klass.module_path + relationship.class_name.to_s.underscore), include_parts.last.partition('.'))
    end
  else
    @errors.concat(JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type),
                                                           include_parts.first).errors)
  end
end

#check_sort_criteria(resource_klass, sort_criteria) ⇒ Object



296
297
298
299
300
301
302
303
304
# File 'lib/jsonapi/request_parser.rb', line 296

def check_sort_criteria(resource_klass, sort_criteria)
  sort_field = sort_criteria[:field]
  sortable_fields = resource_klass.sortable_fields(context)

  unless sortable_fields.include? sort_field.to_sym
    @errors.concat(JSONAPI::Exceptions::InvalidSortCriteria
                     .new(format_key(resource_klass._type), sort_field).errors)
  end
end

#format_key(key) ⇒ Object



670
671
672
# File 'lib/jsonapi/request_parser.rb', line 670

def format_key(key)
  @key_formatter.format(key)
end

#initialize_source(params) ⇒ Object



137
138
139
140
# File 'lib/jsonapi/request_parser.rb', line 137

def initialize_source(params)
  @source_klass = Resource.resource_for(params.require(:source))
  @source_id = @source_klass.verify_key(params.require(@source_klass._as_parent_key), @context)
end

#parse_add_operation(params) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/jsonapi/request_parser.rb', line 364

def parse_add_operation(params)
  fail JSONAPI::Exceptions::InvalidDataFormat unless params.respond_to?(:each_pair)

  verify_type(params[:type])

  data = parse_params(params, @resource_klass.creatable_fields(@context))
  @operations.push JSONAPI::Operation.new(:create_resource,
    @resource_klass,
    context: @context,
    data: data,
    fields: @fields,
    include_directives: @include_directives
  )
rescue JSONAPI::Exceptions::Error => e
  @errors.concat(e.errors)
end

#parse_add_relationship_operation(verified_params, relationship, parent_key) ⇒ Object



574
575
576
577
578
579
580
581
582
583
584
# File 'lib/jsonapi/request_parser.rb', line 574

def parse_add_relationship_operation(verified_params, relationship, parent_key)
  if relationship.is_a?(JSONAPI::Relationship::ToMany)
    @operations.push JSONAPI::Operation.new(:create_to_many_relationships,
      resource_klass,
      context: @context,
      resource_id: parent_key,
      relationship_type: relationship.name,
      data: verified_params[:to_many].values[0]
    )
  end
end

#parse_fields(fields) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/jsonapi/request_parser.rb', line 149

def parse_fields(fields)
  return if fields.nil?

  extracted_fields = {}

  # Extract the fields for each type from the fields parameters
  if fields.is_a?(ActionController::Parameters)
    fields.each do |field, value|
      resource_fields = value.split(',') unless value.nil? || value.empty?
      extracted_fields[field] = resource_fields
    end
  else
    fail JSONAPI::Exceptions::InvalidFieldFormat.new
  end

  # Validate the fields
  extracted_fields.each do |type, values|
    underscored_type = unformat_key(type)
    extracted_fields[type] = []
    begin
      if type != format_key(type)
        fail JSONAPI::Exceptions::InvalidResource.new(type)
      end
      type_resource = Resource.resource_for(@resource_klass.module_path + underscored_type.to_s)
    rescue NameError
      @errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
    rescue JSONAPI::Exceptions::InvalidResource => e
      @errors.concat(e.errors)
    end

    if type_resource.nil?
      @errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
    else
      unless values.nil?
        valid_fields = type_resource.fields.collect { |key| format_key(key) }
        values.each do |field|
          if valid_fields.include?(field)
            extracted_fields[type].push unformat_key(field)
          else
            @errors.concat(JSONAPI::Exceptions::InvalidField.new(type, field).errors)
          end
        end
      else
        @errors.concat(JSONAPI::Exceptions::InvalidField.new(type, 'nil').errors)
      end
    end
  end

  @fields = extracted_fields.deep_transform_keys { |key| unformat_key(key) }
end

#parse_filters(filters) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/jsonapi/request_parser.rb', line 238

def parse_filters(filters)
  return unless filters

  unless JSONAPI.configuration.allow_filter
    fail JSONAPI::Exceptions::ParametersNotAllowed.new([:filter])
  end

  unless filters.class.method_defined?(:each)
    @errors.concat(JSONAPI::Exceptions::InvalidFiltersSyntax.new(filters).errors)
    return
  end

  filters.each do |key, value|
    filter = unformat_key(key)
    if @resource_klass._allowed_filter?(filter)
      @filters[filter] = value
    else
      @errors.concat(JSONAPI::Exceptions::FilterNotAllowed.new(filter).errors)
    end
  end
end

#parse_include_directives(raw_include) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/jsonapi/request_parser.rb', line 214

def parse_include_directives(raw_include)
  return unless raw_include

  unless JSONAPI.configuration.allow_include
    fail JSONAPI::Exceptions::ParametersNotAllowed.new([:include])
  end

  included_resources = []
  begin
    included_resources += CSV.parse_line(raw_include)
  rescue CSV::MalformedCSVError
    fail JSONAPI::Exceptions::InvalidInclude.new(format_key(@resource_klass._type), raw_include)
  end

  return if included_resources.empty?

  result = included_resources.compact.map do |included_resource|
    check_include(@resource_klass, included_resource.partition('.'))
    unformat_key(included_resource).to_s
  end

  @include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, result)
end

#parse_modify_relationship_action(params, modification_type) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/jsonapi/request_parser.rb', line 116

def parse_modify_relationship_action(params, modification_type)
  relationship_type = params.require(:relationship)
  parent_key = params.require(@resource_klass._as_parent_key)
  relationship = @resource_klass._relationship(relationship_type)

  # Removals of to-one relationships are done implicitly and require no specification of data
  data_required = !(modification_type == :remove && relationship.is_a?(JSONAPI::Relationship::ToOne))

  if data_required
    data = params.fetch(:data)
    object_params = { relationships: { format_key(relationship.name) => { data: data } } }
    verified_params = parse_params(object_params, @resource_klass.updatable_fields(@context))

    parse_arguments = [verified_params, relationship, parent_key]
  else
    parse_arguments = [params, relationship, parent_key]
  end

  send(:"parse_#{modification_type}_relationship_operation", *parse_arguments)
end

#parse_pagination(page) ⇒ Object



142
143
144
145
146
147
# File 'lib/jsonapi/request_parser.rb', line 142

def parse_pagination(page)
  paginator_name = @resource_klass._paginator
  @paginator = JSONAPI::Paginator.paginator_for(paginator_name).new(page) unless paginator_name == :none
rescue JSONAPI::Exceptions::Error => e
  @errors.concat(e.errors)
end

#parse_params(params, allowed_fields) ⇒ Object



424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/jsonapi/request_parser.rb', line 424

def parse_params(params, allowed_fields)
  verify_permitted_params(params, allowed_fields)

  checked_attributes = {}
  checked_to_one_relationships = {}
  checked_to_many_relationships = {}

  params.each do |key, value|
    case key.to_s
    when 'relationships'
      value.each do |link_key, link_value|
        param = unformat_key(link_key)
        relationship = @resource_klass._relationship(param)

        if relationship.is_a?(JSONAPI::Relationship::ToOne)
          checked_to_one_relationships[param] = parse_to_one_relationship(link_value, relationship)
        elsif relationship.is_a?(JSONAPI::Relationship::ToMany)
          parse_to_many_relationship(link_value, relationship) do |result_val|
            checked_to_many_relationships[param] = result_val
          end
        end
      end
    when 'id'
      checked_attributes['id'] = unformat_value(:id, value)
    when 'attributes'
      value.each do |key, value|
        param = unformat_key(key)
        checked_attributes[param] = unformat_value(param, value)
      end
    end
  end

  return {
    'attributes' => checked_attributes,
    'to_one' => checked_to_one_relationships,
    'to_many' => checked_to_many_relationships
  }.deep_transform_keys { |key| unformat_key(key) }
end

#parse_remove_operation(params) ⇒ Object



644
645
646
647
648
649
650
651
# File 'lib/jsonapi/request_parser.rb', line 644

def parse_remove_operation(params)
  @operations.push JSONAPI::Operation.new(:remove_resource,
    @resource_klass,
    context: @context,
    resource_id: @resource_klass.verify_key(params.require(:id), context))
rescue JSONAPI::Exceptions::Error => e
  @errors.concat(e.errors)
end

#parse_remove_relationship_operation(params, relationship, parent_key) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/jsonapi/request_parser.rb', line 653

def parse_remove_relationship_operation(params, relationship, parent_key)
  operation_base_args = [resource_klass].push(
    context: @context,
    resource_id: parent_key,
    relationship_type: relationship.name
  )

  if relationship.is_a?(JSONAPI::Relationship::ToMany)
    operation_args = operation_base_args.dup
    keys = params[:to_many].values[0]
    operation_args[1] = operation_args[1].merge(associated_keys: keys)
    @operations.push JSONAPI::Operation.new(:remove_to_many_relationships, *operation_args)
  else
    @operations.push JSONAPI::Operation.new(:remove_to_one_relationship, *operation_base_args)
  end
end

#parse_replace_operation(data, keys) ⇒ Object



638
639
640
641
642
# File 'lib/jsonapi/request_parser.rb', line 638

def parse_replace_operation(data, keys)
  parse_single_replace_operation(data, [keys], id_key_presence_check_required: keys.present?)
rescue JSONAPI::Exceptions::Error => e
  @errors.concat(e.errors)
end

#parse_single_replace_operation(data, keys, id_key_presence_check_required: true) ⇒ Object



614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/jsonapi/request_parser.rb', line 614

def parse_single_replace_operation(data, keys, id_key_presence_check_required: true)
  fail JSONAPI::Exceptions::InvalidDataFormat unless data.respond_to?(:each_pair)

  fail JSONAPI::Exceptions::MissingKey.new if data[:id].nil?

  key = data[:id].to_s
  if id_key_presence_check_required && !keys.include?(key)
    fail JSONAPI::Exceptions::KeyNotIncludedInURL.new(key)
  end

  data.delete(:id) unless keys.include?(:id)

  verify_type(data[:type])

  @operations.push JSONAPI::Operation.new(:replace_fields,
    @resource_klass,
    context: @context,
    resource_id: key,
    data: parse_params(data, @resource_klass.updatable_fields(@context)),
    fields: @fields,
    include_directives: @include_directives
  )
end

#parse_sort_criteria(sort_criteria) ⇒ Object



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/jsonapi/request_parser.rb', line 267

def parse_sort_criteria(sort_criteria)
  return unless sort_criteria.present?

  unless JSONAPI.configuration.allow_sort
    fail JSONAPI::Exceptions::ParametersNotAllowed.new([:sort])
  end

  sorts = []
  begin
    raw = CGI.unescape(sort_criteria)
    sorts += CSV.parse_line(raw)
  rescue CSV::MalformedCSVError
    fail JSONAPI::Exceptions::InvalidSortCriteria.new(format_key(@resource_klass._type), raw)
  end

  @sort_criteria = sorts.collect do |sort|
    if sort.start_with?('-')
      sort_criteria = { field: unformat_key(sort[1..-1]).to_s }
      sort_criteria[:direction] = :desc
    else
      sort_criteria = { field: unformat_key(sort).to_s }
      sort_criteria[:direction] = :asc
    end

    check_sort_criteria(@resource_klass, sort_criteria)
    sort_criteria
  end
end


408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/jsonapi/request_parser.rb', line 408

def parse_to_many_links_object(raw)
  fail JSONAPI::Exceptions::InvalidLinksObject.new if raw.nil?

  links_object = {}
  if raw.is_a?(Array)
    raw.each do |link|
      link_object = parse_to_one_links_object(link)
      links_object[link_object[:type]] ||= []
      links_object[link_object[:type]].push(link_object[:id])
    end
  else
    fail JSONAPI::Exceptions::InvalidLinksObject.new
  end
  links_object
end

#parse_to_many_relationship(link_value, relationship, &add_result) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/jsonapi/request_parser.rb', line 489

def parse_to_many_relationship(link_value, relationship, &add_result)
  if link_value.is_a?(Array) && link_value.length == 0
    linkage = []
  elsif (link_value.is_a?(Hash) || link_value.is_a?(ActionController::Parameters))
    linkage = link_value[:data]
  else
    fail JSONAPI::Exceptions::InvalidLinksObject.new
  end

  links_object = parse_to_many_links_object(linkage)

  # Since we do not yet support polymorphic to_many relationships we will raise an error if the type does not match the
  # relationship's type.
  # ToDo: Support Polymorphic relationships

  if links_object.length == 0
    add_result.call([])
  else
    if links_object.length > 1 || !links_object.has_key?(unformat_key(relationship.type).to_s)
      fail JSONAPI::Exceptions::TypeMismatch.new(links_object[:type])
    end

    links_object.each_pair do |type, keys|
      relationship_resource = Resource.resource_for(@resource_klass.module_path + unformat_key(type).to_s)
      add_result.call relationship_resource.verify_keys(keys, @context)
    end
  end
end


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/jsonapi/request_parser.rb', line 389

def parse_to_one_links_object(raw)
  if raw.nil?
    return {
      type: nil,
      id: nil
    }
  end

  if !(raw.is_a?(Hash) || raw.is_a?(ActionController::Parameters)) ||
     raw.keys.length != 2 || !(raw.key?('type') && raw.key?('id'))
    fail JSONAPI::Exceptions::InvalidLinksObject.new
  end

  {
    type: unformat_key(raw['type']).to_s,
    id: raw['id']
  }
end

#parse_to_one_relationship(link_value, relationship) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/jsonapi/request_parser.rb', line 463

def parse_to_one_relationship(link_value, relationship)
  if link_value.nil?
    linkage = nil
  else
    linkage = link_value[:data]
  end

  links_object = parse_to_one_links_object(linkage)
  if !relationship.polymorphic? && links_object[:type] && (links_object[:type].to_s != relationship.type.to_s)
    fail JSONAPI::Exceptions::TypeMismatch.new(links_object[:type])
  end

  unless links_object[:id].nil?
    resource = self.resource_klass || Resource
    relationship_resource = resource.resource_for(unformat_key(links_object[:type]).to_s)
    relationship_id = relationship_resource.verify_key(links_object[:id], @context)
    if relationship.polymorphic?
      { id: relationship_id, type: unformat_key(links_object[:type].to_s) }
    else
      relationship_id
    end
  else
    nil
  end
end

#parse_update_relationship_operation(verified_params, relationship, parent_key) ⇒ Object



586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/jsonapi/request_parser.rb', line 586

def parse_update_relationship_operation(verified_params, relationship, parent_key)
  options = {
    context: @context,
    resource_id: parent_key,
    relationship_type: relationship.name
  }

  if relationship.is_a?(JSONAPI::Relationship::ToOne)
    if relationship.polymorphic?
      options[:key_value] = verified_params[:to_one].values[0][:id]
      options[:key_type] = verified_params[:to_one].values[0][:type]

      operation_type = :replace_polymorphic_to_one_relationship
    else
      options[:key_value] = verified_params[:to_one].values[0]
      operation_type = :replace_to_one_relationship
    end
  elsif relationship.is_a?(JSONAPI::Relationship::ToMany)
    unless relationship.acts_as_set
      fail JSONAPI::Exceptions::ToManySetReplacementForbidden.new
    end
    options[:data] = verified_params[:to_many].values[0]
    operation_type = :replace_to_many_relationships
  end

  @operations.push JSONAPI::Operation.new(operation_type, resource_klass, options)
end

#set_default_filtersObject



260
261
262
263
264
265
# File 'lib/jsonapi/request_parser.rb', line 260

def set_default_filters
  @resource_klass._allowed_filters.each do |filter, opts|
    next if opts[:default].nil? || !@filters[filter].nil?
    @filters[filter] = opts[:default]
  end
end

#setup_action(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jsonapi/request_parser.rb', line 31

def setup_action(params)
  return if params.nil?

  @resource_klass ||= Resource.resource_for(params[:controller]) if params[:controller]

  setup_action_method_name = "setup_#{params[:action]}_action"
  if respond_to?(setup_action_method_name)
    raise params[:_parser_exception] if params[:_parser_exception]
    send(setup_action_method_name, params)
  end
rescue ActionController::ParameterMissing => e
  @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors)
end

#setup_create_action(params) ⇒ Object



88
89
90
91
92
# File 'lib/jsonapi/request_parser.rb', line 88

def setup_create_action(params)
  parse_fields(params[:fields])
  parse_include_directives(params[:include])
  parse_add_operation(params.require(:data))
end

#setup_create_relationship_action(params) ⇒ Object



94
95
96
# File 'lib/jsonapi/request_parser.rb', line 94

def setup_create_relationship_action(params)
  parse_modify_relationship_action(params, :add)
end

#setup_destroy_action(params) ⇒ Object



108
109
110
# File 'lib/jsonapi/request_parser.rb', line 108

def setup_destroy_action(params)
  parse_remove_operation(params)
end

#setup_destroy_relationship_action(params) ⇒ Object



112
113
114
# File 'lib/jsonapi/request_parser.rb', line 112

def setup_destroy_relationship_action(params)
  parse_modify_relationship_action(params, :remove)
end


55
56
57
58
59
60
61
62
63
64
# File 'lib/jsonapi/request_parser.rb', line 55

def setup_get_related_resource_action(params)
  initialize_source(params)
  parse_fields(params[:fields])
  parse_include_directives(params[:include])
  set_default_filters
  parse_filters(params[:filter])
  parse_sort_criteria(params[:sort])
  parse_pagination(params[:page])
  add_show_related_resource_operation(params[:relationship])
end


66
67
68
69
70
71
72
73
74
75
# File 'lib/jsonapi/request_parser.rb', line 66

def setup_get_related_resources_action(params)
  initialize_source(params)
  parse_fields(params[:fields])
  parse_include_directives(params[:include])
  set_default_filters
  parse_filters(params[:filter])
  parse_sort_criteria(params[:sort])
  parse_pagination(params[:page])
  add_show_related_resources_operation(params[:relationship])
end

#setup_index_action(params) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/jsonapi/request_parser.rb', line 45

def setup_index_action(params)
  parse_fields(params[:fields])
  parse_include_directives(params[:include])
  set_default_filters
  parse_filters(params[:filter])
  parse_sort_criteria(params[:sort])
  parse_pagination(params[:page])
  add_find_operation
end

#setup_show_action(params) ⇒ Object



77
78
79
80
81
82
# File 'lib/jsonapi/request_parser.rb', line 77

def setup_show_action(params)
  parse_fields(params[:fields])
  parse_include_directives(params[:include])
  @id = params[:id]
  add_show_operation
end

#setup_show_relationship_action(params) ⇒ Object



84
85
86
# File 'lib/jsonapi/request_parser.rb', line 84

def setup_show_relationship_action(params)
  add_show_relationship_operation(params[:relationship], params.require(@resource_klass._as_parent_key))
end

#setup_update_action(params) ⇒ Object



102
103
104
105
106
# File 'lib/jsonapi/request_parser.rb', line 102

def setup_update_action(params)
  parse_fields(params[:fields])
  parse_include_directives(params[:include])
  parse_replace_operation(params.require(:data), params[:id])
end

#setup_update_relationship_action(params) ⇒ Object



98
99
100
# File 'lib/jsonapi/request_parser.rb', line 98

def setup_update_relationship_action(params)
  parse_modify_relationship_action(params, :update)
end

#unformat_key(key) ⇒ Object



674
675
676
677
# File 'lib/jsonapi/request_parser.rb', line 674

def unformat_key(key)
  unformatted_key = @key_formatter.unformat(key)
  unformatted_key.nil? ? nil : unformatted_key.to_sym
end

#unformat_value(attribute, value) ⇒ Object



518
519
520
521
# File 'lib/jsonapi/request_parser.rb', line 518

def unformat_value(attribute, value)
  value_formatter = JSONAPI::ValueFormatter.value_formatter_for(@resource_klass._attribute_options(attribute)[:format])
  value_formatter.unformat(value)
end

#verify_permitted_params(params, allowed_fields) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/jsonapi/request_parser.rb', line 523

def verify_permitted_params(params, allowed_fields)
  formatted_allowed_fields = allowed_fields.collect { |field| format_key(field).to_sym }
  params_not_allowed = []

  params.each do |key, value|
    case key.to_s
    when 'relationships'
      value.keys.each do |links_key|
        unless formatted_allowed_fields.include?(links_key.to_sym)
          params_not_allowed.push(links_key)
          unless JSONAPI.configuration.raise_if_parameters_not_allowed
            value.delete links_key
          end
        end
      end
    when 'attributes'
      value.each do |attr_key, attr_value|
        unless formatted_allowed_fields.include?(attr_key.to_sym)
          params_not_allowed.push(attr_key)
          unless JSONAPI.configuration.raise_if_parameters_not_allowed
            value.delete attr_key
          end
        end
      end
    when 'type'
    when 'id'
      unless formatted_allowed_fields.include?(:id)
        params_not_allowed.push(:id)
        unless JSONAPI.configuration.raise_if_parameters_not_allowed
          params.delete :id
        end
      end
    else
      params_not_allowed.push(key)
    end
  end

  if params_not_allowed.length > 0
    if JSONAPI.configuration.raise_if_parameters_not_allowed
      fail JSONAPI::Exceptions::ParametersNotAllowed.new(params_not_allowed)
    else
      params_not_allowed_warnings = params_not_allowed.map do |key|
        JSONAPI::Warning.new(code: JSONAPI::PARAM_NOT_ALLOWED,
                             title: 'Param not allowed',
                             detail: "#{key} is not allowed.")
      end
      self.warnings.concat(params_not_allowed_warnings)
    end
  end
end

#verify_type(type) ⇒ Object



381
382
383
384
385
386
387
# File 'lib/jsonapi/request_parser.rb', line 381

def verify_type(type)
  if type.nil?
    fail JSONAPI::Exceptions::ParameterMissing.new(:type)
  elsif unformat_key(type).to_sym != @resource_klass._type
    fail JSONAPI::Exceptions::InvalidResource.new(type)
  end
end