hanami-events-cloud_pubsub 1.0.0 → 1.0.1

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
- SHA256:
3
- metadata.gz: 852603b14e6c264281e40be86dad5e487ac3e77759dd9564b3be400fa519618e
4
- data.tar.gz: b5a85a233718c80e8704219010e1fe86075609249776846929a7b0a5c2ed849e
2
+ SHA1:
3
+ metadata.gz: 97f77c6262cd320d8cfdbe03c2ebddcc69008d45
4
+ data.tar.gz: 5dd8219a8cf755fb32bfb75c8f4e3306bff6715b
5
5
  SHA512:
6
- metadata.gz: beaf55c053904c12b69c64f2797a959dafa733d30e4364bc62e868a3cb9e78c654c392b58a97b7090e0fd3f705af6a1618cea2a7213e030976135160af2346a4
7
- data.tar.gz: d540a62c81a7d25564768b7f539d91894176bea80527f59001da2429a08e3615925ef90207000a43cefb6db26b599f0046e2fd786e4e895204c2eeab052bd396
6
+ metadata.gz: 4c971196c9903b9466bed196532fda7d9bd4658d05c5a5ecac841ef6caec31aa0b1d356befccfc0ee5f4fa7a55436463f4b1e8faef7c9f9590f321869f28edba
7
+ data.tar.gz: 501ad612c1ab074e7d9ce4809770b4aa1423250e4e7b88e1f45db3f3fa7b942877c50e00a69c07b6e056ca4d0a11cd02a36622c82947b1ba513dbe04143be5b7
data/.rubocop_todo.yml CHANGED
@@ -1,24 +1,28 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2018-06-11 14:37:44 -0400 using RuboCop version 0.54.0.
3
+ # on 2018-06-13 22:39:48 -0400 using RuboCop version 0.54.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
+ # Offense count: 1
10
+ # Configuration parameters: CountComments.
11
+ Metrics/MethodLength:
12
+ Max: 14
13
+
9
14
  # Offense count: 1
10
15
  # Configuration parameters: CountKeywordArgs.
11
16
  Metrics/ParameterLists:
12
17
  Max: 6
13
18
 
14
- # Offense count: 3
19
+ # Offense count: 1
15
20
  # Configuration parameters: AllowedVariables.
16
21
  Style/GlobalVars:
17
22
  Exclude:
18
23
  - 'examples/server.rb'
19
- - 'lib/hanami/events/cloud_pubsub/cli.rb'
20
24
 
21
- # Offense count: 22
25
+ # Offense count: 23
22
26
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
23
27
  # URISchemes: http, https
24
28
  Metrics/LineLength:
data/Gemfile.lock CHANGED
@@ -9,7 +9,7 @@ GIT
9
9
  PATH
10
10
  remote: .
11
11
  specs:
12
- hanami-events-cloud_pubsub (1.0.0)
12
+ hanami-events-cloud_pubsub (1.0.1)
13
13
  dry-configurable (~> 0.7.0)
14
14
  google-cloud-pubsub (~> 0.30.2)
15
15
  hanami-cli (~> 0.2.0)
data/README.md CHANGED
@@ -3,15 +3,37 @@
3
3
 
4
4
  ## Installation
5
5
 
6
- Add this line to your application's Gemfile:
6
+ ```ruby
7
+ bundle add hanami-events-cloud_pubsub
8
+ ```
9
+
10
+ If using Hanami, register the adapter in your `config/environment.rb`:
7
11
 
8
12
  ```ruby
9
- gem 'hanami-events-cloud_pubsub'
13
+ # config/environment.rb
14
+ # ...
15
+ require_relative './../lib/my_app'
16
+ require_relative './../apps/web/application'
17
+ # ...
18
+
19
+ require 'hanami/events/cloud_pubsub/register' # <----
10
20
  ```
11
21
 
12
- And then execute:
22
+ Configure the pubsub adapter how you want (optional):
13
23
 
