Class: ActiveHook::Server::Message
- Inherits:
-
Object
- Object
- ActiveHook::Server::Message
- Defined in:
- lib/activehook/server/message.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#id ⇒ Object
Returns the value of attribute id.
-
#key ⇒ Object
Returns the value of attribute key.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#retry_max ⇒ Object
Returns the value of attribute retry_max.
-
#retry_time ⇒ Object
Returns the value of attribute retry_time.
-
#token ⇒ Object
Returns the value of attribute token.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
- #fail_at ⇒ Object
- #final_payload ⇒ Object
-
#initialize(options = {}) ⇒ Message
constructor
A new instance of Message.
- #retry? ⇒ Boolean
- #retry_at ⇒ Object
- #retry_max_time ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
- #signature ⇒ Object
- #to_json ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Message
Returns a new instance of Message.
7 8 9 10 11 |
# File 'lib/activehook/server/message.rb', line 7 def initialize( = {}) = defaults.merge() .each { |key, value| send("#{key}=", value) } @errors = {} end |
Instance Attribute Details
#created_at ⇒ Object
Returns the value of attribute created_at.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def created_at @created_at end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
5 6 7 |
# File 'lib/activehook/server/message.rb', line 5 def errors @errors end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def key @key end |
#payload ⇒ Object
Returns the value of attribute payload.
5 6 7 |
# File 'lib/activehook/server/message.rb', line 5 def payload @payload end |
#retry_max ⇒ Object
Returns the value of attribute retry_max.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def retry_max @retry_max end |
#retry_time ⇒ Object
Returns the value of attribute retry_time.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def retry_time @retry_time end |
#token ⇒ Object
Returns the value of attribute token.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def token @token end |
#uri ⇒ Object
Returns the value of attribute uri.
4 5 6 |
# File 'lib/activehook/server/message.rb', line 4 def uri @uri end |
Instance Method Details
#fail_at ⇒ Object
41 42 43 |
# File 'lib/activehook/server/message.rb', line 41 def fail_at @created_at.to_i + retry_max_time end |
#final_payload ⇒ Object
60 61 62 63 64 65 |
# File 'lib/activehook/server/message.rb', line 60 def final_payload { id: @id, key: @key, created_at: @created_at, payload: @payload }.to_json end |
#retry? ⇒ Boolean
33 34 35 |
# File 'lib/activehook/server/message.rb', line 33 def retry? fail_at > Time.now.to_i end |
#retry_at ⇒ Object
37 38 39 |
# File 'lib/activehook/server/message.rb', line 37 def retry_at Time.now.to_i + @retry_time.to_i end |
#retry_max_time ⇒ Object
45 46 47 |
# File 'lib/activehook/server/message.rb', line 45 def retry_max_time @retry_time.to_i * @retry_max.to_i end |
#save ⇒ Object
13 14 15 16 |
# File 'lib/activehook/server/message.rb', line 13 def save return false unless valid? save_hook end |
#save! ⇒ Object
18 19 20 21 |
# File 'lib/activehook/server/message.rb', line 18 def save! raise Errors::Message, 'Message is invalid' unless valid? save_hook end |
#signature ⇒ Object
67 68 69 |
# File 'lib/activehook/server/message.rb', line 67 def signature OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), @token, final_payload) end |
#to_json ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/activehook/server/message.rb', line 49 def to_json { id: @id, key: @key, token: @token, created_at: @created_at, retry_time: @retry_time, retry_max: @retry_max, uri: @uri, payload: @payload }.to_json end |
#valid? ⇒ Boolean
71 72 73 74 |
# File 'lib/activehook/server/message.rb', line 71 def valid? validate! @errors.empty? end |