Class: LinkedRails::ParamsParser

Inherits:
Object
  • Object
show all
Includes:
Empathy::EmpJson::Helpers::Parsing, Empathy::EmpJson::Helpers::Slices
Defined in:
lib/linked_rails/params_parser.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slice: nil, params: {}, user_context: nil) ⇒ ParamsParser

Returns a new instance of ParamsParser.



11
12
13
14
15
# File 'lib/linked_rails/params_parser.rb', line 11

def initialize(slice: nil, params: {}, user_context: nil)
  self.slice = slice
  self.params = params
  self.user_context = user_context
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



8
9
10
# File 'lib/linked_rails/params_parser.rb', line 8

def params
  @params
end

#sliceObject

Returns the value of attribute slice.



8
9
10
# File 'lib/linked_rails/params_parser.rb', line 8

def slice
  @slice
end

#user_contextObject

Returns the value of attribute user_context.



8
9
10
# File 'lib/linked_rails/params_parser.rb', line 8

def user_context
  @user_context
end

Instance Method Details

#attributes_from_filters(klass) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/linked_rails/params_parser.rb', line 17

def attributes_from_filters(klass)
  ActionController::Parameters.new(
    filter_params.each_with_object({}) do |(predicate, value), hash|
      key_and_value = filter_to_param(klass, predicate, value)
      hash[key_and_value.first] = key_and_value.second if key_and_value
    end
  )
end

#parse_param(klass, predicate, object) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/linked_rails/params_parser.rb', line 26

def parse_param(klass, predicate, object)
  field_options = serializer_field(klass, predicate) unless predicate == '_id'
  if field_options.is_a?(FastJsonapi::Attribute)
    parse_attribute(klass, field_options, emp_to_primitive(object))
  elsif field_options.is_a?(FastJsonapi::Relationship)
    parse_association(klass, field_options, emp_to_primitive(object))
  end
end

#parse_resource(subject, klass) ⇒ Object

Recursively parses a resource from slice



36
37
38
39
40
41
42
43
44
# File 'lib/linked_rails/params_parser.rb', line 36

def parse_resource(subject, klass)
  (record_from_slice(slice, subject) || {}).reduce({}) do |h, (k, v)|
    (v.is_a?(Array) ? v : [v]).each do |value|
      param = parse_param(klass, RDF::URI(k), value)
      param ? add_param(h, param.first, param.second) : h
    end
    h
  end
end