Class: FakeSQS::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/fake_sqs/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Returns a new instance of Message.



11
12
13
14
15
16
17
18
19
# File 'lib/fake_sqs/message.rb', line 11

def initialize(options = {})
  @body = options.fetch("MessageBody")
  @id = options.fetch("Id") { SecureRandom.uuid }
  @md5 = options.fetch("MD5") { Digest::MD5.hexdigest(@body) }
  @sender_id = options.fetch("SenderId") { SecureRandom.uuid.delete('-').upcase[0...21] }
  @approximate_receive_count = 0
  @sent_timestamp = Time.now.to_i * 1000
  @delay_seconds = options.fetch("DelaySeconds", 0).to_i
end

Instance Attribute Details

#approximate_first_receive_timestampObject (readonly)

Returns the value of attribute approximate_first_receive_timestamp.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def approximate_first_receive_timestamp
  @approximate_first_receive_timestamp
end

#approximate_receive_countObject (readonly)

Returns the value of attribute approximate_receive_count.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def approximate_receive_count
  @approximate_receive_count
end

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def body
  @body
end

#delay_secondsObject (readonly)

Returns the value of attribute delay_seconds.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def delay_seconds
  @delay_seconds
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def id
  @id
end

#md5Object (readonly)

Returns the value of attribute md5.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def md5
  @md5
end

#sender_idObject (readonly)

Returns the value of attribute sender_id.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def sender_id
  @sender_id
end

#sent_timestampObject (readonly)

Returns the value of attribute sent_timestamp.



7
8
9
# File 'lib/fake_sqs/message.rb', line 7

def sent_timestamp
  @sent_timestamp
end

#visibility_timeoutObject

Returns the value of attribute visibility_timeout.



9
10
11
# File 'lib/fake_sqs/message.rb', line 9

def visibility_timeout
  @visibility_timeout
end

Instance Method Details

#attributesObject



47
48
49
50
51
52
53
54
# File 'lib/fake_sqs/message.rb', line 47

def attributes
  {
    "SenderId" => sender_id,
    "ApproximateFirstReceiveTimestamp" => approximate_first_receive_timestamp,
    "ApproximateReceiveCount"=> approximate_receive_count,
    "SentTimestamp"=> sent_timestamp
  }
end

#expire!Object



21
22
23
# File 'lib/fake_sqs/message.rb', line 21

def expire!
  self.visibility_timeout = nil
end

#expire_at(seconds) ⇒ Object



34
35
36
# File 'lib/fake_sqs/message.rb', line 34

def expire_at(seconds)
  self.visibility_timeout = Time.now + seconds
end

#expired?(limit = Time.now) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/fake_sqs/message.rb', line 30

def expired?( limit = Time.now )
  self.visibility_timeout.nil? || self.visibility_timeout < limit
end

#published?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/fake_sqs/message.rb', line 38

def published?
  if self.delay_seconds && self.delay_seconds > 0
    elapsed_seconds = Time.now.to_i - (self.sent_timestamp.to_i / 1000)
    elapsed_seconds >= self.delay_seconds
  else
    true
  end
end

#receiptObject



56
57
58
# File 'lib/fake_sqs/message.rb', line 56

def receipt
  Digest::SHA1.hexdigest self.id
end

#receive!Object



25
26
27
28
# File 'lib/fake_sqs/message.rb', line 25

def receive!
  @approximate_first_receive_timestamp ||= Time.now.to_i * 1000
  @approximate_receive_count += 1
end