Skip to content

Commit

Permalink
fix lint & remove redundant initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahad-10 committed Jul 25, 2024
1 parent 490c8a8 commit e9eef90
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 67 deletions.
3 changes: 2 additions & 1 deletion lib/wampproto/acceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ def handle_cryptosign_authenticate(msg)
end

def create_session_id
rand(100_000..9_007_199_254_740_992)
# to_i is added to satisfy RBS else they are not required
rand(100_000..9_007_199_254_740_992).to_i
end
end
end
3 changes: 2 additions & 1 deletion lib/wampproto/id_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class IdGenerator

class << self
def generate_session_id
rand(1..MAX_ID)
# to_i is added to satisfy RBS else they are not required
rand(1..MAX_ID).to_i
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/wampproto/message.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# frozen_string_literal: true

require_relative "message/util"
require_relative "message/validation_spec"

require_relative "message/base"
require_relative "message/hello"
require_relative "message/welcome"
Expand Down Expand Up @@ -30,8 +33,6 @@
require_relative "message/invocation"
require_relative "message/yield"

require_relative "message/util"

module Wampproto
# message root
module Message
Expand Down
4 changes: 2 additions & 2 deletions lib/wampproto/message/abort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IAbortFields
class AbortFields < IAbortFields
attr_reader :details, :reason, :args, :kwargs

def initialize(details, reason, args, kwargs)
def initialize(details, reason, *args, **kwargs)
super()
@details = details
@reason = reason
Expand All @@ -25,7 +25,7 @@ class Abort < Base
attr_reader :details, :reason, :args, :kwargs

