Class: JSONAPI::Processor

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/jsonapi/processor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

included

Constructor Details

#initialize(resource_klass, operation_type, params) ⇒ Processor

Returns a new instance of Processor


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

def initialize(resource_klass, operation_type, params)
  @resource_klass = resource_klass
  @operation_type = operation_type
  @params = params
  @context = params[:context]
  @result = nil
  @result_options = {}
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context


44
45
46
# File 'lib/jsonapi/processor.rb', line 44

def context
  @context
end

#operation_typeObject (readonly)

Returns the value of attribute operation_type


44
45
46
# File 'lib/jsonapi/processor.rb', line 44

def operation_type
  @operation_type
end

#paramsObject (readonly)

Returns the value of attribute params


44
45
46
# File 'lib/jsonapi/processor.rb', line 44

def params
  @params
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass


44
45
46
# File 'lib/jsonapi/processor.rb', line 44

def resource_klass
  @resource_klass
end

#resultObject (readonly)

Returns the value of attribute result


44
45
46
# File 'lib/jsonapi/processor.rb', line 44

def result
  @result
end

#result_optionsObject (readonly)

Returns the value of attribute result_options


44
45
46
# File 'lib/jsonapi/processor.rb', line 44

def result_options
  @result_options
end

Class Method Details

._processor_from_resource_type(resource_klass) ⇒ Object


25
26
27
28
29
30
31
32
# File 'lib/jsonapi/processor.rb', line 25

def _processor_from_resource_type(resource_klass)
  processor = resource_klass.name.gsub(/Resource$/,'Processor').safe_constantize
  if processor.nil?
    processor = JSONAPI.configuration.default_processor_klass
  end

  return processor
end

.processor_instance_for(resource_klass, operation_type, params) ⇒ Object


21
22
23
# File 'lib/jsonapi/processor.rb', line 21

def processor_instance_for(resource_klass, operation_type, params)
  _processor_from_resource_type(resource_klass).new(resource_klass, operation_type, params)
end

.transactional_operation_type?(operation_type) ⇒ Boolean

Returns:

  • (Boolean)

34
35
36
37
38
39
40
41
# File 'lib/jsonapi/processor.rb', line 34

def transactional_operation_type?(operation_type)
  case operation_type
    when :find, :show, :show_related_resource, :show_related_resources
      return false
    else
      return true
  end
end

Instance Method Details

#create_resourceObject


190
191
192
193
194
195
196
# File 'lib/jsonapi/processor.rb', line 190

def create_resource
  data = params[:data]
  resource = resource_klass.create(context)
  result = resource.replace_fields(data)

  return JSONAPI::ResourceOperationResult.new((result == :completed ? :created : :accepted), resource)
end

#create_to_many_relationshipObject


240
241
242
243
244
245
246
247
248
249
# File 'lib/jsonapi/processor.rb', line 240

def create_to_many_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  data = params[:data]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.create_to_many_links(relationship_type, data)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#findObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/jsonapi/processor.rb', line 66

def find
  filters = params[:filters]
  include_directives = params[:include_directives]
  sort_criteria = params.fetch(:sort_criteria, [])
  paginator = params[:paginator]
  fields = params[:fields]

  verified_filters = resource_klass.verify_filters(filters, context)
  resource_records = resource_klass.find(verified_filters,
                                         context: context,
                                         include_directives: include_directives,
                                         sort_criteria: sort_criteria,
                                         paginator: paginator,
                                         fields: fields)

  page_options = {}
  if (JSONAPI.configuration.top_level_meta_include_record_count ||
    (paginator && paginator.class.requires_record_count))
    page_options[:record_count] = resource_klass.find_count(verified_filters,
                                                            context: context,
                                                            include_directives: include_directives)
  end

  if (JSONAPI.configuration.top_level_meta_include_page_count && page_options[:record_count])
    page_options[:page_count] = paginator.calculate_page_count(page_options[:record_count])
  end

  if JSONAPI.configuration.top_level_links_include_pagination && paginator
    page_options[:pagination_params] = paginator.links_page_params(page_options)
  end

  return JSONAPI::ResourcesOperationResult.new(:ok, resource_records, page_options)
end

#processObject


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

def process
  run_callbacks :operation do
    run_callbacks operation_type do
      @result = send(operation_type)
    end
  end

rescue JSONAPI::Exceptions::Error => e
  @result = JSONAPI::ErrorsOperationResult.new(e.errors[0].code, e.errors)
end

#remove_resourceObject


198
199
200
201
202
203
204
205
# File 'lib/jsonapi/processor.rb', line 198

def remove_resource
  resource_id = params[:resource_id]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.remove

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#remove_to_many_relationshipObject


262
263
264
265
266
267
268
269
270
271
# File 'lib/jsonapi/processor.rb', line 262

def remove_to_many_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  associated_key = params[:associated_key]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.remove_to_many_link(relationship_type, associated_key)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#remove_to_one_relationshipObject


