class ActiveHook::Hook
Attributes
created_at[RW]
errors[R]
id[RW]
key[RW]
payload[R]
retry_max[RW]
retry_time[RW]
token[RW]
uri[RW]
Public Class Methods
new(options = {})
click to toggle source
# File lib/activehook/hook.rb, line 6 def initialize(options = {}) options = defaults.merge(options) options.each { |key, value| send("#{key}=", value) } @errors = {} end
Public Instance Methods
as_json(_options)
click to toggle source
# File lib/activehook/hook.rb, line 59 def as_json(_options) to_json end
fail_at()
click to toggle source
# File lib/activehook/hook.rb, line 40 def fail_at @created_at.to_i + retry_max_time end
final_payload()
click to toggle source
# File lib/activehook/hook.rb, line 63 def final_payload { hook_id: @id, hook_key: @key, hook_time: @created_at, hook_signature: ActiveHook.config.signature_header, payload: @payload }.to_json end
payload=(payload)
click to toggle source
# File lib/activehook/hook.rb, line 22 def payload=(payload) if payload.is_a?(String) @payload = JSON.parse(payload) else @payload = payload end rescue JSON::ParserError @payload = nil end
retry?()
click to toggle source
# File lib/activehook/hook.rb, line 32 def retry? fail_at > Time.now.to_i end
retry_at()
click to toggle source
# File lib/activehook/hook.rb, line 36 def retry_at Time.now.to_i + @retry_time.to_i end
retry_max_time()
click to toggle source
# File lib/activehook/hook.rb, line 44 def retry_max_time @retry_time.to_i * @retry_max.to_i end
save()
click to toggle source
# File lib/activehook/hook.rb, line 12 def save return false unless valid? save_hook end
save!()
click to toggle source
# File lib/activehook/hook.rb, line 17 def save! raise Errors::Hook, 'Hook is invalid' unless valid? save_hook end
signature()
click to toggle source
# File lib/activehook/hook.rb, line 71 def signature OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), @token, final_payload) end
to_json()
click to toggle source
# 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?()
click to toggle source
# File lib/activehook/hook.rb, line 75 def valid? validate! @errors.empty? end
Private Instance Methods
defaults()
click to toggle source
# File lib/activehook/hook.rb, line 90 def defaults { key: SecureRandom.uuid, created_at: Time.now.to_i, retry_time: 3600, retry_max: 3 } end
save_hook()
click to toggle source
# File lib/activehook/hook.rb, line 82 def save_hook ActiveHook.redis.with do |conn| @id = conn.incr('ah:total_queued') conn.lpush('ah:queue', to_json) conn.zadd('ah:validation', @id, @key) end end
validate!()
click to toggle source
# File lib/activehook/hook.rb, line 97 def validate! @errors.merge!(token: ['must be a string.']) unless @token.is_a?(String) @errors.merge!(payload: ['must be a Hash']) unless @payload.is_a?(Hash) @errors.merge!(uri: ['is not a valid format.']) unless @uri =~ /\A#{URI::regexp}\z/ @errors.merge!(created_at: ['must be an Integer.']) unless @created_at.is_a?(Integer) @errors.merge!(retry_time: ['must be an Integer.']) unless @retry_time.is_a?(Integer) @errors.merge!(retry_max: ['must be an Integer.']) unless @retry_max.is_a?(Integer) end