module ApplicationCable
  class Connection < ActionCable::Connection::Base
  	identified_by :current_subdomain
  	def connect
      if ENV["server_mode"] == "cloud"
    		self.current_subdomain = verified_subdomain
      end
  		logger.add_tags "SX-ActionCable"
  	end

  	private 
  	def verified_subdomain            
      from = request.subdomain.downcase  + "." + request.domain.downcase    
      file_path = "config/shops.json"
      shop_data = File.read(file_path)

      shop_json = JSON.parse(shop_data) 
      shop_json["data"].each do |j|
        if j["lookup"] == from      
          verified_subdomain = from
        else
          reject_unauthorized_connection
        end
      end        
    end 	
  end
end
