Class: ActiveHook::Hook
- Inherits:
-
Object
- Object
- ActiveHook::Hook
- Defined in:
- lib/activehook/hook.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 = {}) ⇒ Hook
constructor
A new instance of Hook.
- #retry? ⇒ Boolean
- #retry_at ⇒ Object
- #retry_max_time ⇒ Object
- #save ⇒ Object
- #save! ⇒ Object
- #signature ⇒ Object
- #to_json ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(options = {}) ⇒ Hook
Returns a new instance of Hook.
6 7 8 9 10 |
# File 'lib/activehook/hook.rb', line 6 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.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def created_at @created_at end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
4 5 6 |
# File 'lib/activehook/hook.rb', line 4 def errors @errors end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def id @id end |
#key ⇒ Object
Returns the value of attribute key.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def key @key end |
#payload ⇒ Object
Returns the value of attribute payload.
4 5 6 |
# File 'lib/activehook/hook.rb', line 4 def payload @payload end |
#retry_max ⇒ Object
Returns the value of attribute retry_max.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def retry_max @retry_max end |
#retry_time ⇒ Object
Returns the value of attribute retry_time.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def retry_time @retry_time end |
#token ⇒ Object
Returns the value of attribute token.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def token @token end |
#uri ⇒ Object
Returns the value of attribute uri.
3 4 5 |
# File 'lib/activehook/hook.rb', line 3 def uri @uri end |
Instance Method Details
#fail_at ⇒ Object
40 41 42 |
# File 'lib/activehook/hook.rb', line 40 def fail_at @created_at.to_i + retry_max_time end |
#final_payload ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/activehook/hook.rb', line 59 def final_payload { hook_id: @id, hook_key: @key, hook_time: @created_at, hook_signature: ActiveHook.config.signature_header, payload: @payload }.to_json end |
#retry? ⇒ Boolean
32 33 34 |
# File 'lib/activehook/hook.rb', line 32 def retry? fail_at > Time.now.to_i end |
#retry_at ⇒ Object
36 37 38 |
# File 'lib/activehook/hook.rb', line 36 def retry_at Time.now.to_i + @retry_time.to_i end |
#retry_max_time ⇒ Object
44 45 46 |
# File 'lib/activehook/hook.rb', line 44 def retry_max_time @retry_time.to_i * @retry_max.to_i end |
#save ⇒ Object
12 13 14 15 |
# File 'lib/activehook/hook.rb', line 12 def save return false unless valid? save_hook end |
#save! ⇒ Object
17 18 19 20 |
# File 'lib/activehook/hook.rb', line 17 def save! raise Errors::Hook, 'Hook is invalid' unless valid? save_hook end |
#signature ⇒ Object
67 68 69 |
# File 'lib/activehook/hook.rb', line 67 def signature OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), @token, final_payload) end |
#to_json ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/activehook/hook.rb', line 48 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/hook.rb', line 71 def valid? validate! @errors.empty? end |