Class: JSONAPI::Request

Inherits:
Object
  • Object
show all
Includes:
ResourceFor
Defined in:
lib/jsonapi/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ResourceFor

included

Constructor Details

#initialize(context = nil, params = nil) ⇒ Request

Returns a new instance of Request.



10
11
12
13
14
15
16
17
18
19
# File 'lib/jsonapi/request.rb', line 10

def initialize(context = nil, params = nil)
  @context = context
  @errors = []
  @operations = []
  @fields = {}
  @includes = []
  @filters = {}

  setup(params) if params
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def context
  @context
end

#errorsObject

Returns the value of attribute errors.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def errors
  @errors
end

#fieldsObject

Returns the value of attribute fields.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def fields
  @fields
end

#filtersObject

Returns the value of attribute filters.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def filters
  @filters
end

#includesObject

Returns the value of attribute includes.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def includes
  @includes
end

#operationsObject

Returns the value of attribute operations.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def operations
  @operations
end

#resource_klassObject

Returns the value of attribute resource_klass.



8
9
10
# File 'lib/jsonapi/request.rb', line 8

def resource_klass
  @resource_klass
end

Instance Method Details

#parse_add_association_operation(params) ⇒ Object



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
# File 'lib/jsonapi/request.rb', line 145

def parse_add_association_operation(params)
  association_type = params[:association].to_sym

  parent_key = params[resource_klass._as_parent_key]

  if params[association_type].nil?
    raise ActionController::ParameterMissing.new(association_type)
  end

  object_params = {links: {association_type => params[association_type]}}
  verified_param_set = @resource_klass.verify_update_params(object_params, @context)

  association = resource_klass._association(association_type)

  if association.is_a?(JSONAPI::Association::HasOne)
    @operations.push JSONAPI::ReplaceHasOneAssociationOperation.new(resource_klass,
                                                                      parent_key,
                                                                      association_type,
                                                                      verified_param_set[:has_one].values[0])
  else
    @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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/jsonapi/request.rb', line 126

def parse_add_operation(params)
  object_params_raw = params.require(@resource_klass._type)

  if object_params_raw.is_a?(Array)
    object_params_raw.each do |p|
      @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
                                                              @resource_klass.verify_create_params(p, @context))
    end
  else
    @operations.push JSONAPI::CreateResourceOperation.new(@resource_klass,
                                                            @resource_klass.verify_create_params(object_params_raw,
                                                                                                 @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



54
55
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
# File 'lib/jsonapi/request.rb', line 54

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(',').map {|s| s.to_sym } unless value.nil? || value.empty?
      type = @resource_klass._serialize_as
      fields[type] = resource_fields
    elsif params[:fields].is_a?(ActionController::Parameters)
      params[:fields].each do |param, value|
        resource_fields = value.split(',').map {|s| s.to_sym } unless value.nil? || value.empty?
        type = param.to_sym
        fields[type] = resource_fields
      end
    end
  end

  # Validate the fields
  fields.each do |type, values|
    fields[type] = []
    type_resource = self.class.resource_for(type)
    if type_resource.nil? || !(@resource_klass._type == type || @resource_klass._has_association?(type))
      @errors.concat(JSONAPI::Exceptions::InvalidResource.new(type).errors)
    else
      unless values.nil?
        values.each do |field|
          if type_resource._validate_field(field)
            fields[type].push 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
end

#parse_filters(params) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/jsonapi/request.rb', line 104

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

    if [:include, :fields, :format, :controller, :action, :sort].include?(filter)
      # Ignore non-filter parameters
    elsif @resource_klass._allowed_filter?(filter)
      filters[filter] = value
    else
      @errors.concat(JSONAPI::Exceptions::FilterNotAllowed.new(filter).errors)
    end
  end
  @filters = filters
end

#parse_includes(params) ⇒ Object



97
98
99
100
101
102
# File 'lib/jsonapi/request.rb', line 97

def parse_includes(params)
  includes = params[:include]
  included_resources = []
  included_resources += CSV.parse_line(includes) unless includes.nil? || includes.empty?
  @includes = included_resources
end

#parse_key_array(raw) ⇒ Object



246
247
248
249
250
251
252
# File 'lib/jsonapi/request.rb', line 246

def parse_key_array(raw)
  keys = []
  raw.split(/,/).collect do |key|
    keys.push @resource_klass.verify_key(key, context)
  end
  return keys
end

#parse_remove_association_operation(params) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/jsonapi/request.rb', line 225

def parse_remove_association_operation(params)
  association_type = params[:association].to_sym

  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



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/jsonapi/request.rb', line 213

def parse_remove_operation(params)
  keys = parse_key_array(params.permit(@resource_klass._key)[@resource_klass._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



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
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/jsonapi/request.rb', line 174

def parse_replace_operation(params)
  object_params_raw = params.require(@resource_klass._type)

  keys = params[@resource_klass._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._key].nil?
        raise JSONAPI::Exceptions::MissingKey.new
      end

      if !keys.include?(object_params[@resource_klass._key])
        raise JSONAPI::Exceptions::KeyNotIncludedInURL.new(object_params[@resource_klass._key])
      end
      @operations.push JSONAPI::ReplaceFieldsOperation.new(@resource_klass,
                                                             object_params[@resource_klass._key],
                                                             @resource_klass.verify_update_params(object_params,
                                                                                                  @context))
    end
  else
    if !object_params_raw[@resource_klass._key].nil? && keys != object_params_raw[@resource_klass._key]
      raise JSONAPI::Exceptions::KeyNotIncludedInURL.new(object_params_raw[@resource_klass._key])
    end

    @operations.push JSONAPI::ReplaceFieldsOperation.new(@resource_klass,
                                                           params[@resource_klass._key],
                                                           @resource_klass.verify_update_params(object_params_raw,
                                                                                                @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

#setup(params) ⇒ Object



21
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
# File 'lib/jsonapi/request.rb', line 21

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_includes(params)
        parse_filters(params)
      when 'show_associations'
      when 'show'
        parse_fields(params)
        parse_includes(params)
      when 'create'
        parse_fields(params)
        parse_includes(params)
        parse_add_operation(params)
      when 'create_association'
        parse_fields(params)
        parse_includes(params)
        parse_add_association_operation(params)
      when 'update'
        parse_fields(params)
        parse_includes(params)
        parse_replace_operation(params)
      when 'destroy'
        parse_remove_operation(params)
      when 'destroy_association'
        parse_remove_association_operation(params)
    end
  end
end