273
274
275
276
277
278
279
280
281
# File 'lib/jsonapi/processor.rb', line 273

def remove_to_one_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.remove_to_one_link(relationship_type)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#replace_fieldsObject


207
208
209
210
211
212
213
214
215
# File 'lib/jsonapi/processor.rb', line 207

def replace_fields
  resource_id = params[:resource_id]
  data = params[:data]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_fields(data)

  return JSONAPI::ResourceOperationResult.new(result == :completed ? :ok : :accepted, resource)
end

#replace_polymorphic_to_one_relationshipObject


228
229
230
231
232
233
234
235
236
237
238
# File 'lib/jsonapi/processor.rb', line 228

def replace_polymorphic_to_one_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  key_value = params[:key_value]
  key_type = params[:key_type]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_polymorphic_to_one_link(relationship_type, key_value, key_type)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#replace_to_many_relationshipObject


251
252
253
254
255
256
257
258
259
260
# File 'lib/jsonapi/processor.rb', line 251

def replace_to_many_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  data = params.fetch(:data)

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_to_many_links(relationship_type, data)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#replace_to_one_relationshipObject


217
218
219
220
221
222
223
224
225
226
# File 'lib/jsonapi/processor.rb', line 217

def replace_to_one_relationship
  resource_id = params[:resource_id]
  relationship_type = params[:relationship_type].to_sym
  key_value = params[:key_value]

  resource = resource_klass.find_by_key(resource_id, context: context)
  result = resource.replace_to_one_link(relationship_type, key_value)

  return JSONAPI::OperationResult.new(result == :completed ? :no_content : :accepted)
end

#showObject


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jsonapi/processor.rb', line 100

def show
  include_directives = params[:include_directives]
  fields = params[:fields]
  id = params[:id]

  key = resource_klass.verify_key(id, context)

  resource_record = resource_klass.find_by_key(key,
                                               context: context,
                                               include_directives: include_directives,
                                               fields: fields)

  return JSONAPI::ResourceOperationResult.new(:ok, resource_record)
end

126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jsonapi/processor.rb', line 126

def show_related_resource
  source_klass = params[:source_klass]
  source_id = params[:source_id]
  relationship_type = params[:relationship_type].to_sym
  fields = params[:fields]

  source_resource = source_klass.find_by_key(source_id, context: context, fields: fields)

  related_resource = source_resource.public_send(relationship_type)

  return JSONAPI::ResourceOperationResult.new(:ok, related_resource)
end

139
140
141
142
143
144
145
146
147
148
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
# File 'lib/jsonapi/processor.rb', line 139

def show_related_resources
  source_klass = params[:source_klass]
  source_id = params[:source_id]
  relationship_type = params[:relationship_type]
  filters = params[:filters]
  sort_criteria = params[:sort_criteria]
  paginator = params[:paginator]
  fields = params[:fields]

  source_resource ||= source_klass.find_by_key(source_id, context: context, fields: fields)

  related_resources = source_resource.public_send(relationship_type,
                                                  filters:  filters,
                                                  sort_criteria: sort_criteria,
                                                  paginator: paginator,
                                                  fields: fields)

  if ((JSONAPI.configuration.top_level_meta_include_record_count) ||
      (paginator && paginator.class.requires_record_count) ||
      (JSONAPI.configuration.top_level_meta_include_page_count))
    related_resource_records = source_resource.public_send("records_for_" + relationship_type)
    records = resource_klass.filter_records(filters, {},
                                            related_resource_records)

    record_count = resource_klass.count_records(records)
  end

  if (JSONAPI.configuration.top_level_meta_include_page_count && record_count)
    page_count = paginator.calculate_page_count(record_count)
  end

  pagination_params = if paginator && JSONAPI.configuration.top_level_links_include_pagination
                        page_options = {}
                        page_options[:record_count] = record_count if paginator.class.requires_record_count
                        paginator.links_page_params(page_options)
                      else
                        {}
                      end

  opts = {}
  opts.merge!(pagination_params: pagination_params) if JSONAPI.configuration.top_level_links_include_pagination
  opts.merge!(record_count: record_count) if JSONAPI.configuration.top_level_meta_include_record_count
  opts.merge!(page_count: page_count) if JSONAPI.configuration.top_level_meta_include_page_count

  return JSONAPI::RelatedResourcesOperationResult.new(:ok,
                                                      source_resource,
                                                      relationship_type,
                                                      related_resources,
                                                      opts)
end

#show_relationshipObject


115
116
117
118
119
120
121
122
123
124
# File 'lib/jsonapi/processor.rb', line 115

def show_relationship
  parent_key = params[:parent_key]
  relationship_type = params[:relationship_type].to_sym

  parent_resource = resource_klass.find_by_key(parent_key, context: context)

  return JSONAPI::LinksObjectOperationResult.new(:ok,
                                                 parent_resource,
                                                 resource_klass._relationship(relationship_type))
end