Class: Webhooks
- Inherits:
-
Object
- Object
- Webhooks
- Defined in:
- lib/webhook.rb
Defined Under Namespace
Classes: EventNames
Constant Summary collapse
- SIGNATURE_HEADER =
'HTTP-RIMINDER-SIGNATURE'
Instance Method Summary collapse
- #base64urlDecode(inp) ⇒ Object
- #check ⇒ Object
- #customstrstr(inp, to_replace, by) ⇒ Object
- #getEncodedHeader(receivedMessage) ⇒ Object
- #handle(receivedMessage) ⇒ Object
-
#initialize(clientw, webhook_key) ⇒ Webhooks
constructor
A new instance of Webhooks.
- #isHandlerPresent(eventName) ⇒ Object
- #isSignatureValid(payload, sign) ⇒ Object
- #removeHandler(eventName) ⇒ Object
- #setHandler(eventName, callback) ⇒ Object
Constructor Details
#initialize(clientw, webhook_key) ⇒ Webhooks
Returns a new instance of Webhooks.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/webhook.rb', line 28 def initialize(clientw, webhook_key) @clientw = clientw @webhook_key = webhook_key @handlers = {} @handlers[EventNames::EVENT_PROFILE_PARSE_SUCCESS] = nil @handlers[EventNames::EVENT_PROFILE_PARSE_ERROR] = nil @handlers[EventNames::EVENT_PROFILE_SCORE_SUCCESS] = nil @handlers[EventNames::EVENT_PROFILE_SCORE_ERROR] = nil @handlers[EventNames::EVENT_FILTER_TRAIN_SUCCESS] = nil @handlers[EventNames::EVENT_FILTER_TRAIN_ERROR] = nil @handlers[EventNames::EVENT_FILTER_TRAIN_START] = nil @handlers[EventNames::EVENT_FILTER_SCORE_SUCCESS] = nil @handlers[EventNames::EVENT_FILTER_SCORE_ERROR] = nil @handlers[EventNames::EVENT_FILTER_SCORE_START] = nil @handlers[EventNames::ACTION_STAGE_SUCCESS] = nil @handlers[EventNames::ACTION_STAGE_ERROR] = nil @handlers[EventNames::ACTION_RATING_SUCCESS] = nil @handlers[EventNames::ACTION_RATING_ERROR] = nil end |
Instance Method Details
#base64urlDecode(inp) ⇒ Object
98 99 100 |
# File 'lib/webhook.rb', line 98 def base64urlDecode(inp) return Base64.decode64(customstrstr(inp, "-_", "+/")) end |
#check ⇒ Object
49 50 51 52 |
# File 'lib/webhook.rb', line 49 def check resp = @clientw.post('webhook/check') return resp["data"] end |
#customstrstr(inp, to_replace, by) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/webhook.rb', line 85 def customstrstr(inp, to_replace, by) res = "" inp.each_char {|c| tmpc = c tr_idx = to_replace.index(tmpc) if (!tr_idx.nil? && by.length < tr_idx) tmpc = by[tr_idx] end res = res + tmpc } return res end |
#getEncodedHeader(receivedMessage) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/webhook.rb', line 75 def getEncodedHeader(receivedMessage) if (receivedMessage.is_a?(Hash)) if (receivedMessage.key?(SIGNATURE_HEADER)) return receivedMessage[SIGNATURE_HEADER] end raise RiminderArgumentException.new("Webhhook request header should contain %s key." [SIGNATURE_HEADER]) end return receivedMessage end |
#handle(receivedMessage) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/webhook.rb', line 107 def handle(receivedMessage) if (@webhook_key.blank?) raise RiminderWebhookException.new("No webhook secret key provided") end encodedMessage = getEncodedHeader(receivedMessage) tmp = encodedMessage.split('.', 2) sign = base64urlDecode(tmp[0]) json_payload = base64urlDecode(tmp[1]) if (!isSignatureValid(json_payload, sign)) raise RiminderWebhookException.new("Invalid signature") end payload = JSON.parse(json_payload) payloadtype = payload['type'] if (payloadtype.nil? || !@handlers.key?(payloadtype)) raise RiminderWebhookException.new("Null or absent webhook type.") end callback = @handlers[payloadtype] if (!callback.nil?) callback.call(payloadtype, payload) end end |
#isHandlerPresent(eventName) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/webhook.rb', line 68 def isHandlerPresent(eventName) if (!@handlers.key?(eventName)) raise RiminderArgumentException("%s is not a valid event." [eventName]) end return @handlers[eventName].nil end |
#isSignatureValid(payload, sign) ⇒ Object
102 103 104 105 |
# File 'lib/webhook.rb', line 102 def isSignatureValid(payload, sign) expectedsign = OpenSSL::HMAC.digest("SHA256", @webhook_key, payload) return expectedsign == sign end |
#removeHandler(eventName) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/webhook.rb', line 61 def removeHandler(eventName) if (!@handlers.key?(eventName)) raise RiminderArgumentException("%s is not a valid event." [eventName]) end @handlers[eventName] = nil end |
#setHandler(eventName, callback) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/webhook.rb', line 54 def setHandler(eventName, callback) if (!@handlers.key?(eventName)) raise RiminderArgumentException("%s is not a valid event." [eventName]) end @handlers[eventName] = callback end |