pubsub_tie 0.0.2 → 1.0.0

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: 8cc8c5620bfda35e60f358ea6b2cdee4e800c027d1ca9c405dbce3ce861be5fe
4
- data.tar.gz: f8d6f1c5b6bb267e64dca4fedb13cf48aa2a7cc7cef10899c4711809fa129471
3
+ metadata.gz: 34f6b84fac7ce4deacc85da202eb97c5991a8d39f8cb655eae8f1aff487316d9
4
+ data.tar.gz: 874c65f960fd033f90b70262f7dc06e282470a31190351b8fafeea01e924ec37
5
5
  SHA512:
6
- metadata.gz: 22f689074bb1dde17cfd75de1a40fdc9a6efe53cd1f6456a76a6b30ea1726a85ba4c526b3c2552904764fd2b4788a177e32847c2fd4d89e0ffae48f1ede5caff
7
- data.tar.gz: 700eedab10a24a766b278a677c4cd582a70e151fdcfd510784811be9191478abad8a12e68d91bf7c532a0222892ad39ddfbbee1fbb6f26c810f2098907188426
6
+ metadata.gz: 8653c31411d06e09597d7bab07e5ed27f196cc82f623f0848fcacc8397def04768df71122cb6d5254b946abf5bb140dde88677fe1ed4d176658606c268a9c1c1
7
+ data.tar.gz: e97c1edc4606b1e9f8662f54178b13853a74b323a29327fb5d51302bb7475220975a6fc5b9cbf04f103d0385ad0efcaf750b5d0843bb1ae3cd07eb9fc96590cb
@@ -7,25 +7,41 @@ module PubSubTie
7
7
 
8
8
  evs = config['events'].map{|e| e['name']}
9
9
  @events = Hash[evs.map(&:to_sym).zip(config['events'])]
10
+ @events.each do |k, evt|
11
+ fields = (evt['required'] || []) + (evt['optional'] || [])
12
+ evt['fields'] = Hash[ fields.map {|f| [f['name'], f['type']]} ]
13
+ end
10
14
  end
11
15
 
12
16
  # Full event name from symbol protecting from typos
13
17
  # Raises KeyError if bad symbol
18
+ def full_name(sym)
19
+ "#{@prefix}-#{name(sym)}"
20
+ end
21
+
14
22
  def name(sym)
15
- "#{@prefix}-#{value(sym, 'name')}"
23
+ value(sym, 'name')
16
24
  end
17
25
 
18
26
  def required(sym)
19
- (value(sym, 'required') || []).map(&:to_sym)
27
+ field_names(sym, 'required')
20
28
  end
21
29
 
22
30
  def optional(sym)
23
- (value(sym, 'optional') || []).map(&:to_sym)
31
+ field_names(sym, 'optional')
32
+ end
33
+
34
+ def types(sym)
35
+ value(sym, 'fields')
24
36
  end
25
37
 
26
38
  private
27
39
  def value(sym, key)
28
40
  @events.fetch(sym)[key]
29
41
  end
42
+
43
+ def field_names(sym, mode)
44
+ (value(sym, mode) || []).map {|field| field['name'].to_sym}
45
+ end
30
46
  end
31
47
  end
@@ -16,28 +16,36 @@ module PubSubTie
16
16
  credentials: creds)
17
17
  end
18
18
 
19
- def publish(topic_sym, data, resource)
19
+ #
20
+ # Publishes event data asynchronously to topic inferred from event_sym.
21
+ # Data is augmented with event_name and event_time and validated against
22
+ # loaded configuration
23
+ #
24
+ def publish(event_sym, data, resource)
25
+ message = augmented(data, event_sym)
26
+
20
27
  @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),
28
+ topic(Events.full_name event_sym).
29
+ # publish(message(payload, resource), publish_time: Time.now.utc)
30
+ publish_async(payload(validate_data(event_sym, message), resource),
24
31
  publish_time: Time.now.utc) do |result|
25
32
  unless result.succeeded?
26
33
  Rails.logger.error(
27
- "Failed to publish #{data} to #{topic_sym} on #{resource} due to #{result.error}")
34
+ "Failed to publish #{message} to #{event_sym} on #{resource} due to #{result.error}")
28
35
  end
