A Ruby gem for streaming data from the Bluesky/ATProto firehose

optimized check_if_not_nil further

+20 -13
+1 -1
lib/skyfall/firehose/account_message.rb
··· 24 24 # 25 25 def initialize(type_object, data_object) 26 26 super 27 - check_if_not_nil %w(seq did time active) 27 + check_if_not_nil 'seq', 'did', 'time', 'active' 28 28 29 29 @active = @data_object['active'] 30 30 @status = @data_object['status']&.to_sym
+1 -1
lib/skyfall/firehose/commit_message.rb
··· 26 26 # 27 27 def initialize(type_object, data_object) 28 28 super 29 - check_if_not_nil %w(seq repo commit blocks ops time) 29 + check_if_not_nil 'seq', 'repo', 'commit', 'blocks', 'ops', 'time' 30 30 end 31 31 32 32 # @return [CID] CID (Content Identifier) of the commit
+1 -1
lib/skyfall/firehose/identity_message.rb
··· 26 26 # 27 27 def initialize(type_object, data_object) 28 28 super 29 - check_if_not_nil %w(seq did time) 29 + check_if_not_nil 'seq', 'did', 'time' 30 30 31 31 @handle = @data_object['handle'] 32 32 end
+1 -1
lib/skyfall/firehose/info_message.rb
··· 37 37 # 38 38 def initialize(type_object, data_object) 39 39 super 40 - check_if_not_nil %w(name) 40 + check_if_not_nil 'name' 41 41 42 42 @name = @data_object['name'] 43 43 @message = @data_object['message']
+1 -1
lib/skyfall/firehose/labels_message.rb
··· 26 26 # 27 27 def initialize(type_object, data_object) 28 28 super 29 - check_if_not_nil %w(seq labels) 29 + check_if_not_nil 'seq', 'labels' 30 30 31 31 @labels = @data_object['labels'].map { |x| Label.new(x) } 32 32 end
+15 -8
lib/skyfall/firehose/message.rb
··· 152 152 instance_variables - [:@type_object, :@data_object, :@blocks] 153 153 end 154 154 155 - # Checks if all required fields are set in the data object. 156 - # @param fields [Array<String>] list of fields to check 157 - # @raise [DecodeError] if any of the fields is nil or not set 158 - def check_if_not_nil(fields) 159 - missing = fields.select { |f| @data_object[f].nil? } 160 155 161 - raise DecodeError.new("Missing event details (#{missing.map(&:to_s).join(', ')})") if missing.length > 0 162 - end 156 + private 163 157 158 + # Note: this method is written this way as an optimization 159 + def check_if_not_nil(a, b = nil, c = nil, d = nil, e = nil, f = nil) 160 + ok = @data_object.has_key?(a) 161 + ok &&= @data_object.has_key?(b) if b 162 + ok &&= @data_object.has_key?(c) if c 163 + ok &&= @data_object.has_key?(d) if d 164 + ok &&= @data_object.has_key?(e) if e 165 + ok &&= @data_object.has_key?(f) if f 164 166 165 - private 167 + if !ok 168 + expected_fields = [a, b, c, d, e, f].compact 169 + missing_fields = expected_fields.select { |x| @data_object[x].nil? } 170 + raise DecodeError.new("Missing event details (#{missing_fields.map(&:to_s).join(', ')})") 171 + end 172 + end 166 173 167 174 def self.decode_cbor_objects(data) 168 175 objects = CBOR.decode_sequence(data)