Class: JSONAPI::Request
- Inherits:
-
Object
- Object
- JSONAPI::Request
- Includes:
- ResourceFor
- Defined in:
- lib/jsonapi/request.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#include ⇒ Object
Returns the value of attribute include.
-
#operations ⇒ Object
Returns the value of attribute operations.
-
#paginator ⇒ Object
Returns the value of attribute paginator.
-
#resource_klass ⇒ Object
Returns the value of attribute resource_klass.
-
#sort_criteria ⇒ Object
Returns the value of attribute sort_criteria.
-
#source_id ⇒ Object
Returns the value of attribute source_id.
-
#source_klass ⇒ Object
Returns the value of attribute source_klass.
Instance Method Summary collapse
- #check_include(resource_klass, include_parts) ⇒ Object
- #check_sort_criteria(resource_klass, sort_criteria) ⇒ Object
- #format_key(key) ⇒ Object
-
#initialize(params = nil, options = {}) ⇒ Request
constructor
A new instance of Request.
- #parse_add_association_operation(data, association_type, parent_key) ⇒ Object
- #parse_add_operation(data) ⇒ Object
- #parse_fields(fields) ⇒ Object
- #parse_filters(filters) ⇒ Object
- #parse_has_many_links_object(raw) ⇒ Object
- #parse_has_one_links_object(raw) ⇒ Object
- #parse_include(include) ⇒ Object
- #parse_key_array(raw) ⇒ Object
- #parse_pagination(page) ⇒ Object
- #parse_params(params, allowed_fields) ⇒ Object
- #parse_remove_association_operation(params) ⇒ Object
- #parse_remove_operation(params) ⇒ Object
- #parse_replace_operation(data, keys) ⇒ Object
- #parse_single_replace_operation(data, keys) ⇒ Object
- #parse_sort_criteria(sort_criteria) ⇒ Object
- #parse_update_association_operation(data, association_type, parent_key) ⇒ Object
- #setup(params) ⇒ Object
- #unformat_key(key) ⇒ Object
- #unformat_value(attribute, value) ⇒ Object
- #verify_and_remove_type(params) ⇒ Object
- #verify_permitted_params(params, allowed_fields) ⇒ Object
Methods included from ResourceFor
Constructor Details
#initialize(params = nil, options = {}) ⇒ Request
Returns a new instance of Request.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/jsonapi/request.rb', line 12 def initialize(params = nil, = {}) @context = .fetch(:context, nil) @key_formatter = .fetch(:key_formatter, JSONAPI.configuration.key_formatter) @errors = [] @operations = [] @fields = {} @include = [] @filters = {} @sort_criteria = [] @source_klass = nil @source_id = nil setup(params) if params end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def context @context end |
#errors ⇒ Object
Returns the value of attribute errors.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def errors @errors end |
#fields ⇒ Object
Returns the value of attribute fields.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def fields @fields end |
#filters ⇒ Object
Returns the value of attribute filters.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def filters @filters end |
#include ⇒ Object
Returns the value of attribute include.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def include @include end |
#operations ⇒ Object
Returns the value of attribute operations.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def operations @operations end |
#paginator ⇒ Object
Returns the value of attribute paginator.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def paginator @paginator end |
#resource_klass ⇒ Object
Returns the value of attribute resource_klass.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def resource_klass @resource_klass end |
#sort_criteria ⇒ Object
Returns the value of attribute sort_criteria.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def sort_criteria @sort_criteria end |
#source_id ⇒ Object
Returns the value of attribute source_id.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def source_id @source_id end |
#source_klass ⇒ Object
Returns the value of attribute source_klass.
9 10 11 |
# File 'lib/jsonapi/request.rb', line 9 def source_klass @source_klass end |
Instance Method Details
#check_include(resource_klass, include_parts) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jsonapi/request.rb', line 130 def check_include(resource_klass, include_parts) association_name = unformat_key(include_parts.first) association = resource_klass._association(association_name) if association unless include_parts.last.empty? check_include(Resource.resource_for(@resource_klass.module_path + association.class_name.to_s), 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
189 190 191 192 193 194 195 196 197 |
# File 'lib/jsonapi/request.rb', line 189 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
482 483 484 |
# File 'lib/jsonapi/request.rb', line 482 def format_key(key) @key_formatter.format(key) end |
#parse_add_association_operation(data, association_type, parent_key) ⇒ Object
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/jsonapi/request.rb', line 357 def parse_add_association_operation(data, association_type, parent_key) association = resource_klass._association(association_type) if association.is_a?(JSONAPI::Association::HasMany) ids = data.require(:ids) type = data.require(:type) object_params = {links: {association.name => {'type' => type, 'ids' => ids}}} verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context)) @operations.push JSONAPI::CreateHasManyAssociationOperation.new(resource_klass, parent_key, association_type, verified_param_set[:has_many].values[0]) else # :nocov: @errors.concat(JSONAPI::Exceptions::InvalidLinksObject.new(:data).errors) # :nocov: end rescue ActionController::ParameterMissing => e @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors) end |
#parse_add_operation(data) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/jsonapi/request.rb', line 199 def parse_add_operation(data) if data.is_a?(Array) data.each do |p| @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, parse_params(verify_and_remove_type(p), @resource_klass.createable_fields(@context))) end else @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, parse_params(verify_and_remove_type(data), @resource_klass.createable_fields(@context))) end rescue JSONAPI::Exceptions::Error => e @errors.concat(e.errors) end |
#parse_fields(fields) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/jsonapi/request.rb', line 82 def parse_fields(fields) return if fields.nil? extracted_fields = {} # Extract the fields for each type from the fields parameters if fields.is_a?(String) resource_fields = fields.split(',') unless fields.empty? type = @resource_klass._type extracted_fields[type] = resource_fields elsif 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 end # Validate the fields extracted_fields.each do |type, values| underscored_type = unformat_key(type) extracted_fields[type] = [] begin type_resource = self.class.resource_for(@resource_klass.module_path + underscored_type.to_s) rescue NameError @errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors) end if type_resource.nil? || !(@resource_klass._type == underscored_type || @resource_klass._has_association?(underscored_type)) @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
157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/jsonapi/request.rb', line 157 def parse_filters(filters) return unless filters @filters = {} filters.each do |key, value| filter = unformat_key(key).to_sym if @resource_klass._allowed_filter?(filter) @filters[filter] = value else @errors.concat(JSONAPI::Exceptions::FilterNotAllowed.new(filter).errors) end end end |
#parse_has_many_links_object(raw) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/jsonapi/request.rb', line 247 def parse_has_many_links_object(raw) if raw.nil? raise JSONAPI::Exceptions::InvalidLinksObject.new(raw) end links_object = {} if raw.is_a?(Hash) if raw.length != 2 || !(raw.has_key?('type') && raw.has_key?('ids')) || !(raw['ids'].is_a?(Array)) raise JSONAPI::Exceptions::InvalidLinksObject.new(raw) end links_object[raw['type']] = raw['ids'] elsif raw.is_a?(Array) raw.each do |link| link_object = parse_has_one_links_object(link) links_object[link_object[:type]] ||= [] links_object[link_object[:type]].push(link_object[:id]) end else raise JSONAPI::Exceptions::InvalidLinksObject.new(raw) end links_object end |
#parse_has_one_links_object(raw) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/jsonapi/request.rb', line 229 def parse_has_one_links_object(raw) if raw.nil? return { type: nil, id: nil } end if !raw.is_a?(Hash) || raw.length != 2 || !(raw.has_key?('type') && raw.has_key?('id')) raise JSONAPI::Exceptions::InvalidLinksObject.new(raw) end { type: raw['type'], id: raw['id'] } end |
#parse_include(include) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/jsonapi/request.rb', line 144 def parse_include(include) return if include.nil? included_resources = CSV.parse_line(include) return if included_resources.nil? @include = [] included_resources.each do |included_resource| check_include(@resource_klass, included_resource.partition('.')) @include.push(unformat_key(included_resource).to_s) end end |
#parse_key_array(raw) ⇒ Object
478 479 480 |
# File 'lib/jsonapi/request.rb', line 478 def parse_key_array(raw) return @resource_klass.verify_keys(raw.split(/,/), context) end |
#parse_pagination(page) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/jsonapi/request.rb', line 75 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
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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/jsonapi/request.rb', line 270 def parse_params(params, allowed_fields) verify_permitted_params(params, allowed_fields) checked_attributes = {} checked_has_one_associations = {} checked_has_many_associations = {} params.each do |key, value| if key == 'links' || key == :links value.each do |link_key, link_value| param = unformat_key(link_key) association = @resource_klass._association(param) if association.is_a?(JSONAPI::Association::HasOne) links_object = parse_has_one_links_object(link_value) # Since we do not yet support polymorphic associations we will raise an error if the type does not match the # association's type. # ToDo: Support Polymorphic associations if links_object[:type] && (links_object[:type] != association.type.to_s) raise JSONAPI::Exceptions::TypeMismatch.new(links_object[:type]) end unless links_object[:id].nil? association_resource = @resource_klass.resource_for(@resource_klass.module_path + links_object[:type]) checked_has_one_associations[param] = association_resource.verify_key(links_object[:id], @context) else checked_has_one_associations[param] = nil end elsif association.is_a?(JSONAPI::Association::HasMany) links_object = parse_has_many_links_object(link_value) # Since we do not yet support polymorphic associations we will raise an error if the type does not match the # association's type. # ToDo: Support Polymorphic associations if links_object.length == 0 checked_has_many_associations[param] = [] else if links_object.length > 1 || !links_object.has_key?(association.type.to_s) raise JSONAPI::Exceptions::TypeMismatch.new(links_object[:type]) end links_object.each_pair do |type, keys| association_resource = @resource_klass.resource_for(@resource_klass.module_path + type) checked_has_many_associations[param] = association_resource.verify_keys(keys, @context) end end else # :nocov: raise JSONAPI::Exceptions::InvalidLinksObject.new(key) # :nocov: end end else param = unformat_key(key) checked_attributes[param] = unformat_value(param, value) end end return { 'attributes' => checked_attributes, 'has_one' => checked_has_one_associations, 'has_many' => checked_has_many_associations }.deep_transform_keys { |key| unformat_key(key) } end |
#parse_remove_association_operation(params) ⇒ Object
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/jsonapi/request.rb', line 457 def parse_remove_association_operation(params) association_type = params[:association] parent_key = params[resource_klass._as_parent_key] association = resource_klass._association(association_type) if association.is_a?(JSONAPI::Association::HasMany) keys = parse_key_array(params[:keys]) keys.each do |key| @operations.push JSONAPI::RemoveHasManyAssociationOperation.new(resource_klass, parent_key, association_type, key) end else @operations.push JSONAPI::RemoveHasOneAssociationOperation.new(resource_klass, parent_key, association_type) end end |
#parse_remove_operation(params) ⇒ Object
445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/jsonapi/request.rb', line 445 def parse_remove_operation(params) keys = parse_key_array(params.permit(@resource_klass._primary_key)[@resource_klass._primary_key]) keys.each do |key| @operations.push JSONAPI::RemoveResourceOperation.new(@resource_klass, key) end rescue ActionController::UnpermittedParameters => e @errors.concat(JSONAPI::Exceptions::ParametersNotAllowed.new(e.params).errors) rescue JSONAPI::Exceptions::Error => e @errors.concat(e.errors) end |
#parse_replace_operation(data, keys) ⇒ Object
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/jsonapi/request.rb', line 428 def parse_replace_operation(data, keys) if data.is_a?(Array) if keys.count != data.count raise JSONAPI::Exceptions::CountMismatch end data.each do |object_params| parse_single_replace_operation(object_params, keys) end else parse_single_replace_operation(data, [keys]) end rescue JSONAPI::Exceptions::Error => e @errors.concat(e.errors) end |
#parse_single_replace_operation(data, keys) ⇒ Object
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/jsonapi/request.rb', line 403 def parse_single_replace_operation(data, keys) if data[@resource_klass._primary_key].nil? raise JSONAPI::Exceptions::MissingKey.new end type = data[:type] if type.nil? || type != @resource_klass._type.to_s raise JSONAPI::Exceptions::ParameterMissing.new(:type) end key = data[@resource_klass._primary_key] if !keys.include?(key) raise JSONAPI::Exceptions::KeyNotIncludedInURL.new(key) end if !keys.include?(@resource_klass._primary_key) data.delete(:id) end @operations.push(JSONAPI::ReplaceFieldsOperation.new(@resource_klass, key, parse_params(verify_and_remove_type(data), @resource_klass.updateable_fields(@context)))) end |
#parse_sort_criteria(sort_criteria) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/jsonapi/request.rb', line 170 def parse_sort_criteria(sort_criteria) return unless sort_criteria @sort_criteria = CSV.parse_line(sort_criteria).collect do |sort| sort_criteria = {field: unformat_key(sort[1..-1]).to_s} if sort.start_with?('+') sort_criteria[:direction] = :asc elsif sort.start_with?('-') sort_criteria[:direction] = :desc else @errors.concat(JSONAPI::Exceptions::InvalidSortFormat .new(format_key(resource_klass._type), sort).errors) end check_sort_criteria(@resource_klass, sort_criteria) sort_criteria end end |
#parse_update_association_operation(data, association_type, parent_key) ⇒ Object
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/jsonapi/request.rb', line 380 def parse_update_association_operation(data, association_type, parent_key) association = resource_klass._association(association_type) if association.is_a?(JSONAPI::Association::HasOne) object_params = {links: {association.name => data}} verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context)) @operations.push JSONAPI::ReplaceHasOneAssociationOperation.new(resource_klass, parent_key, association_type, verified_param_set[:has_one].values[0]) else object_params = {links: {association.name => data}} verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context)) @operations.push JSONAPI::ReplaceHasManyAssociationOperation.new(resource_klass, parent_key, association_type, verified_param_set[:has_many].values[0]) end end |
#setup(params) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/jsonapi/request.rb', line 27 def setup(params) @resource_klass ||= self.class.resource_for(params[:controller]) if params[:controller] unless params.nil? case params[:action] when 'index' parse_fields(params[:fields]) parse_include(params[:include]) parse_filters(params[:filter]) parse_sort_criteria(params[:sort]) parse_pagination(params[:page]) when 'get_related_resource', 'get_related_resources' @source_klass = self.class.resource_for(params.require(:source)) @source_id = params.require(@source_klass._as_parent_key) parse_fields(params[:fields]) parse_include(params[:include]) parse_filters(params[:filter]) parse_sort_criteria(params[:sort]) parse_pagination(params[:page]) when 'show' parse_fields(params[:fields]) parse_include(params[:include]) when 'create' parse_fields(params[:fields]) parse_include(params[:include]) parse_add_operation(params.require(:data)) when 'create_association' parse_add_association_operation(params.require(:data), params.require(:association), params.require(@resource_klass._as_parent_key)) when 'update_association' parse_update_association_operation(params.require(:data), params.require(:association), params.require(@resource_klass._as_parent_key)) when 'update' parse_fields(params[:fields]) parse_include(params[:include]) parse_replace_operation(params.require(:data), params.require(@resource_klass._primary_key)) when 'destroy' parse_remove_operation(params) when 'destroy_association' parse_remove_association_operation(params) end end rescue ActionController::ParameterMissing => e @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors) end |
#unformat_key(key) ⇒ Object
486 487 488 |
# File 'lib/jsonapi/request.rb', line 486 def unformat_key(key) @key_formatter.unformat(key) end |
#unformat_value(attribute, value) ⇒ Object
337 338 339 340 |
# File 'lib/jsonapi/request.rb', line 337 def unformat_value(attribute, value) value_formatter = JSONAPI::ValueFormatter.value_formatter_for(@resource_klass.(attribute)[:format]) value_formatter.unformat(value, @context) end |
#verify_and_remove_type(params) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/jsonapi/request.rb', line 215 def verify_and_remove_type(params) #remove type and verify it matches the resource if params[:type] == @resource_klass._type.to_s params.delete(:type) else if params[:type].nil? raise JSONAPI::Exceptions::ParameterMissing.new(:type) else raise JSONAPI::Exceptions::InvalidResource.new(params[:type]) end end params end |
#verify_permitted_params(params, allowed_fields) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/jsonapi/request.rb', line 342 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| if key == 'links' || key == :links value.each_key do |links_key| params_not_allowed.push(links_key) unless formatted_allowed_fields.include?(links_key.to_sym) end else params_not_allowed.push(key) unless formatted_allowed_fields.include?(key.to_sym) end end raise JSONAPI::Exceptions::ParametersNotAllowed.new(params_not_allowed) if params_not_allowed.length > 0 end |