Guest User

Untitled

a guest
Jun 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. ## API controller
  2.  
  3. class ApiController < ApplicationController
  4. prepend_around_filter ApiAuthorizedFilter.new
  5.  
  6. def characters_training
  7. current_user.characters.each do |character|
  8. if character.skill_in_training
  9. output += character.skill_in_training.to_xml
  10. end
  11. logger.info "Character: #{character.name}, skill in training: #{character.skill_in_training}"
  12. end
  13. respond_to do |format|
  14. format.xml { render :xml => output }
  15. format.json { render :json => current_user.characters.to_json }
  16. end
  17. end
  18. end
  19.  
  20.  
  21.  
  22. ## Route in Routes.rb
  23.  
  24. map.connect '/api/:action/:format/:api_key', :controller => 'api'
  25.  
  26.  
  27. ## lib/api_authorized_filter.rb
  28.  
  29. # Use this filter as an around_filter around actions that can be
  30. # accessed via the API.
  31. #
  32. # Example:
  33. # class ItemsController < ApplicationController
  34. # prepend_around_filter ApiAuthorizedFilter.new, :only => [:create]
  35. # end
  36. #
  37. class ApiAuthorizedFilter
  38. def before(controller)
  39. return true unless controller.params[:api_key]
  40. controller.current_user = User.find_by_api_key(controller.params[:api_key])
  41. end
  42.  
  43. def after(controller)
  44. controller.current_user = nil
  45. end
  46. end
Add Comment
Please, Sign In to add comment