class WebsocketRails::TargetValidator

Public Class Methods

validate_target(target) click to toggle source

Parses the target and extracts controller/action pair or raises an error if target is invalid

# File lib/websocket_rails/event_map.rb, line 148
def self.validate_target(target)
  case target
    when Hash
      validate_hash_target target
    when String
      validate_string_target target
  else
    raise('Must specify the event target either as a string product#new_product or as a Hash to: ProductController, with_method: :new_product')
  end
end

Private Class Methods

constantize_controller(controller_string) click to toggle source
# File lib/websocket_rails/event_map.rb, line 177
def self.constantize_controller(controller_string)
  strings = (controller_string << '_controller').split('/')
  strings.map{|string| string.camelize}.join('::').constantize
end
validate_hash_target(target) click to toggle source

Parses the target as a Hash, expecting keys to: and with_method:

# File lib/websocket_rails/event_map.rb, line 162
def self.validate_hash_target(target)
  klass  = target[:to] || raise("Must specify a class for to: option in event route")
  action = target[:with_method] || raise("Must specify a method for with_method: option in event route")
  [klass, action]
end
validate_string_target(target) click to toggle source

Parses the target as a String, expecting it to be in the format “product#new_product”

# File lib/websocket_rails/event_map.rb, line 169
def self.validate_string_target(target)
  strings = target.split('#')
  raise('The string must be in a format like product#new_product') unless strings.count == 2
  klass = constantize_controller strings[0]
  action = strings[1].to_sym
  [klass, action]
end