Class: ActiveHook::Hook

Inherits:
Object
  • Object
show all
Defined in:
lib/activehook/hook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  options = defaults.merge(options)
  options.each { |key, value| send("#{key}=", value) }
  @errors = {}
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def created_at
  @created_at
end

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'lib/activehook/hook.rb', line 4

def errors
  @errors
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def id
  @id
end

#keyObject

Returns the value of attribute key.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def key
  @key
end

#payloadObject

Returns the value of attribute payload.



4
5
6
# File 'lib/activehook/hook.rb', line 4

def payload
  @payload
end

#retry_maxObject

Returns the value of attribute retry_max.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def retry_max
  @retry_max
end

#retry_timeObject

Returns the value of attribute retry_time.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def retry_time
  @retry_time
end

#tokenObject

Returns the value of attribute token.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def token
  @token
end

#uriObject

Returns the value of attribute uri.



3
4
5
# File 'lib/activehook/hook.rb', line 3

def uri
  @uri
end

Instance Method Details

#fail_atObject



40
41
42
# File 'lib/activehook/hook.rb', line 40

def fail_at
  @created_at.to_i + retry_max_time
end

#final_payloadObject



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

Returns:

  • (Boolean)


32
33
34
# File 'lib/activehook/hook.rb', line 32

def retry?
  fail_at > Time.now.to_i
end

#retry_atObject



36
37
38
# File 'lib/activehook/hook.rb', line 36

def retry_at
  Time.now.to_i + @retry_time.to_i
end

#retry_max_timeObject



44
45
46
# File 'lib/activehook/hook.rb', line 44

def retry_max_time
  @retry_time.to_i * @retry_max.to_i
end

#saveObject



12
13
14
15
# File 'lib/activehook/hook.rb', line 12

def save
  return false unless valid?
  save_hook
end

#save!Object

Raises:



17
18
19
20
# File 'lib/activehook/hook.rb', line 17

def save!
  raise Errors::Hook, 'Hook is invalid' unless valid?
  save_hook
end

#signatureObject



67
68
69
# File 'lib/activehook/hook.rb', line 67

def signature
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), @token, final_payload)
end

#to_jsonObject



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

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/activehook/hook.rb', line 71

def valid?
  validate!
  @errors.empty?
end