TEXT = "ABORT"
VALIDATION_SPEC = ValidationSpec.new(
VALIDATION_SPEC = Message::ValidationSpec.new(
3,
5,
TEXT,
Expand Down
90 changes: 32 additions & 58 deletions lib/wampproto/message/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,14 @@ class Fields
:message_type, :signature, :reason, :topic, :extra,
:options, :details, :subscription_id, :publication_id,
:registration_id

def initialize # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
@request_id = nil
@uri = nil
@args = nil
@kwargs = nil

@session_id = nil

@realm = nil
@authid = nil
@authrole = nil
@authmethod = nil
@authmethods = nil
@authextra = nil
@roles = nil

@message_type = nil
@signature = nil
@reason = nil
@topic = nil

@extra = nil
@options = nil
@details = nil

@subscription_id = nil
@publication_id = nil
@registration_id = nil
end
end

# util module
module Util # rubocop:disable Metrics/ModuleLength
DEFAULT_ROLES = { caller: {}, publisher: {}, subscriber: {}, callee: {} }.freeze

module_function

def validate_int(value, index, message)
return nil if value.is_a?(Integer)

Expand Down Expand Up @@ -151,16 +125,16 @@ def validate_realm(wamp_msg, index, fields, message)
end

def validate_authid(details, index, fields, message) # rubocop: disable Metrics/MethodLength
if details.key?(:authid)
authid = details[:authid]
if details.key?("authid")
authid = details["authid"]
error = validate_string(authid, index, message)
new_error = format(
Exceptions::INVALID_DETAIL_ERROR,
message:,
index:,
key: "authid",
expected_type: "string",
actual_type: value.class
actual_type: authid.class
)

return new_error if error
Expand All @@ -172,16 +146,16 @@ def validate_authid(details, index, fields, message) # rubocop: disable Metrics/
end

def validate_authrole(details, index, fields, message) # rubocop:disable Metrics/MethodLength
if details.key?(:authrole)
authrole = details[:authrole]
error = validate_string(authid, index, message)
if details.key?("authrole")
authrole = details["authrole"]
error = validate_string(authrole, index, message)
new_error = format(
Exceptions::INVALID_DETAIL_ERROR,
message:,
index:,
key: "authrole",
expected_type: "string",
actual_type: value.class
actual_type: authrole.class
)

return new_error if error
Expand All @@ -202,16 +176,16 @@ def validate_authmethod(wamp_msg, index, fields, message)
end

def validate_authmethods(details, index, fields, message) # rubocop:disable Metrics/MethodLength
if details.key?(:authmethods)
authmethods = details[:authmethods]
error = validate_list(authid, index, message)
if details.key?("authmethods")
authmethods = details["authmethods"]
error = validate_list(authmethods, index, message)
new_error = format(
Exceptions::INVALID_DETAIL_ERROR,
message:,
index:,
key: "authmethods",
expected_type: "list",
actual_type: value.class
actual_type: authmethods.class
)

return new_error if error
Expand All @@ -223,16 +197,16 @@ def validate_authmethods(details, index, fields, message) # rubocop:disable Metr
end

def validate_welcome_authmethod(details, index, fields, message) # rubocop:disable Metrics/MethodLength
if details.key?(:authmethod)
authmethod = details[:authmethod]
error = validate_string(authid, index, message)
if details.key?("authmethod")
authmethod = details["authmethod"]
error = validate_string(authmethod, index, message)
new_error = format(
Exceptions::INVALID_DETAIL_ERROR,
message:,
index:,
key: "authmethod",
expected_type: "string",
actual_type: value.class
actual_type: authmethod.class
)

return new_error if error
Expand All @@ -244,16 +218,16 @@ def validate_welcome_authmethod(details, index, fields, message) # rubocop:disab
end

def validate_authextra(details, index, fields, message) # rubocop:disable Metrics/MethodLength
if details.key?(:authextra)
authextra = details[:authextra]
error = validate_hash(authid, index, message)
if details.key?("authextra")
authextra = details["authextra"]
error = validate_hash(authextra, index, message)
new_error = format(
Exceptions::INVALID_DETAIL_ERROR,
message:,
index:,
key: "authextra",
expected_type: "hash",
actual_type: value.class
actual_type: authextra.class
)

return new_error if error
Expand All @@ -265,22 +239,22 @@ def validate_authextra(details, index, fields, message) # rubocop:disable Metric
end

def validate_roles(details, index, fields, message) # rubocop:disable Metrics/MethodLength
if details.key?(:roles)
roles = details[:roles]
error = validate_hash(authid, index, message)
if details.key?("roles")
roles = details["roles"]
error = validate_hash(roles, index, message)
new_error = format(
Exceptions::INVALID_DETAIL_ERROR,
message:,
index:,
key: "roles",
expected_type: "hash",
actual_type: value.class
actual_type: roles.class
)

return new_error if error

valid_keys = default_roles.keys
invalid_keys = hash.keys - valid_keys
valid_keys = DEFAULT_ROLES.keys
invalid_keys = roles.keys - valid_keys

if invalid_keys.any?
return "#{message}: value at index #{index} for roles key must be in #{valid_keys} but was #{invalid_keys}"
Expand Down Expand Up @@ -440,7 +414,7 @@ def validate_welcome_details(wamp_msg, index, fields, message) # rubocop: disabl
end

def sanity_check(wamp_message, min_length, max_length, expected_id, message) # rubocop:disable Metrics/MethodLength
unless value.is_a?(Array)
unless wamp_message.is_a?(Array)
raise ArgumentError, "invalid message type #{wamp_message.class} for #{message}, type should be a list"
end

Expand All @@ -464,10 +438,10 @@ def validate_message(wamp_msg, type, message, val_spec)
errors = []
f = Fields.new
val_spec.spec.each do |idx, func|
error = send(func, message, idx, f, val_spec.message)
error = func.call(message, idx, f, val_spec.message)
errors.append(error) if error
end
raise ArgumentError, *errors unless errors.empty?
raise ArgumentError, errors.join(", ") unless errors.empty?

f
end
Expand Down
23 changes: 23 additions & 0 deletions sig/wampproto/message/abort.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@ module Wampproto
attr_reader kwargs: Hash[Symbol, untyped]
end

class AbortFields
@details: Hash[Symbol, untyped]

@reason: String

@args: Array[untyped]

@kwargs: Hash[Symbol, untyped]

@marshal: Array[untyped]

attr_reader details: Hash[Symbol, untyped]

attr_reader reason: String

attr_reader args: Array[untyped]

attr_reader kwargs: Hash[Symbol, untyped]

def initialize: (Hash[Symbol, untyped] details, String reason, *Array[untyped] args, **Hash[Symbol, untyped] kwargs) -> void

end

# abort message
class Abort < Base
TEXT: String
Expand Down
8 changes: 5 additions & 3 deletions sig/wampproto/message/util.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ module Wampproto
end

module Util
def validate_int: (Integer value, Integer index, String message) -> (String | Integer)
DEFAULT_ROLES: Hash[Symbol, Hash[untyped, untyped]]

def validate_int: (Integer value, Integer index, String message) -> (String | nil)

def validate_string: (String value, Integer index, String message) -> (String | nil)

Expand Down Expand Up @@ -83,9 +85,9 @@ module Wampproto

def validate_details: (Array[untyped] wamp_msg, Integer index, Fields fields, String message) -> (String | nil)

def validate_hello_details: (Array[untyped] wamp_msg, Integer index, Fields fields, String message) -> (String | nil)
def validate_hello_details: (Array[untyped] wamp_msg, Integer index, Fields fields, String message) -> (String | Array[untyped] | nil)

def validate_welcome_details: (Array[untyped] wamp_msg, Integer index, Fields fields, String message) -> (String | nil)
def validate_welcome_details: (Array[untyped] wamp_msg, Integer index, Fields fields, String message) -> (String | Array[untyped] | nil)

def validate_subscription_id: (Array[untyped] wamp_msg, Integer index, Fields fields, String message) -> (String | nil)

Expand Down

0 comments on commit e9eef90

Please sign in to comment.