1 # Update and read user preferences, which are arbitrayr key/val pairs
2 class UserPreferenceController < ApplicationController
3 before_filter :authorize
6 pref = UserPreference.find(@user.id, params[:preference_key])
8 render :text => pref.v.to_s
9 rescue ActiveRecord::RecordNotFound => ex
10 render :text => 'OH NOES! PREF NOT FOUND!', :status => :not_found
15 pref = UserPreference.find(@user.id, params[:preference_key])
16 pref.v = request.raw_post.chomp
18 rescue ActiveRecord::RecordNotFound
19 pref = UserPreference.new
21 pref.k = params[:preference_key]
22 pref.v = request.raw_post.chomp
26 render :nothing => true
30 UserPreference.delete(@user.id, params[:preference_key])
32 render :nothing => true
33 rescue ActiveRecord::RecordNotFound => ex
34 render :text => "param: #{params[:preference_key]} not found", :status => :not_found
37 # print out all the preferences as a big xml block
39 doc = OSM::API.new.get_xml_doc
41 prefs = @user.preferences
43 el1 = XML::Node.new 'preferences'
46 el1 << pref.to_xml_node
50 render :text => doc.to_s, :content_type => "text/xml"
53 # update the entire set of preferences
56 p = XML::Parser.string(request.raw_post)
57 rescue LibXML::XML::Error, ArgumentError => ex
58 raise OSM::APIBadXMLError.new("preferences", xml, ex.message)
66 doc.find('//preferences/preference').each do |pt|
67 pref = UserPreference.new
69 unless keyhash[pt['k']].nil? # already have that key
70 render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => :not_acceptable
77 pref.user_id = @user.id
82 render :text => 'Too many preferences', :status => :request_entity_too_large
85 # kill the existing ones
86 UserPreference.delete_all(['user_id = ?', @user.id])
92 render :nothing => true
94 rescue Exception => ex
95 render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => :internal_server_error