Class: FastlyNsq::TestMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/fastly_nsq/testing.rb

Overview

Stub for Nsq::Message used for testing. Use this class instead of a struct or test stubs when testing application logic that requires a Nsq::Message.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_body) ⇒ TestMessage

Returns a new instance of TestMessage.



122
123
124
125
126
# File 'lib/fastly_nsq/testing.rb', line 122

def initialize(raw_body)
  @raw_body = raw_body
  @id = Digest::SHA1.hexdigest(raw_body.to_s + Time.now.to_s)
  @attempts = 0
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



119
120
121
# File 'lib/fastly_nsq/testing.rb', line 119

def attempts
  @attempts
end

#idObject (readonly)

Returns the value of attribute id.



120
121
122
# File 'lib/fastly_nsq/testing.rb', line 120

def id
  @id
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



118
119
120
# File 'lib/fastly_nsq/testing.rb', line 118

def raw_body
  @raw_body
end

Instance Method Details

#bodyObject



128
129
130
131
132
# File 'lib/fastly_nsq/testing.rb', line 128

def body
  JSON.parse(JSON.dump(raw_body))
rescue JSON::ParserError
  raw_body
end

#finishObject



134
135
136
# File 'lib/fastly_nsq/testing.rb', line 134

def finish
  FastlyNsq::Messages.messages.find { |_, ms| ms.delete(self) }
end

#requeueObject



138
139
140
141
# File 'lib/fastly_nsq/testing.rb', line 138

def requeue(*)
  @attempts += 1
  true
end