forked from ibalosh/sinhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.rb
84 lines (69 loc) · 2.16 KB
/
hooks.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# encoding: utf-8
require_relative 'hook_storage/folder'
require_relative 'hook_storage/ram'
require_relative 'hook_storage/redis'
require 'sinatra'
module Hooks
# Responses class represents objects which will hold all responses tied to hooks, while server is up and running.
#
# It's main function is to hold response types in hash form like: [HOOK_ID][STATUS_TYPE][VALUE]
# These responses types are picked up by sinatra app and handled
class Responses
def initialize
@responses = {}
end
def add(hook_id, type, value)
clear(hook_id) unless hook_responses_set?(hook_id)
@responses[hook_id][type] = value
end
def clear(hook_id)
@responses[hook_id] = {}
end
def get(hook_id)
hook_responses_set?(hook_id) ? @responses[hook_id] : {}
end
def hook_responses_set?(hook_id)
!@responses[hook_id].nil?
end
end
# Delegation class for different web hook data holders.
# Currently only one type of web hook data holders is available - Folder storage - which holds all data
# for web hooks locally, in folders and files.
class Data
def initialize(settings)
case settings[:hooks_storage]
when 'folder'
@hook_storage = HookStorage::Folder.new(settings[:hooks_folder_storage_path], settings[:hooks_to_store].to_i)
when 'redis'
@hook_storage = HookStorage::Redis.new(settings[:hooks_redis_host], 6379, settings[:hooks_to_store].to_i)
else
@hook_storage = HookStorage::RAM.new(settings[:hooks_to_store].to_i)
end
end
def hooks_to_store_count
@hook_storage.hooks_to_store_count
end
def create(hook_id = nil)
@hook_storage.create(hook_id)
end
def delete(hook_id)
@hook_storage.delete(hook_id)
end
def is_available?(hook_id)
@hook_storage.is_available?(hook_id)
end
# Hook data management
def set_data(hook_id, hook_data)
@hook_storage.set_data(hook_id, hook_data)
end
def read_data(hook_id)
@hook_storage.read_data(hook_id)
end
def clear_data(hook_id)
@hook_storage.clear_data(hook_id)
end
def endpoints
@hook_storage.endpoints
end
end
end