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.
-
#resource_klass ⇒ Object
Returns the value of attribute resource_klass.
-
#sort_params ⇒ Object
Returns the value of attribute sort_params.
Instance Method Summary collapse
- #check_include(resource_klass, include_parts) ⇒ Object
- #check_sort_param(resource_klass, sort_param) ⇒ Object
- #format_key(key) ⇒ Object
-
#initialize(params = nil, options = {}) ⇒ Request
constructor
A new instance of Request.
- #parse_add_association_operation(params) ⇒ Object
- #parse_add_operation(params) ⇒ Object
- #parse_fields(params) ⇒ Object
- #parse_filters(params) ⇒ Object
- #parse_include(params) ⇒ Object
- #parse_key_array(raw) ⇒ Object
- #parse_params(params, allowed_fields) ⇒ Object
- #parse_remove_association_operation(params) ⇒ Object
- #parse_remove_operation(params) ⇒ Object
- #parse_replace_operation(params) ⇒ Object
- #parse_sort_params(params) ⇒ Object
- #parse_update_association_operation(params) ⇒ Object
- #setup(params) ⇒ Object
- #unformat_key(key) ⇒ Object
- #unformat_value(attribute, value) ⇒ 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.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/jsonapi/request.rb', line 10 def initialize(params = nil, = {}) @context = .fetch(:context, nil) @key_formatter = .fetch(:key_formatter, JSONAPI.configuration.key_formatter) @errors = [] @operations = [] @fields = {} @include = [] @filters = {} setup(params) if params end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def context @context end |
#errors ⇒ Object
Returns the value of attribute errors.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def errors @errors end |
#fields ⇒ Object
Returns the value of attribute fields.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def fields @fields end |
#filters ⇒ Object
Returns the value of attribute filters.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def filters @filters end |
#include ⇒ Object
Returns the value of attribute include.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def include @include end |
#operations ⇒ Object
Returns the value of attribute operations.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def operations @operations end |
#resource_klass ⇒ Object
Returns the value of attribute resource_klass.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def resource_klass @resource_klass end |
#sort_params ⇒ Object
Returns the value of attribute sort_params.
8 9 10 |
# File 'lib/jsonapi/request.rb', line 8 def sort_params @sort_params end |
Instance Method Details
#check_include(resource_klass, include_parts) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/jsonapi/request.rb', line 106 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_param(resource_klass, sort_param) ⇒ Object
174 175 176 177 178 179 180 181 182 |
# File 'lib/jsonapi/request.rb', line 174 def check_sort_param(resource_klass, sort_param) sort_param = sort_param.sub(/\A-/, '') sortable_fields = resource_klass.sortable_fields(context) unless sortable_fields.include? sort_param.to_sym @errors.concat(JSONAPI::Exceptions::InvalidSortParam .new(format_key(resource_klass._type), sort_param).errors) end end |
#format_key(key) ⇒ Object
407 408 409 |
# File 'lib/jsonapi/request.rb', line 407 def format_key(key) @key_formatter.format(key) end |
#parse_add_association_operation(params) ⇒ Object
257 258 259 260 261 262 263 264 265 266 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 |
# File 'lib/jsonapi/request.rb', line 257 def parse_add_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::HasOne) plural_association_type = association_type.pluralize if params[plural_association_type].nil? raise ActionController::ParameterMissing.new(plural_association_type) end object_params = {links: {association_type => params[plural_association_type]}} verified_param_set = parse_params(object_params, @resource_klass.updateable_fields(@context)) @operations.push JSONAPI::CreateHasOneAssociationOperation.new(resource_klass, parent_key, association_type, verified_param_set[:has_one].values[0]) else if params[association_type].nil? raise ActionController::ParameterMissing.new(association_type) end object_params = {links: {association_type => params[association_type]}} 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]) end rescue ActionController::ParameterMissing => e @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors) end |
#parse_add_operation(params) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/jsonapi/request.rb', line 184 def parse_add_operation(params) object_params_raw = params.require(format_key(@resource_klass._type)) if object_params_raw.is_a?(Array) object_params_raw.each do |p| @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, parse_params(p, @resource_klass.createable_fields(@context))) end else @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass, parse_params(object_params_raw, @resource_klass.createable_fields(@context))) end rescue ActionController::ParameterMissing => e @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors) rescue JSONAPI::Exceptions::Error => e @errors.concat(e.errors) end |
#parse_fields(params) ⇒ Object
56 57 58 59 60 61 62 63 64 65 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 99 100 101 102 103 104 |
# File 'lib/jsonapi/request.rb', line 56 def parse_fields(params) fields = {} # Extract the fields for each type from the fields parameters unless params[:fields].nil? if params[:fields].is_a?(String) value = params[:fields] resource_fields = value.split(',') unless value.nil? || value.empty? type = @resource_klass._type fields[type] = resource_fields elsif params[:fields].is_a?(ActionController::Parameters) params[:fields].each do |param, value| resource_fields = value.split(',') unless value.nil? || value.empty? type = param fields[type] = resource_fields end end end # Validate the fields fields.each do |type, values| underscored_type = unformat_key(type) 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) 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 = fields.deep_transform_keys{ |key| unformat_key(key) } end |
#parse_filters(params) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/jsonapi/request.rb', line 130 def parse_filters(params) # Coerce :ids -> :id if params[:ids] params[:id] = params[:ids] params.delete(:ids) end filters = {} params.each do |key, value| filter = key.to_sym # Ignore non-filter parameters next if JSONAPI.configuration.allowed_request_params.include?(filter) 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 @filters = filters end |
#parse_include(params) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/jsonapi/request.rb', line 120 def parse_include(params) included_resources_raw = CSV.parse_line(params[:include]) unless params[:include].nil? || params[:include].empty? @include = [] return if included_resources_raw.nil? included_resources_raw.each do |include| check_include(@resource_klass, include.partition('.')) @include.push(unformat_key(include).to_s) end end |
#parse_key_array(raw) ⇒ Object
403 404 405 |
# File 'lib/jsonapi/request.rb', line 403 def parse_key_array(raw) return @resource_klass.verify_keys(raw.split(/,/), context) end |
#parse_params(params, allowed_fields) ⇒ Object
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/jsonapi/request.rb', line 202 def parse_params(params, allowed_fields) # push links into top level param list with attributes in order to check for invalid params if params[:links] params[:links].each do |link, value| params[link] = value end params.delete(:links) end verify_permitted_params(params, allowed_fields) checked_attributes = {} checked_has_one_associations = {} checked_has_many_associations = {} params.each do |key, value| param = unformat_key(key) association = @resource_klass._association(param) if association.is_a?(JSONAPI::Association::HasOne) checked_has_one_associations[param] = @resource_klass.resource_for(@resource_klass.module_path + association.type.to_s).verify_key(value, @context) elsif association.is_a?(JSONAPI::Association::HasMany) keys = [] if value.is_a?(Array) keys = @resource_klass.resource_for(@resource_klass.module_path + association.type.to_s).verify_keys(value, @context) else keys.push(@resource_klass.resource_for(@resource_klass.module_path + association.type.to_s).verify_key(value, @context)) end checked_has_many_associations[param] = keys else 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
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 382 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
370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/jsonapi/request.rb', line 370 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(params) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/jsonapi/request.rb', line 333 def parse_replace_operation(params) object_params_raw = params.require(format_key(@resource_klass._type)) keys = params[@resource_klass._primary_key] if object_params_raw.is_a?(Array) if keys.count != object_params_raw.count raise JSONAPI::Exceptions::CountMismatch end object_params_raw.each do |object_params| if object_params[@resource_klass._primary_key].nil? raise JSONAPI::Exceptions::MissingKey.new end if !keys.include?(object_params[@resource_klass._primary_key]) raise JSONAPI::Exceptions::KeyNotIncludedInURL.new(object_params[@resource_klass._primary_key]) end @operations.push JSONAPI::ReplaceFieldsOperation.new(@resource_klass, object_params[@resource_klass._primary_key], parse_params(object_params, @resource_klass.updateable_fields(@context))) end else if !object_params_raw[@resource_klass._primary_key].nil? && keys != object_params_raw[@resource_klass._primary_key] raise JSONAPI::Exceptions::KeyNotIncludedInURL.new(object_params_raw[@resource_klass._primary_key]) end @operations.push JSONAPI::ReplaceFieldsOperation.new(@resource_klass, params[@resource_klass._primary_key], parse_params(object_params_raw, @resource_klass.updateable_fields(@context))) end rescue ActionController::ParameterMissing => e @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors) rescue JSONAPI::Exceptions::Error => e @errors.concat(e.errors) end |
#parse_sort_params(params) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/jsonapi/request.rb', line 154 def parse_sort_params(params) @sort_params = if params[:sort].present? CSV.parse_line(params[:sort]).collect do |sort_param| # A parameter name may not start with a dash # We need to preserve the dash as the sort direction before we unformat the string if sort_param.start_with?('-') unformatted_sort_param = unformat_key(sort_param[1..-1]).to_s unformatted_sort_param.prepend('-') else unformatted_sort_param = unformat_key(sort_param).to_s end check_sort_param(@resource_klass, unformatted_sort_param) unformatted_sort_param end else [] end end |
#parse_update_association_operation(params) ⇒ Object
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 |
# File 'lib/jsonapi/request.rb', line 295 def parse_update_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::HasOne) plural_association_type = association_type.pluralize if params[plural_association_type].nil? raise ActionController::ParameterMissing.new(plural_association_type) end object_params = {links: {association_type => params[plural_association_type]}} 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 if params[association_type].nil? raise ActionController::ParameterMissing.new(association_type) end object_params = {links: {association_type => params[association_type]}} 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 rescue ActionController::ParameterMissing => e @errors.concat(JSONAPI::Exceptions::ParameterMissing.new(e.param).errors) end |
#setup(params) ⇒ Object
22 23 24 25 26 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 |
# File 'lib/jsonapi/request.rb', line 22 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) parse_include(params) parse_filters(params) parse_sort_params(params) when 'show_associations' when 'show' parse_fields(params) parse_include(params) when 'create' parse_fields(params) parse_include(params) parse_add_operation(params) when 'create_association' parse_add_association_operation(params) when 'update_association' parse_update_association_operation(params) when 'update' parse_fields(params) parse_include(params) parse_replace_operation(params) when 'destroy' parse_remove_operation(params) when 'destroy_association' parse_remove_association_operation(params) end end end |
#unformat_key(key) ⇒ Object
411 412 413 |
# File 'lib/jsonapi/request.rb', line 411 def unformat_key(key) @key_formatter.unformat(key) end |
#unformat_value(attribute, value) ⇒ Object
243 244 245 246 |
# File 'lib/jsonapi/request.rb', line 243 def unformat_value(attribute, value) value_formatter = JSONAPI::ValueFormatter.value_formatter_for(@resource_klass.(attribute)[:format]) value_formatter.unformat(value, @context) end |
#verify_permitted_params(params, allowed_fields) ⇒ Object
248 249 250 251 252 253 254 255 |
# File 'lib/jsonapi/request.rb', line 248 def verify_permitted_params(params, allowed_fields) formatted_allowed_fields = allowed_fields.collect {|field| format_key(field).to_sym} params_not_allowed = [] params.keys.each do |key| params_not_allowed.push(key) unless formatted_allowed_fields.include?(key.to_sym) end raise JSONAPI::Exceptions::ParametersNotAllowed.new(params_not_allowed) if params_not_allowed.length > 0 end |