Skip to content

Commit

Permalink
Added ablyextensions to check for
Browse files Browse the repository at this point in the history
1. nil or empty string
2. fetch with default value for hash
  • Loading branch information
sacOO7 committed Jun 10, 2024
1 parent cb9c2cf commit f470f3d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
11 changes: 6 additions & 5 deletions lib/ably/realtime/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Client
include Ably::Modules::Conversions

extend Forwardable
using Ably::Util::AblyExtensions

DOMAIN = 'realtime.ably.io'

Expand Down Expand Up @@ -121,17 +122,17 @@ def initialize(options)
acc[key.to_s] = value.to_s
end
@rest_client = Ably::Rest::Client.new(options.merge(realtime_client: self))
@echo_messages = rest_client.options.fetch(:echo_messages, true)
@queue_messages = rest_client.options.fetch(:queue_messages, true)
@echo_messages = rest_client.options.fetch_or_default(:echo_messages, true)
@queue_messages = rest_client.options.fetch_or_default(:queue_messages, true)
@custom_realtime_host = rest_client.options[:realtime_host] || rest_client.options[:ws_host]
@auto_connect = rest_client.options.fetch(:auto_connect, true)
@recover = rest_client.options.fetch(:recover, '')
@auto_connect = rest_client.options.fetch_or_default(:auto_connect, true)
@recover = rest_client.options.fetch_or_default(:recover, '')

@auth = Ably::Realtime::Auth.new(self)
@channels = Ably::Realtime::Channels.new(self)
@connection = Ably::Realtime::Connection.new(self, options)

unless @recover.empty?
unless @recover.nil_or_empty?
recovery_context = RecoveryKeyContext.from_json(@recover, logger)
unless recovery_context.nil?
@channels.set_channel_serials recovery_context.channel_serials # RTN16j
Expand Down
8 changes: 5 additions & 3 deletions lib/ably/realtime/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Connection
include Ably::Modules::Conversions
include Ably::Modules::SafeYield
extend Ably::Modules::Enum
using Ably::Util::AblyExtensions


# The current {Ably::Realtime::Connection::STATE} of the connection.
# Describes the realtime [Connection]{@link Connection} object states.
Expand Down Expand Up @@ -345,7 +347,7 @@ def recovery_key
# @return [String] a json string which incorporates the @connectionKey@, the current @msgSerial@ and collection
# of pairs of channel @name@ and current @channelSerial@ for every currently attached channel
def create_recovery_key
if key.nil? || key.empty? || state == :closing || state == :closed || state == :failed || state == :suspended
if key.nil_or_empty? || state == :closing || state == :closed || state == :failed || state == :suspended
return nil #RTN16g2
end
RecoveryKeyContext.new(key, client_msg_serial, client.channels.get_channel_serials).to_json
Expand Down Expand Up @@ -471,10 +473,10 @@ def create_websocket_transport
url_params['clientId'] = client.auth.client_id if client.auth.has_client_id?
url_params.merge!(client.transport_params)

if not Ably::Util::String.is_null_or_empty(key)
if not key.nil_or_empty?
url_params.merge! resume: key
logger.debug { "Resuming connection with key #{key}" }
elsif not Ably::Util::String.is_null_or_empty(client.recover)
elsif not client.recover.nil_or_empty?
recovery_context = RecoveryKeyContext.from_json(client.recover, logger)
unless recovery_context.nil?
key = recovery_context.connection_key
Expand Down
4 changes: 2 additions & 2 deletions lib/ably/realtime/connection/connection_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ConnectionManager
RESOLVABLE_ERROR_CODES = {
token_expired: Ably::Exceptions::TOKEN_EXPIRED_CODE
}
using Ably::Util::AblyExtensions

def initialize(connection)
@connection = connection
Expand Down Expand Up @@ -112,8 +113,7 @@ def connected(protocol_message)
# Update the connection details and any associated defaults
connection.set_connection_details protocol_message.connection_details

is_connection_resume_or_recover_attempt = !Ably::Util::String.is_null_or_empty(connection.key) ||
!Ably::Util::String.is_null_or_empty(client.recover)
is_connection_resume_or_recover_attempt = !connection.key.nil_or_empty? || !client.recover.nil_or_empty?

# RTN15c7, RTN16d
failed_resume_or_recover = !protocol_message.connection_id == connection.id && !protocol_message.error.nil?
Expand Down
5 changes: 3 additions & 2 deletions lib/ably/rest/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Client
include Ably::Modules::Conversions
include Ably::Modules::HttpHelpers
extend Forwardable
using Ably::Util::AblyExtensions

# Default Ably domain for REST
DOMAIN = 'rest.ably.io'
Expand Down Expand Up @@ -186,7 +187,7 @@ def initialize(options)

@agent = options.delete(:agent) || Ably::AGENT
@realtime_client = options.delete(:realtime_client)
@tls = options.fetch(:tls, true); options.delete(:tls)
@tls = options.fetch_or_default(:tls, true); options.delete(:tls)
@environment = options.delete(:environment) # nil is production
@environment = nil if [:production, 'production'].include?(@environment)
@protocol = options.delete(:protocol) || :msgpack
Expand All @@ -200,7 +201,7 @@ def initialize(options)
@log_retries_as_info = options.delete(:log_retries_as_info)
@max_message_size = options.delete(:max_message_size) || MAX_MESSAGE_SIZE
@max_frame_size = options.delete(:max_frame_size) || MAX_FRAME_SIZE
@idempotent_rest_publishing = options.fetch(:idempotent_rest_publishing, true); options.delete(:idempotent_rest_publishing)
@idempotent_rest_publishing = options.fetch_or_default(:idempotent_rest_publishing, true); options.delete(:idempotent_rest_publishing)

if options[:fallback_hosts_use_default] && options[:fallback_hosts]
raise ArgumentError, "fallback_hosts_use_default cannot be set to try when fallback_hosts is also provided"
Expand Down
20 changes: 17 additions & 3 deletions lib/ably/util/string.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# frozen_string_literal: true

module Ably::Util
module String
def self.is_null_or_empty(str)
str.nil? || str.empty?
module AblyExtensions
refine Object do
def nil_or_empty?
self.nil? || self.empty?
end
end

refine Hash do
# @param [Object] key
# @param [Object] default
def fetch_or_default(key, default)
value = self.fetch(key, default)
if value.nil?
return default
end
return value
end
end
end
end

0 comments on commit f470f3d

Please sign in to comment.