activehook 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/activehook/hook.rb +39 -13
- data/lib/activehook/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c3a53e2c3199781a30230e50e37cbae879b4b77
|
4
|
+
data.tar.gz: f4f621b6e241f5e8814ba5de7422ebc2f6ccd7b7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67c129e8544779658183a853e2343e293330c9a0639ca3d59530c232fc84c32e05b6c0af77591f0392ecbf6d4d98755207ec292a6741d5102f6631579774d125
|
7
|
+
data.tar.gz: 6e64885a0ae89f39afe81b5ecdabd2f253963cbb864e1105799c1d66ebdd9e4b30bd2c9d615c17cdc2d72dd2e1ef9178c90f121036a4a33555365b1b452f3b6a
|
data/README.md
CHANGED
@@ -84,7 +84,13 @@ add_column :users, :webhook_token, :string
|
|
84
84
|
With our app setup, we can create webhooks for processing. From within our application, all we have to do is:
|
85
85
|
|
86
86
|
```ruby
|
87
|
-
ActiveHook::Hook.new(token: webhook_token, uri: webhook_uri, payload: { msg: 'My first webhook!' })
|
87
|
+
hook = ActiveHook::Hook.new(token: webhook_token, uri: webhook_uri, payload: { msg: 'My first webhook!' })
|
88
|
+
if hook.save # We can also do save!, which would raise an exception upon failure.
|
89
|
+
# Success.
|
90
|
+
else
|
91
|
+
# Failed - access errors at hook.errors
|
92
|
+
end
|
93
|
+
|
88
94
|
```
|
89
95
|
|
90
96
|
That's it! We provide a valid string token and URI, as well hash payload. ActiveHooks server will then attempt to send the webhook. If the webhook fails to be delivered, it will be sent to the retry queue. Delivery will be reattempted at the specified intervals, and eventually dropped if all attempts fail.
|
data/lib/activehook/hook.rb
CHANGED
@@ -1,19 +1,32 @@
|
|
1
1
|
module ActiveHook
|
2
2
|
class Hook
|
3
|
-
attr_accessor :token, :uri, :
|
3
|
+
attr_accessor :token, :uri, :id, :key, :retry_max, :retry_time, :created_at
|
4
|
+
attr_reader :errors, :payload
|
4
5
|
|
5
6
|
def initialize(options = {})
|
6
7
|
options = defaults.merge(options)
|
7
8
|
options.each { |key, value| send("#{key}=", value) }
|
9
|
+
@errors = {}
|
8
10
|
end
|
9
11
|
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
12
|
+
def save
|
13
|
+
return false unless valid?
|
14
|
+
save_hook
|
15
|
+
end
|
16
|
+
|
17
|
+
def save!
|
18
|
+
raise Errors::Hook, 'Hook is invalid' unless valid?
|
19
|
+
save_hook
|
20
|
+
end
|
21
|
+
|
22
|
+
def payload=(payload)
|
23
|
+
if payload.is_a?(String)
|
24
|
+
@payload = JSON.parse(payload)
|
25
|
+
else
|
26
|
+
@payload = payload
|
16
27
|
end
|
28
|
+
rescue JSON::ParserError
|
29
|
+
@payload = nil
|
17
30
|
end
|
18
31
|
|
19
32
|
def retry?
|
@@ -55,8 +68,21 @@ module ActiveHook
|
|
55
68
|
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), @token, final_payload)
|
56
69
|
end
|
57
70
|
|
71
|
+
def valid?
|
72
|
+
validate!
|
73
|
+
@errors.empty?
|
74
|
+
end
|
75
|
+
|
58
76
|
private
|
59
77
|
|
78
|
+
def save_hook
|
79
|
+
ActiveHook.redis.with do |conn|
|
80
|
+
@id = conn.incr('ah:total_queued')
|
81
|
+
conn.lpush('ah:queue', to_json)
|
82
|
+
conn.zadd('ah:validation', @id, @key)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
60
86
|
def defaults
|
61
87
|
{ key: SecureRandom.uuid,
|
62
88
|
created_at: Time.now.to_i,
|
@@ -65,12 +91,12 @@ module ActiveHook
|
|
65
91
|
end
|
66
92
|
|
67
93
|
def validate!
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
94
|
+
@errors.merge!(token: ['must be a string.']) unless @token.is_a?(String)
|
95
|
+
@errors.merge!(payload: ['must be a Hash']) unless @payload.is_a?(Hash)
|
96
|
+
@errors.merge!(uri: ['is not a valid format.']) unless @uri =~ /\A#{URI::regexp}\z/
|
97
|
+
@errors.merge!(created_at: ['must be an Integer.']) unless @created_at.is_a?(Integer)
|
98
|
+
@errors.merge!(retry_time: ['must be an Integer.']) unless @retry_time.is_a?(Integer)
|
99
|
+
@errors.merge!(retry_max: ['must be an Integer.']) unless @retry_max.is_a?(Integer)
|
74
100
|
end
|
75
101
|
end
|
76
102
|
end
|
data/lib/activehook/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activehook
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicholas Sweeting
|
@@ -207,3 +207,4 @@ signing_key:
|
|
207
207
|
specification_version: 4
|
208
208
|
summary: Fast and simple webhook delivery microservice for Ruby.
|
209
209
|
test_files: []
|
210
|
+
has_rdoc:
|