Class: TentD::API::Profile::Patch

Inherits:
Middleware show all
Defined in:
lib/tentd/api/profile.rb

Instance Method Summary collapse

Methods inherited from Middleware

#call, #initialize

Methods included from Authorizable

#authorize_env!, #authorize_env?

Constructor Details

This class inherits a constructor from TentD::API::Middleware

Instance Method Details

#action(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/tentd/api/profile.rb', line 31

def action(env)
  diff_array = env.params[:data]
  profile_hash = env.delete(:response)
  new_profile_hash = Marshal.load(Marshal.dump(profile_hash)).to_hash # equivalent of recursive dup
  JsonPatch.merge(new_profile_hash, diff_array)
  if new_profile_hash != profile_hash
    env.updated_info = new_profile_hash.map do |type, data|
      Model::ProfileInfo.update_profile(type, data)
    end
  end
  env
rescue JsonPatch::ObjectNotFound, JsonPatch::ObjectExists => e
  env['response.status'] = 422
  env
end