class UserPreferenceController < ApplicationController
+ before_filter :authorize
+
+ def read
+
+ doc = OSM::API.new.get_xml_doc
+
+ prefs = @user.preferences
+
+ el1 = XML::Node.new 'preferences'
+
+ prefs.each do |pref|
+ el1 << pref.to_xml_node
+ end
+
+ doc.root << el1
+ render :text => doc.to_s, :content_type => "text/xml"
+
+ end
+
+ def update
+ begin
+ p = XML::Parser.new
+ p.string = request.raw_post
+ doc = p.parse
+
+ prefs = []
+
+ keyhash = {}
+
+ doc.find('//preferences/preference').each do |pt|
+ pref = UserPreference.new
+
+ unless keyhash[pt['k']].nil? # already have that key
+ render :text => 'OH NOES! CAN HAS UNIQUE KEYS?', :status => 406
+ return
+ end
+
+ keyhash[pt['k']] = 1
+
+ pref.k = pt['k']
+ pref.v = pt['v']
+ pref.user_id = @user.id
+ prefs << pref
+ end
+
+ if prefs.size > 150
+ render :text => 'Too many preferences', :status => 413
+ return
+ end
+
+ # kill the existing ones
+ UserPreference.delete_all(['user_id = ?', @user.id])
+
+ # save the new ones
+ prefs.each do |pref|
+ pref.save!
+ end
+
+ rescue Exception => ex
+ render :text => 'OH NOES! FAIL!: ' + ex.to_s, :status => 500
+ return
+ end
+
+ render :nothing => true
+ end
+
end
class UserPreference < ActiveRecord::Base
belongs_to :user
+
+ # Turn this Node in to an XML Node without the <osm> wrapper.
+ def to_xml_node
+ el1 = XML::Node.new 'preference'
+ el1['k'] = self.k
+ el1['v'] = self.v
+
+ return el1
+ end
+
end
map.connect "api/#{API_VERSION}/nodes/search", :controller => 'search', :action => 'search_nodes'
map.connect "api/#{API_VERSION}/user/details", :controller => 'user', :action => 'api_details'
+ map.connect "api/#{API_VERSION}/user/preferences", :controller => 'user_preference', :action => 'read', :conditions => { :method => :get }
+ map.connect "api/#{API_VERSION}/user/preferences", :controller => 'user_preference', :action => 'update', :conditions => { :method => :put }
map.connect "api/#{API_VERSION}/user/gpx_files", :controller => 'user', :action => 'api_gpx_files'
map.connect "api/#{API_VERSION}/gpx/create", :controller => 'trace', :action => 'api_create'