Guest User

Untitled

a guest
Oct 15th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. # This is the base class for all Action processing classes. This is somewhat
  2. # similar in concept to the Rails Controller/Action, and how the Rails router
  3. # knows by convention what class/method to call.
  4. class ActionBase
  5. # Store all inherited classes.
  6. def self.inherited(klass)
  7. @actions ||= {}
  8. @actions[klass.name] = klass
  9. end
  10.  
  11. class << self
  12.  
  13. #
  14. def route_to_action(name, data, connection)
  15. klass = @actions[name]
  16. action = klass.new(connection)
  17. catch(:end_action) {
  18. action.process(data.with_indifferent_access)
  19. }
  20. return action.response
  21. end
  22. end
  23.  
  24. attr_accessor :connection
  25.  
  26. # @param [EM::Connection] connection The connection that sent the message
  27. def initialize(connection)
  28. @connection = connection
  29. end
  30.  
  31. # This is very much like the Controller#render in Rails
  32. def respond(message)
  33. @response = message
  34. throw(:end_action)
  35. end
  36.  
  37. # This is similar to a rendered view ready to be sent back to the Client
  38. def response
  39. return nil unless @response
  40.  
  41. klass = self.class.name
  42. klass = klass[0].downcase + klass[1..-1]
  43. response_class = klass.gsub("Message", "Response")
  44.  
  45. return @response.merge(:class => response_class, :status_code => @status_code)
  46. end
  47. end
Add Comment
Please, Sign In to add comment