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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 55b81e64e33c0af62107fd2c389333b28faa81d0
4
- data.tar.gz: 743442e1f3890a4da4f211b3e353faa86db9fc9b
3
+ metadata.gz: 4c3a53e2c3199781a30230e50e37cbae879b4b77
4
+ data.tar.gz: f4f621b6e241f5e8814ba5de7422ebc2f6ccd7b7
5
5
  SHA512:
6
- metadata.gz: a80cc6ad62c7e317f6806c6c37673187448be63b8005d571d7a80566e7673def1e8a9398b9184240742f59144d404f5aca75d1de46411b7acf499cd15962118c
7
- data.tar.gz: e09951cc388ce943b6a854d5d236b2d919c0980b1a4c1e981b2f0ddafa46cf60c63113f51e26891b95038ff391df460488d3560470c27c1deff64062aad3b5b0
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.
@@ -1,19 +1,32 @@
1
1
  module ActiveHook
2
2
  class Hook
3
- attr_accessor :token, :uri, :payload, :id, :key, :retry_max, :retry_time, :created_at
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 perform
11
- validate!
12
- ActiveHook.redis.with do |conn|
13
- @id = conn.incr('ah:total_queued')
14
- conn.lpush('ah:queue', to_json)
15
- conn.zadd('ah:validation', @id, @key)
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
- raise Errors::Hook, 'Token must be a String.' unless @token.is_a?(String)
69
- raise Errors::Hook, 'Payload must be a Hash.' unless @payload.is_a?(Hash)
70
- raise Errors::Hook, 'URI is not a valid format.' unless @uri =~ /\A#{URI::regexp}\z/
71
- raise Errors::Hook, 'Created at must be an Integer.' unless @created_at.is_a?(Integer)
72
- raise Errors::Hook, 'Retry time must be an Integer.' unless @retry_time.is_a?(Integer)
73
- raise Errors::Hook, 'Retry max must be an Integer.' unless @retry_max.is_a?(Integer)
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
@@ -1,4 +1,4 @@
1
1
  module ActiveHook
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  CODENAME = 'Fat Sparrow'
4
4
  end
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.5
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: