zaikio-webhooks 0.0.3 → 0.0.8

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
  SHA256:
3
- metadata.gz: 59f8182e718c5ded0c81a0fbde73afa950407aa3d0373922855cb2111e969190
4
- data.tar.gz: aadae2eb05d434998fa6d4d94c2981d910a25ccda0bac5fe105a07a62c284678
3
+ metadata.gz: cf6756e5fa3c37dfe5d4aed4cc5566d3390071a8e00be34345db988175dd4906
4
+ data.tar.gz: 0a0ca2e2511ce82f4ec2505ca1389ced5062f23edaee106431162fa3d7cb93d2
5
5
  SHA512:
6
- metadata.gz: 076bdcb6a097ddab10d2eeb18a55d088fa8f5976eea1f3d1be0e02cee53d2e4025a3de8f3f5ac76a1e4e1611a183098651d10f1af89c976af29f7948f0fc0048
7
- data.tar.gz: 9828497a523bcdae0f946d2319de93381c894daf0b44c3490d2808b04220d3a66b2ef6f9dbb5bcbf5026850f94ca2c7d562ba66e89dfe0a8a636ac2b0e022de5
6
+ metadata.gz: 9945cb3f9ab6f0b08650ee1ca71e40c82c04e4f52844fe6774d46718361d22443b2d41c337b86e66de23ccb4e663768a0ad079c8da83929c6d93c45765b99270
7
+ data.tar.gz: b85e483c5e7fc506aaee90c171b62a336ab99e88d11ffbf9ff67f4257bc931768bcb1719e48aeba79c4e20f6fd0f6d0c36b266e757cbbf7e32fbb4df6e28cf31
data/README.md CHANGED
@@ -34,13 +34,6 @@ Zaikio::Webhooks.configure do |config|
34
34
  my_other_app.shared_secret = "test-secret"
35
35
  end
36
36
  end
37
-
38
- # Perform job immediately, for all clients
39
- Zaikio::Webhooks.on "directory.revoked_access_token", RevokeAccessTokenJob,
40
- perform_now: true
41
- # Only for a specific client
42
- Zaikio::Webhooks.on "directory.machine_added", AddMachineJob,
43
- client_name: :my_app
44
37
  ```
45
38
 
46
39
  ### 3. Mount Engine
@@ -77,3 +70,47 @@ class AddMachineJob < ApplicationJob
77
70
  end
78
71
  end
79
72
  ```