14
- $ bundle
24
+ ```ruby
25
+ # config/environment.rb
26
+
27
+ Hanami.configure do
28
+ environment :development do
29
+ cloud_pubsub do |config|
30
+ config.pubsub = { project_id: 'emulator' }
31
+ config.logger = Hanami.logger
32
+ # ...
33
+ end
34
+ end
35
+ end
36
+ ```
15
37
 
16
38
  ## Usage
17
39
 
@@ -22,27 +44,12 @@ This gem is compatible with the
22
44
  should pass this:
23
45
 
24
46
  ```ruby
25
- $events.subscribe('user.deleted', id: 'my-subscriber-id') do |payload|
47
+ Hanami.events.subscribe('user.deleted', id: 'my-subscriber-id') do |payload|
26
48
  puts "Deleted user: #{payload}"
27
49
  end
28
50
  ```
29
51
 
30
- 2. If you want mixin behavior, follow this example until [this patch is
31
- merged](https://github.com/hanami/events/pull/76)
32
-
33
- ```ruby
34
- class WelcomeMailer
35
- include Hanami::Events::CloudPubsub::Mixin
36
-
37
- subscribe_to $events, 'user.created', id: 'welcome-mailer'
38
-
39
- def call(payload)
40
- payload
41
- end
42
- end
43
- ```
44
-
45
- 3. Responding to events is done in a different process via the CLI.
52
+ 2. Responding to events is done in a different process via the CLI.
46
53
 
47
54
  First, create a config file:
48
55
 
@@ -41,8 +41,6 @@ module Hanami
41
41
  # @param event_name [Symbol, String] the event name
42
42
  # @param block [Block] to execute when event is broadcasted
43
43
  def subscribe(event_name, id:, &block)
44
- return false unless listening?
45
-
46
44
  logger.debug("Subscribed listener \"#{id}\" for event \"#{event_name}\"")
47
45
 
48
46
  @subscribers << Subscriber.new(event_name, block, logger)
@@ -56,15 +54,6 @@ module Hanami
56
54
  pubs.each(&:stop).map(&:wait!)
57
55
  end
58
56
 
59
- def listen(should_listen = true)
60
- @listen = should_listen
61
- self
62
- end
63
-
64
- def listening?
65
- !!@listen # rubocop:disable Style/DoubleNegation
66
- end
67
-
68
57
  private
69
58
 
70
59
  attr_reader :logger
@@ -28,13 +28,9 @@ module Hanami
28
28
 
29
29
  option :config,
30
30
  type: :string,
31
- default: './config/cloudpubsub.rb',
31
+ default: './config/boot.rb',
32
32
  desc: 'Config file which is loaded before starting the runner'
33
33
 
34
- option :project_id,
35
- type: :string,
36
- desc: 'Project ID for the project'
37
-
38
34
  def call(opts)
39
35
  setup_env(opts)
40
36
  parse_opts(opts)
@@ -55,7 +51,6 @@ module Hanami
55
51
 
56
52
  def setup_env(opts)
57
53
  ENV['PUBSUB_EMULATOR_HOST'] ||= 'localhost:8085' if opts[:emulator]
58
- CloudPubsub.setup
59
54
  end
60
55
 
61
56
  def start_server
@@ -85,23 +80,14 @@ module Hanami
85
80
  end
86
81
 
87
82
  def parse_opts(opts)
88
- @project_id = opts[:project_id]
89
83
  @emulator = opts[:emulator]
90
84
  @config = opts[:config]
91
85
  end
92
86
 
93
87
  def build_runner
94
- pubsub_opts = {}
95
-
96
- pubsub_opts[:project_id] = 'emulator' if @emulator
97
- pubsub_opts[:project_id] = @project_id if @project_id
98
-
99
- pubsub = Google::Cloud::Pubsub.new pubsub_opts
100
- $events = Hanami::Events.initialize(:cloud_pubsub,
101
- pubsub: pubsub,
102
- logger: logger,
103
- listen: true)
104
- @runner = Runner.new(logger: logger, adapter: $events.adapter)
88
+ Hanami::Components.resolve 'events'
89
+ events = Hanami::Components['events']
90
+ @runner = Runner.new(logger: logger, adapter: events.adapter)
105
91
  end
106
92
 
107
93
  def logger
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ module Events
5
+ module CloudPubsub
6
+ module Integration
7
+ # Helpers use to setup all integration with hanami
8
+ module Setup
9
+ module_function
10
+
11
+ def setup
12
+ register_events_adapter
13
+ register_hanami_component
14
+ hook_into_all
15
+ add_configuration_helpers
16
+ end
17
+
18
+ def register_events_adapter
19
+ ::Hanami::Events::Adapter.register(:cloud_pubsub) do
20
+ require 'hanami/events/adapter/cloud_pubsub'
21
+ ::Hanami::Events::Adapter::CloudPubsub
22
+ end
23
+ end
24
+
25
+ def register_hanami_component
26
+ ::Hanami::Components.register 'events' do
27
+ requires 'logger', 'code'
28
+
29
+ prepare { require 'hanami/events/cloud_pubsub' }
30
+
31
+ resolve do |conf|
32
+ CloudPubsub.configure do |config|
33
+ conf.cloud_pubsub.each { |blk| blk.call(config) }
34
+ end
35
+
36
+ ::Hanami::Events.initialize(
37
+ :cloud_pubsub,
38
+ pubsub: Google::Cloud::Pubsub.new,
39
+ logger: Hanami::Components['logger']
40
+ )
41
+ end
42
+ end
43
+ end
44
+
45
+ def hook_into_all
46
+ require 'hanami/components'
47
+ # Unfortunately, hanami does not provide a way to add the component
48
+ # requirements easily, so we take the old requirements and append 'events'
49
+ requirements_for_all = ::Hanami::Components.component('all').send(:requirements)
50
+
51
+ ::Hanami::Components.register 'all' do
52
+ requires(*requirements_for_all, 'events')
53
+ resolve { true }
54
+ end
55
+ end
56
+
57
+ def add_configuration_helpers
58
+ require 'hanami/utils/load_paths'
59
+ require 'hanami/configuration'
60
+ ::Hanami::Configuration.include(Integration::Configuration)
61
+ ::Hanami.extend Integration::EasyAccess
62
+ end
63
+ end
64
+
65
+ # Extra configuration methods for Hanami
66
+ #
67
+ # @example Use emulator in development
68
+ # # config/environment.rb
69
+ # Hanami.configure do
70
+ # environment :development do
71
+ # cloud_pubsub do |conf|
72
+ # conf.pubsub = { project_id: 'emulator' }
73
+ # end
74
+ # end
75
+ # end
76
+ module Configuration
77
+ def cloud_pubsub(&blk)
78
+ if block_given?
79
+ settings[:cloud_pubsub] ||= []
80
+ settings[:cloud_pubsub] << blk
81
+ end
82
+
83
+ settings[:cloud_pubsub]
84
+ end
85
+ end
86
+
87
+ # Easy access to events, i.e. Hanami.events
88
+ module EasyAccess
89
+ def events
90
+ Hanami::Components['events']
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'hanami/components'
4
+ require 'hanami/configuration'
5
+ require_relative 'integration'
6
+
7
+ module Hanami
8
+ module Events
9
+ module CloudPubsub
10
+ # Register all the things needed to integrate with hanami
11
+ module Register
12
+ Integration::Setup.setup
13
+ end
14
+ end
15
+ end
16
+ end
@@ -3,7 +3,7 @@
3
3
  module Hanami
4
4
  module Events
5
5
  module CloudPubsub
6
- VERSION = '1.0.0'
6
+ VERSION = '1.0.1'
7
7
  end
8
8
  end
9
9
  end
@@ -6,7 +6,6 @@ require 'hanami/events/cloud_pubsub/middleware/stack'
6
6
  require 'hanami/events/cloud_pubsub/middleware/logging'
7
7
  require 'hanami/events/cloud_pubsub/middleware/auto_acknowledge'
8
8
  require 'hanami/events/cloud_pubsub/runner'
9
- require 'hanami/events/cloud_pubsub/mixin'
10
9
  require 'google/cloud/pubsub'
11
10
  require 'dry-configurable'
12
11
 
@@ -23,6 +22,11 @@ module Hanami
23
22
  setting :push, 4
24
23
  end
25
24
  end
25
+
26
+ setting :pubsub do |conf_hash|
27
+ conf_hash.each { |key, val| Google::Cloud::Pubsub.configure[key] = val }
28
+ end
29
+
26
30
  setting :project_id, reader: true
27
31
  setting :logger, Logger.new(STDOUT), reader: true
28
32
  setting :subscriptions_loader, proc {
@@ -53,14 +57,6 @@ module Hanami
53
57
  Middleware::Logging.new,
54
58
  Middleware::AutoAcknowledge.new
55
59
  )
56
-
57
- def self.setup
58
- Hanami::Events::Adapter.register(:cloud_pubsub) do
59
- require_relative 'adapter/cloud_pubsub'
60
-
61
- ::Hanami::Events::Adapter::CloudPubsub
62
- end
63
- end
64
60
  end
65
61
  end
66
62
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-events-cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
@@ -139,11 +139,12 @@ files:
139
139
  - lib/hanami/events/cloud_pubsub/cli.rb
140
140
  - lib/hanami/events/cloud_pubsub/handler.rb
141
141
  - lib/hanami/events/cloud_pubsub/health_check_server.rb
142
+ - lib/hanami/events/cloud_pubsub/integration.rb
142
143
  - lib/hanami/events/cloud_pubsub/listener.rb
143
144
  - lib/hanami/events/cloud_pubsub/middleware/auto_acknowledge.rb
144
145
  - lib/hanami/events/cloud_pubsub/middleware/logging.rb
145
146
  - lib/hanami/events/cloud_pubsub/middleware/stack.rb
146
- - lib/hanami/events/cloud_pubsub/mixin.rb
147
+ - lib/hanami/events/cloud_pubsub/register.rb
147
148
  - lib/hanami/events/cloud_pubsub/runner.rb
148
149
  - lib/hanami/events/cloud_pubsub/safe_error_handler.rb
149
150
  - lib/hanami/events/cloud_pubsub/thread_inspector.rb
@@ -169,7 +170,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
169
170
  version: '0'
170
171
  requirements: []
171
172
  rubyforge_project:
172
- rubygems_version: 2.7.7
173
+ rubygems_version: 2.6.14
173
174
  signing_key:
174
175
  specification_version: 4
175
176
  summary: Google Cloud Pub/Sub adapter for the hanami-events gem
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Hanami
4
- module Events
5
- module CloudPubsub
6
- # Mixin that extends class by `subscribe_to` method.
7
- #
8
- # @example
9
- # $events = Hanami::Events.initialize(:memory)
10
- #
11
- # class WelcomeMailer
12
- # include Hanami::Events::Mixin
13
- #
14
- # subscribe_to $events, 'user.created', id: 'welcome-mailer'
15
- #
16
- # def call(payload)
17
- # # send email
18
- # end
19
- # end
20
- #
21
- # @since 0.1.0
22
- #
23
- # @api public
24
- module Mixin
25
- def self.included(klass)
26
- klass.extend(ClassMethods)
27
- end
28
-
29
- # Class interfaces
30
- module ClassMethods
31
- def subscribe_to(event_bus, event_name, id:)
32
- klass = self
33
- event_bus.subscribe(event_name, id: id) { |payload| klass.new.call(payload) }
34
- end
35
- end
36
- end
37
- end
38
- end
39
- end