Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## API controller
- class ApiController < ApplicationController
- prepend_around_filter ApiAuthorizedFilter.new
- def characters_training
- current_user.characters.each do |character|
- if character.skill_in_training
- output += character.skill_in_training.to_xml
- end
- logger.info "Character: #{character.name}, skill in training: #{character.skill_in_training}"
- end
- respond_to do |format|
- format.xml { render :xml => output }
- format.json { render :json => current_user.characters.to_json }
- end
- end
- end
- ## Route in Routes.rb
- map.connect '/api/:action/:format/:api_key', :controller => 'api'
- ## lib/api_authorized_filter.rb
- # Use this filter as an around_filter around actions that can be
- # accessed via the API.
- #
- # Example:
- # class ItemsController < ApplicationController
- # prepend_around_filter ApiAuthorizedFilter.new, :only => [:create]
- # end
- #
- class ApiAuthorizedFilter
- def before(controller)
- return true unless controller.params[:api_key]
- controller.current_user = User.find_by_api_key(controller.params[:api_key])
- end
- def after(controller)
- controller.current_user = nil
- end
- end
Add Comment
Please, Sign In to add comment