Class: CableX::Cable::Connection
- Inherits:
-
ActionCable::Connection::Base
- Object
- ActionCable::Connection::Base
- CableX::Cable::Connection
- Defined in:
- lib/cable_x/cable/connection.rb
Overview
Connection class to serve entry point
Class Attribute Summary collapse
-
.rate_limit ⇒ Object
Returns the value of attribute rate_limit.
-
.redis_config ⇒ Object
Returns the value of attribute redis_config.
Instance Attribute Summary collapse
-
#device_id ⇒ Object
Returns the value of attribute device_id.
-
#rate_limit ⇒ Object
Returns the value of attribute rate_limit.
-
#redis ⇒ Object
Returns the value of attribute redis.
-
#redis_config ⇒ Object
Returns the value of attribute redis_config.
Instance Method Summary collapse
- #check_message(message) ⇒ Object
- #close(reason, reconnect = false) ⇒ Object
- #connect ⇒ Object
- #malformed_message_response ⇒ Object
- #receive(message) ⇒ Object
- #request_str ⇒ Object
- #setup_rate_limit ⇒ Object
Methods included from RateLimit::BlockCheck
#block_device, #check_block, #check_block_unit, #disconnect_client
Methods included from RateLimit::CheckRateLimit
#check_rate_limit, #check_unit, #do_rate_limit
Methods included from RateLimit::RateLimitRedis
#block_device_key, #device_key, #redis_exists, #redis_get, #redis_incr, #redis_set
Class Attribute Details
.rate_limit ⇒ Object
Returns the value of attribute rate_limit.
17 18 19 |
# File 'lib/cable_x/cable/connection.rb', line 17 def rate_limit @rate_limit end |
.redis_config ⇒ Object
Returns the value of attribute redis_config.
17 18 19 |
# File 'lib/cable_x/cable/connection.rb', line 17 def redis_config @redis_config end |
Instance Attribute Details
#device_id ⇒ Object
Returns the value of attribute device_id.
20 21 22 |
# File 'lib/cable_x/cable/connection.rb', line 20 def device_id @device_id end |
#rate_limit ⇒ Object
Returns the value of attribute rate_limit.
20 21 22 |
# File 'lib/cable_x/cable/connection.rb', line 20 def rate_limit @rate_limit end |
#redis ⇒ Object
Returns the value of attribute redis.
20 21 22 |
# File 'lib/cable_x/cable/connection.rb', line 20 def redis @redis end |
#redis_config ⇒ Object
Returns the value of attribute redis_config.
20 21 22 |
# File 'lib/cable_x/cable/connection.rb', line 20 def redis_config @redis_config end |
Instance Method Details
#check_message(message) ⇒ Object
52 53 54 |
# File 'lib/cable_x/cable/connection.rb', line 52 def () JSON.parse() rescue false end |
#close(reason, reconnect = false) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/cable_x/cable/connection.rb', line 40 def close(reason, reconnect = false) transmit(type: ActionCable::INTERNAL[:message_types][:disconnect], reason: reason, reconnect: reconnect) websocket.close end |
#connect ⇒ Object
23 24 25 26 27 28 |
# File 'lib/cable_x/cable/connection.rb', line 23 def connect self.device_id = Digest::SHA1.hexdigest(request_str) setup_rate_limit rescue StandardError => _e end |
#malformed_message_response ⇒ Object
47 48 49 50 |
# File 'lib/cable_x/cable/connection.rb', line 47 def transmit(type: 'error', message: 'malformed_message') end |
#receive(message) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/cable_x/cable/connection.rb', line 30 def receive() if (blocked_unit = check_block) || (blocked_unit = do_rate_limit) disconnect_client(blocked_unit) elsif () super() else end end |
#request_str ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/cable_x/cable/connection.rb', line 66 def request_str headers_to_get = %w[SERVER_PROTOCOL SERVER_SOFTWARE REQUEST_METHOD REQUEST_PATH HTTP_USER_AGENT\ HTTP_CONNECTION HTTP_ORIGIN REMOTE_ADDR ORIGINAL_FULLPATH ORIGINAL_SCRIPT_NAME\ action_dispatch.authorized_host] headers = request.headers.to_h headers_to_get.map { |key| headers[key] }.join('-').gsub(' ', '') end |
#setup_rate_limit ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/cable_x/cable/connection.rb', line 56 def setup_rate_limit self.rate_limit = self.class.rate_limit self.redis_config = self.class.redis_config if rate_limit self.redis = Redis.new(url: redis_config[:url]) raise StandardError, 'Rate limiting: Slow down!. Try after some time' if check_block end true end |