73
+
74
+ ### 6. Add Webhook URL to Zaikio Hub
75
+
76
+ Login to [Zaikio Hub](https://directory.zaikio.com/) and go to your Apps and go to Edit App and add your webhook URL e.g. `https://mydomain.de/zaikio/webhook/my_app_name`.
77
+
78
+ Now you should receive all events you subscribed to.
79
+
80
+ ### 7. Usage
81
+
82
+ When everything is set up you can register events with custom jobs, using the
83
+ `Zaikio::Webhooks.on` function. This is idempotent and can be safely re-executed.
84
+
85
+ For Rails 6.0 and above, we can setup an initializer like so:
86
+
87
+ ```rb
88
+ # config/initializers/zaikio_webhooks.rb
89
+
90
+ # We need to add this special wrapper here because we're referencing autoloaded constants
91
+ # (RevokeAccessTokenJob, AddMachineJob) at boot time. For more information, see:
92
+ # https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-when-the-application-boots
93
+ Rails.application.reloader.to_prepare do
94
+ # Perform job immediately, for all clients
95
+ Zaikio::Webhooks.on "directory.revoked_access_token", RevokeAccessTokenJob,
96
+ perform_now: true
97
+ # Only for a specific client
98
+ Zaikio::Webhooks.on "directory.machine_added", AddMachineJob,
99
+ client_name: :my_app
100
+ end
101
+ ```
102
+
103
+ If you're using a Rails v5.2 or less, you can skip the outer `Rails.application.reloader`
104
+ block and just use the inner section directly.
105
+
106
+ ## Contributing
107
+
108
+ - Make your changes and submit a pull request for them
109
+ - Make sure to update `CHANGELOG.md`
110
+
111
+ To release a new version of the gem:
112
+ - Update the version in `lib/zaikio/webhooks/version.rb`
113
+ - Update `CHANGELOG.md` to include the new version and its release date
114
+ - Commit and push your changes
115
+ - Create a [new release on GitHub](https://github.com/zaikio/zaikio-webhooks/releases/new)
116
+ - CircleCI will build the Gem package and push it Rubygems for you
@@ -5,9 +5,10 @@ module Zaikio
5
5
  before_action :verify_signature
6
6
 
7
7
  def receive_event
8
- Zaikio::Webhooks.webhooks_for(params[:client_name], event_params[:name]).each do |webhook|
9
- webhook[:job_klass].public_send(webhook[:perform_now] ? :perform_now : :perform_later,
10
- Zaikio::Webhooks::Event.new(event_params))
8
+ Zaikio::Webhooks.webhooks_for(params[:client_name], event_params[:name])
9
+ .each do |job_klass, options|
10
+ job_klass.public_send(options[:perform_now] ? :perform_now : :perform_later,
11
+ Zaikio::Webhooks::Event.new(event_params))
11
12
  end
12
13
 
13
14
  head :ok
@@ -29,7 +30,8 @@ module Zaikio
29
30
  end
30
31
 
31
32
  def event_params
32
- params.permit(:id, :name, :subject, :timestamp, :version, :link, :received_at, payload: {})
33
+ params.permit(:id, :client_name, :name, :subject, :timestamp,
34
+ :version, :link, :received_at, payload: {})
33
35
  end
34
36
  end
35
37
  end
@@ -0,0 +1 @@
1
+ Rails.application.config.active_job.custom_serializers << Zaikio::Webhooks::EventSerializer
@@ -1,5 +1,7 @@
1
+ require "active_support/core_ext/integer/time"
1
2
  require "zaikio/webhooks/configuration"
2
3
  require "zaikio/webhooks/event"
4
+ require "zaikio/webhooks/event_serializer"
3
5
  require "zaikio/webhooks/engine"
4
6
 
5
7
  module Zaikio
@@ -30,7 +32,7 @@ module Zaikio
30
32
  end
31
33
 
32
34
  def webhooks_for(client_name, event_name)
33
- (@webhooks.dig(client_name.to_s, event_name.to_s) || [])
35
+ (@webhooks.dig(client_name.to_s, event_name.to_s) || {})
34
36
  end
35
37
 
36
38
  def on(event_name, job_klass, client_name: nil, perform_now: false)
@@ -40,10 +42,9 @@ module Zaikio
40
42
  client_names = Array(client_name || configuration.all_client_names).map(&:to_s)
41
43
  client_names.each do |name|
42
44
  @webhooks[name] ||= {}
43
- @webhooks[name][event_name.to_s] ||= []
44
- @webhooks[name][event_name.to_s] << {
45
- perform_now: perform_now,
46
- job_klass: job_klass
45
+ @webhooks[name][event_name.to_s] ||= {}
46
+ @webhooks[name][event_name.to_s][job_klass] = {
47
+ perform_now: perform_now
47
48
  }
48
49
  end
49
50
  end
@@ -12,7 +12,7 @@ module Zaikio
12
12
  end
13
13
 
14
14
  def logger
15
- @logger ||= Logger.new(STDOUT)
15
+ @logger ||= Logger.new($stdout)
16
16
  end
17
17
 
18
18
  def register_client(name)
@@ -5,7 +5,8 @@ module Zaikio
5
5
  class Event
6
6
  extend Forwardable
7
7
  attr_reader :data
8
- def_delegators :data, :id, :name, :version, :payload, :link
8
+
9
+ def_delegators :data, :id, :name, :version, :payload, :link, :client_name, :to_h
9
10
 
10
11
  def initialize(event_data)
11
12
  @data = OpenStruct.new(event_data)
@@ -0,0 +1,17 @@
1
+ module Zaikio
2
+ module Webhooks
3
+ class EventSerializer < ActiveJob::Serializers::ObjectSerializer
4
+ def serialize?(argument)
5
+ argument.is_a? Event
6
+ end
7
+
8
+ def serialize(event)
9
+ super(event.to_h)
10
+ end
11
+
12
+ def deserialize(data)
13
+ Event.new(data)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,5 +1,5 @@
1
1
  module Zaikio
2
2
  module Webhooks
3
- VERSION = "0.0.3".freeze
3
+ VERSION = "0.0.8".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,35 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zaikio-webhooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zaikio GmbH
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-27 00:00:00.000000000 Z
11
+ date: 2021-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '6.0'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 6.0.2.2
19
+ version: 5.0.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '6.0'
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: 6.0.2.2
26
+ version: 5.0.0
33
27
  description: Handle incoming Zaikio loom webhooks
34
28
  email:
35
29
  - js@crispymtn.com
@@ -41,6 +35,7 @@ files:
41
35
  - README.md
42
36
  - Rakefile
43
37
  - app/controllers/zaikio/webhooks/webhooks_controller.rb
38
+ - config/initializers/register_zaikio_webhooks_event_serializer.rb
44
39
  - config/routes.rb
45
40
  - lib/tasks/zaikio/webhook_tasks.rake
46
41
  - lib/zaikio/webhooks.rb
@@ -48,11 +43,14 @@ files:
48
43
  - lib/zaikio/webhooks/configuration.rb
49
44
  - lib/zaikio/webhooks/engine.rb
50
45
  - lib/zaikio/webhooks/event.rb
46
+ - lib/zaikio/webhooks/event_serializer.rb
51
47
  - lib/zaikio/webhooks/version.rb
52
- homepage: https://www.zaikio.com/
48
+ homepage: https://github.com/zaikio/zaikio-webhooks
53
49
  licenses:
54
50
  - MIT
55
- metadata: {}
51
+ metadata:
52
+ changelog_uri: https://github.com/zaikio/zaikio-webhooks/blob/main/CHANGELOG.md
53
+ source_code_uri: https://github.com/zaikio/zaikio-webhooks
56
54
  post_install_message:
57
55
  rdoc_options: []
58
56
  require_paths:
@@ -61,14 +59,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
59
  requirements:
62
60
  - - ">="
63
61
  - !ruby/object:Gem::Version
64
- version: '0'
62
+ version: 2.6.5
65
63
  required_rubygems_version: !ruby/object:Gem::Requirement
66
64
  requirements:
67
65
  - - ">="
68
66
  - !ruby/object:Gem::Version
69
67
  version: '0'
70
68
  requirements: []
71
- rubygems_version: 3.1.2
69
+ rubygems_version: 3.1.4
72
70
  signing_key:
73
71
  specification_version: 4
74
72
  summary: Handle incoming Zaikio loom webhooks