Class: Sbmt::Outbox::V2::RedisJob

Inherits:
Object
  • Object
show all
Defined in:
lib/sbmt/outbox/v2/redis_job.rb

Constant Summary collapse

GENERIC_SEPARATOR =
":"
IDS_SEPARATOR =
","

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bucket, ids, timestamp = Time.current.to_i) ⇒ RedisJob

Returns a new instance of RedisJob.



12
13
14
15
16
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 12

def initialize(bucket, ids, timestamp = Time.current.to_i)
  @bucket = bucket
  @ids = ids
  @timestamp = timestamp
end

Instance Attribute Details

#bucketObject (readonly)

Returns the value of attribute bucket.



7
8
9
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 7

def bucket
  @bucket
end

#idsObject (readonly)

Returns the value of attribute ids.



7
8
9
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 7

def ids
  @ids
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



7
8
9
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 7

def timestamp
  @timestamp
end

Class Method Details

.deserialize!(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 26

def self.deserialize!(value)
  raise "invalid data type: string is required" unless value.is_a?(String)

  bucket, ts_utc, ids_str, _ = value.split(GENERIC_SEPARATOR)
  raise "invalid data format: bucket or ids are not valid" if bucket.blank? || ts_utc.blank? || ids_str.blank?

  ts = ts_utc.to_i

  ids = ids_str.split(IDS_SEPARATOR).map(&:to_i)
  raise "invalid data format: IDs are empty" if ids.blank?

  new(bucket, ids, ts)
end

Instance Method Details

#serializeObject



22
23
24
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 22

def serialize
  [bucket, timestamp, ids.join(IDS_SEPARATOR)].join(GENERIC_SEPARATOR)
end

#to_sObject



18
19
20
# File 'lib/sbmt/outbox/v2/redis_job.rb', line 18

def to_s
  serialize
end