pubsub_tie 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a07811f6de7eee6628bff537126ed2186da058002ec43d2f5789278869a2b739
4
+ data.tar.gz: 5bfe2ac42674e2720d3aaf4cf5262befc892eb62d8959ffed827a90a49aa33a8
5
+ SHA512:
6
+ metadata.gz: e6eb7e15aa40bb9c19ceee94430269d256c5c0e36a4e02d4fe8a0387ea39c99590890e818ff5fc7b15d8fd4453770c9a9ab5e2ca144e992b7e37cd4e60d3cc93
7
+ data.tar.gz: 164bdc7080b020257cf852618dcd75540102767cd18bf84d1dd20510d1c626bf4b0f103f20255072b41af972641bff2aa33ef7af804513b8d18eabbc1dffce55
@@ -0,0 +1,31 @@
1
+ module PubSubTie
2
+ module Events
3
+ extend self
4
+
5
+ def configure(config)
6
+ @prefix = config['app_prefix']
7
+
8
+ evs = config['events'].map{|e| e['name']}
9
+ @events = Hash[evs.map(&:to_sym).zip(config['events'])]
10
+ end
11
+
12
+ # Full event name from symbol protecting from typos
13
+ # Raises KeyError if bad symbol
14
+ def name(sym)
15
+ "#{@prefix}-#{value(sym, 'name')}"
16
+ end
17
+
18
+ def required(sym)
19
+ (value(sym, 'required') || []).map(&:to_sym)
20
+ end
21
+
22
+ def optional(sym)
23
+ (value(sym, 'optional') || []).map(&:to_sym)
24
+ end
25
+
26
+ private
27
+ def value(sym, key)
28
+ @events.fetch(sym)[key]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,52 @@
1
+ require 'google/cloud/pubsub'
2
+
3
+ module PubSubTie
4
+ module Publisher
5
+ extend self
6
+
7
+ def configure(config)
8
+ @pubsub = google_pubsub(config)
9
+ end
10
+
11
+ def google_pubsub(config)
12
+ keyfile = File.join(PubSubTie.app_root, 'config', config['keyfile'])
13
+ creds = Google::Cloud::PubSub::Credentials.new keyfile
14
+
15
+ Google::Cloud::PubSub.new(project_id: config['project_id'],
16
+ credentials: creds)
17
+ end
18
+
19
+ def publish(topic_sym, data, resource)
20
+ @pubsub.
21
+ topic(Events.name topic_sym).
22
+ # publish(message(data, resource), publish_time: Time.now.utc)
23
+ publish_async(message(validate_data(topic_sym, data), resource),
24
+ publish_time: Time.now.utc) do |result|
25
+ unless result.succeeded?
26
+ Rails.logger.error(
27
+ "Failed to publish #{data} to #{topic_name} on #{resource} due to #{result.error}")
28
+ end
29
+ end
30
+ end
31
+
32
+ private
33
+ def message(data, resource)
34
+ # TODO: embed resource in message
35
+ data.to_json
36
+ end
37
+
38
+ def validate_data(sym, data)
39
+ missing = missing_required(sym, data)
40
+ unless missing.empty?
41
+ raise ArgumentError.new(
42
+ "Missing event required args for #{sym}: #{missing}")
43
+ end
44
+
45
+ data.slice(*(Events.required(sym) + Events.optional(sym)))
46
+ end
47
+
48
+ def missing_required(sym, data)
49
+ Events.required(sym) - data.keys
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ module PubSubTie
2
+ class Railtie < Rails::Railtie
3
+ initializer 'configure PubSubTie' do
4
+ PubSubTie.configure
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module PubSubTie
2
+ VERSION = "0.0.1"
3
+ end
data/lib/pubsub_tie.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'yaml'
2
+ require 'logger'
3
+
4
+ require 'pubsub_tie/events'
5
+ require 'pubsub_tie/publisher'
6
+
7
+ module PubSubTie
8
+ extend self
9
+
10
+ attr_writer :logger
11
+
12
+ def configure
13
+ configure_publisher
14
+ configure_events
15
+ end
16
+
17
+ def app_root
18
+ Dir.pwd
19
+ end
20
+
21
+ def logger
22
+ @logger ||= defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
23
+ end
24
+
25
+ def env
26
+ @env ||= defined?(Rails) ? Rails.env : ENV["ENV"] || 'developent'
27
+ end
28
+
29
+ def configure_publisher
30
+ config = YAML.load_file(File.join(app_root, 'config', 'gcp.yml'))[env]
31
+ Publisher.configure(config)
32
+ end
33
+
34
+ def configure_events
35
+ config = YAML.load_file(File.join(app_root, 'config', 'events.yml'))
36
+ Events.configure(config)
37
+ end
38
+
39
+ def publish(topic, data, resource: nil)
40
+ Publisher.publish(topic, data, resource)
41
+ end
42
+ end
43
+
44
+ require 'pubsub_tie/railtie' if defined? Rails::Railtie
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pubsub_tie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pablo Calderon
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-pubsub
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: ''
70
+ email:
71
+ - pablo@auddia.dev
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/pubsub_tie.rb
77
+ - lib/pubsub_tie/events.rb
78
+ - lib/pubsub_tie/publisher.rb
79
+ - lib/pubsub_tie/railtie.rb
80
+ - lib/pubsub_tie/version.rb
81
+ homepage: https://github.com/ClipInteractive/pubsub-tie
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '2.1'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubygems_version: 3.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Hook for Google PubSub for publication of events enforcing autoimposed rules
104
+ test_files: []