29
36
  end
30
37
  end
31
38
 
32
- def batch(topic_sym, messages, resource)
39
+ def batch(event_sym, messages, resource)
33
40
  topic = @pubsub.
34
- topic(Events.name topic_sym)
41
+ topic(Events.full_name event_sym)
35
42
  messages.each do |data|
36
- topic.publish_async(message(validate_data(topic_sym, data), resource),
43
+ message = augmented(data, event_sym)
44
+ topic.publish_async(payload(validate_data(event_sym, message), resource),
37
45
  publish_time: Time.now.utc) do |result|
38
46
  unless result.succeeded?
39
47
  Rails.logger.error(
40
- "Failed to publish #{data} to #{topic_sym} on #{data} due to #{result.error}")
48
+ "Failed to publish #{message} to #{event_sym} on #{resource} due to #{result.error}")
41
49
  end
42
50
  end
43
51
  end
@@ -45,7 +53,7 @@ module PubSubTie
45
53
  end
46
54
 
47
55
  private
48
- def message(data, resource)
56
+ def payload(data, resource)
49
57
  # TODO: embed resource in message
50
58
  data.to_json
51
59
  end
@@ -57,11 +65,44 @@ module PubSubTie
57
65
  "Missing event required args for #{sym}: #{missing}")
58
66
  end
59
67
 
60
- data.slice(*(Events.required(sym) + Events.optional(sym)))
68
+ validate_types(sym,
69
+ data.slice(*(Events.required(sym) + Events.optional(sym))))
61
70
  end
62
71
 
63
72
  def missing_required(sym, data)
64
73
  Events.required(sym) - data.keys
65
74
  end
75
+
76
+ def augmented(data, event_sym)
77
+ {event_name: Events.name(event_sym),
78
+ event_time: Time.current.utc}.merge(data.to_hash.to_options)
79
+ end
80
+
81
+ def validate_types(sym, data)
82
+ types = Events.types(sym)
83
+
84
+ data.each do |field, val|
85
+ case val
86
+ when String
87
+ bad_type(field, data) unless types[field.to_s] == "STRING"
88
+ when Integer
89
+ bad_type(field, data) unless ["INT", "FLOAT"].include? types[field.to_s]
90
+ when Numeric
91
+ bad_type(field, data) unless types[field.to_s] == "FLOAT"
92
+ when Time
93
+ bad_type(field, data) unless types[field.to_s] == "TIMESTAMP"
94
+ when DateTime
95
+ bad_type(field, data) unless types[field.to_s] == "DATETIME"
96
+ else
97
+ bad_type(field, data)
98
+ end
99
+ end
100
+
101
+ data
102
+ end
103
+
104
+ def bad_type(field, data)
105
+ raise ArgumentError.new("Bad type for field #{field} in event #{data}")
106
+ end
66
107
  end
67
108
  end
@@ -1,3 +1,3 @@
1
1
  module PubSubTie
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pubsub_tie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Calderon
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-16 00:00:00.000000000 Z
11
+ date: 2021-03-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-pubsub
@@ -24,20 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rake
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
45
  - - "~>"
32
46
  - !ruby/object:Gem::Version
33
- version: '10.0'
47
+ version: '13.0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
52
  - - "~>"
39
53
  - !ruby/object:Gem::Version
40
- version: '10.0'
54
+ version: '13.0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -82,7 +96,7 @@ homepage: https://github.com/ClipInteractive/pubsub-tie
82
96
  licenses:
83
97
  - MIT
84
98
  metadata: {}
85
- post_install_message:
99
+ post_install_message:
86
100
  rdoc_options: []
87
101
  require_paths:
88
102
  - lib
@@ -97,8 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
97
111
  - !ruby/object:Gem::Version
98
112
  version: '0'
99
113
  requirements: []
100
- rubygems_version: 3.0.6
101
- signing_key:
114
+ rubygems_version: 3.0.3
115
+ signing_key:
102
116
  specification_version: 4
103
117
  summary: Hook for Google PubSub for publication of events enforcing autoimposed rules
104
118
  test_files: []