artsy-eventservice 0.1.3 → 1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +37 -0
- data/.rubocop_todo.yml +51 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +38 -2
- data/README.md +23 -9
- data/Rakefile +2 -1
- data/artsy-eventservice.gemspec +2 -1
- data/lib/artsy-eventservice.rb +8 -1
- data/lib/artsy-eventservice/artsy/event_service.rb +9 -31
- data/lib/artsy-eventservice/artsy/event_service/publisher.rb +31 -0
- data/lib/artsy-eventservice/artsy/event_service/rabbitmq_connection.rb +47 -0
- data/lib/artsy-eventservice/config.rb +42 -0
- data/lib/artsy-eventservice/presenters/events/base_event.rb +18 -15
- data/lib/artsy-eventservice/version.rb +2 -2
- data/spec/artsy-eventstream/artsy/event_service_spec.rb +18 -46
- data/spec/artsy-eventstream/artsy/publisher_spec.rb +52 -0
- data/spec/artsy-eventstream/config_spec.rb +12 -0
- data/spec/artsy-eventstream/presenters/events/base_event_spec.rb +1 -0
- data/spec/artsy-eventstream/version_spec.rb +2 -1
- data/spec/spec_helper.rb +5 -0
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 63ed0b9d24b6e226c04fff9db41d7e0b7264c8fb
|
4
|
+
data.tar.gz: 4b6a129facf45a7e6bc63cf8bd87094605e0c3f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27ea5fa51ac96e15220a7da0fedd08b3e07eae5203fb2174d2108b9344b78323d8036899cefae88ea00fdee84b08ffbeee67c3577dcc64827c6c8d17177d7a8d
|
7
|
+
data.tar.gz: 36f1f43d6654f7cea29a7db99975bc05779bfa4f726fc2e7bab6ea17a37a7f2fa066b38a90607ae20526e64f76207a56f137a0d314ce5f44a93022bdcd84eeca
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Documentation:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
TargetRubyVersion: 2.3
|
6
|
+
Exclude:
|
7
|
+
- Gemfile
|
8
|
+
|
9
|
+
Metrics/LineLength:
|
10
|
+
Max: 120
|
11
|
+
Exclude:
|
12
|
+
- spec/**/*
|
13
|
+
|
14
|
+
Metrics/BlockLength:
|
15
|
+
CountComments: false # count full line comments?
|
16
|
+
Max: 25
|
17
|
+
ExcludedMethods:
|
18
|
+
- describe
|
19
|
+
- context
|
20
|
+
- it
|
21
|
+
- fake!
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 15
|
25
|
+
|
26
|
+
# Offense count: 1
|
27
|
+
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
|
28
|
+
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
|
29
|
+
Style/FileName:
|
30
|
+
Exclude:
|
31
|
+
- 'lib/artsy-eventservice.rb'
|
32
|
+
|
33
|
+
# Offense count: 2
|
34
|
+
# Configuration parameters: MinBodyLength.
|
35
|
+
Style/GuardClause:
|
36
|
+
Exclude:
|
37
|
+
- 'lib/artsy-eventservice/presenters/events/base_event.rb'
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-03-23 14:07:50 -0400 using RuboCop version 0.47.1.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 4
|
10
|
+
Lint/IneffectiveAccessModifier:
|
11
|
+
Exclude:
|
12
|
+
- 'lib/artsy-eventservice/artsy/event_service.rb'
|
13
|
+
|
14
|
+
# Offense count: 1
|
15
|
+
# Configuration parameters: ContextCreatingMethods, MethodCreatingMethods.
|
16
|
+
Lint/UselessAccessModifier:
|
17
|
+
Exclude:
|
18
|
+
- 'lib/artsy-eventservice/artsy/event_service.rb'
|
19
|
+
|
20
|
+
# Offense count: 1
|
21
|
+
Metrics/AbcSize:
|
22
|
+
Max: 18
|
23
|
+
|
24
|
+
# Offense count: 4
|
25
|
+
# Configuration parameters: CountComments, ExcludedMethods.
|
26
|
+
Metrics/BlockLength:
|
27
|
+
Max: 52
|
28
|
+
|
29
|
+
# Offense count: 8
|
30
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
31
|
+
# URISchemes: http, https
|
32
|
+
Metrics/LineLength:
|
33
|
+
Max: 132
|
34
|
+
|
35
|
+
# Offense count: 1
|
36
|
+
# Configuration parameters: CountComments.
|
37
|
+
Metrics/MethodLength:
|
38
|
+
Max: 14
|
39
|
+
|
40
|
+
# Offense count: 1
|
41
|
+
# Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts, AllowedAcronyms.
|
42
|
+
# AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
|
43
|
+
Style/FileName:
|
44
|
+
Exclude:
|
45
|
+
- 'lib/artsy-eventservice.rb'
|
46
|
+
|
47
|
+
# Offense count: 2
|
48
|
+
# Configuration parameters: MinBodyLength.
|
49
|
+
Style/GuardClause:
|
50
|
+
Exclude:
|
51
|
+
- 'lib/artsy-eventservice/presenters/events/base_event.rb'
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.0
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
@@ -7,6 +7,12 @@ gemspec
|
|
7
7
|
gem 'rake'
|
8
8
|
gem 'bunny' # for producing/consuming evets from RabbitMQ
|
9
9
|
|
10
|
+
group :development do
|
11
|
+
gem 'rubocop', '~> 0.47.1', require: false
|
12
|
+
gem 'pry-nav'
|
13
|
+
gem 'pry-stack_explorer'
|
14
|
+
gem 'pry-rescue'
|
15
|
+
end
|
10
16
|
|
11
17
|
group :test do
|
12
18
|
gem 'rspec'
|
data/Gemfile.lock
CHANGED
@@ -1,16 +1,39 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
artsy-eventservice (
|
4
|
+
artsy-eventservice (1.0)
|
5
5
|
bunny (~> 2.6.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: http://rubygems.org/
|
9
9
|
specs:
|
10
10
|
amq-protocol (2.0.1)
|
11
|
+
ast (2.3.0)
|
12
|
+
binding_of_caller (0.7.2)
|
13
|
+
debug_inspector (>= 0.0.1)
|
11
14
|
bunny (2.6.2)
|
12
15
|
amq-protocol (>= 2.0.1)
|
16
|
+
coderay (1.1.1)
|
17
|
+
debug_inspector (0.0.2)
|
13
18
|
diff-lcs (1.2.5)
|
19
|
+
interception (0.5)
|
20
|
+
method_source (0.8.2)
|
21
|
+
parser (2.4.0.0)
|
22
|
+
ast (~> 2.2)
|
23
|
+
powerpack (0.1.1)
|
24
|
+
pry (0.10.4)
|
25
|
+
coderay (~> 1.1.0)
|
26
|
+
method_source (~> 0.8.1)
|
27
|
+
slop (~> 3.4)
|
28
|
+
pry-nav (0.2.4)
|
29
|
+
pry (>= 0.9.10, < 0.11.0)
|
30
|
+
pry-rescue (1.4.5)
|
31
|
+
interception (>= 0.5)
|
32
|
+
pry
|
33
|
+
pry-stack_explorer (0.4.9.2)
|
34
|
+
binding_of_caller (>= 0.7)
|
35
|
+
pry (>= 0.9.11)
|
36
|
+
rainbow (2.2.1)
|
14
37
|
rake (12.0.0)
|
15
38
|
rspec (3.5.0)
|
16
39
|
rspec-core (~> 3.5.0)
|
@@ -25,6 +48,15 @@ GEM
|
|
25
48
|
diff-lcs (>= 1.2.0, < 2.0)
|
26
49
|
rspec-support (~> 3.5.0)
|
27
50
|
rspec-support (3.5.0)
|
51
|
+
rubocop (0.47.1)
|
52
|
+
parser (>= 2.3.3.1, < 3.0)
|
53
|
+
powerpack (~> 0.1)
|
54
|
+
rainbow (>= 1.99.1, < 3.0)
|
55
|
+
ruby-progressbar (~> 1.7)
|
56
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
57
|
+
ruby-progressbar (1.8.1)
|
58
|
+
slop (3.6.0)
|
59
|
+
unicode-display_width (1.1.3)
|
28
60
|
|
29
61
|
PLATFORMS
|
30
62
|
ruby
|
@@ -32,12 +64,16 @@ PLATFORMS
|
|
32
64
|
DEPENDENCIES
|
33
65
|
artsy-eventservice!
|
34
66
|
bunny
|
67
|
+
pry-nav
|
68
|
+
pry-rescue
|
69
|
+
pry-stack_explorer
|
35
70
|
rake
|
36
71
|
rspec
|
37
72
|
rspec-mocks
|
73
|
+
rubocop (~> 0.47.1)
|
38
74
|
|
39
75
|
RUBY VERSION
|
40
76
|
ruby 2.3.0p0
|
41
77
|
|
42
78
|
BUNDLED WITH
|
43
|
-
1.
|
79
|
+
1.14.6
|
data/README.md
CHANGED
@@ -4,10 +4,28 @@ Ruby Gem for producing events in Artsy's event stream.
|
|
4
4
|
## Installation
|
5
5
|
Add following line to your Gemfile
|
6
6
|
|
7
|
-
```
|
7
|
+
```ruby
|
8
8
|
gem 'artsy-eventservice'
|
9
9
|
```
|
10
10
|
|
11
|
+
## Configuration
|
12
|
+
|
13
|
+
Add `artsy_eventservice.rb under config/initializers. `Artsy::EventService` uses [Bunny](http://rubybunny.info/) to connect to RabbitMQ. Here is a sample of configuration:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
# config/initializers/artsy_eventservice.rb
|
17
|
+
Artsy::EventService.configure do |config|
|
18
|
+
config.app_name = 'my-app' # Used for RabbitMQ queue name
|
19
|
+
config.event_stream_enabled = true # Boolean used to enable/disable posting events
|
20
|
+
config.rabbitmq_url = 'amqp(s)://<user>:<pass>@<host>:<port>/<vhost>' # required
|
21
|
+
config.tls = true # required, Currently only supports TLS enabled
|
22
|
+
config.tls_ca_certificate = <base64 strict decoded>
|
23
|
+
config.tls_cert = <base64 strict decoded>
|
24
|
+
config.tls_key = <base64 strict decoded>
|
25
|
+
config.verify_peer = true # Boolean used to decide in case we are using tls, we should verify peer or not
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
11
29
|
## Usage
|
12
30
|
Create events by inheriting from `Events::BaseEvent`. Override the properties that are different than `BaseEvent` and set extra `properties`.
|
13
31
|
|
@@ -30,15 +48,8 @@ module Events
|
|
30
48
|
end
|
31
49
|
```
|
32
50
|
|
33
|
-
`Artsy::EventService` uses [Bunny](http://rubybunny.info/) to securly connect to RabbitMQ over ssl, make sure following environment variables are set in your project:
|
34
|
-
```
|
35
|
-
RABBITMQ_URL="something like amqps://<user>:<pass>@rabbitmq.artsy.net:<port>/<vhost>"
|
36
|
-
RABBITMQ_CLIENT_CERT=base64 strict decoded
|
37
|
-
RABBTIMQ_CLIENT_KEY=base64 strict decoded
|
38
|
-
RABBITMQ_CA_CERT=base64 strict decoded
|
39
|
-
```
|
40
51
|
|
41
|
-
###
|
52
|
+
### Enabling Posting Events
|
42
53
|
We have a feature flag setup for enabling/disabling EventService. Setting `EVENT_STREAM_ENABLED` env variable will enable posting events. Not having this env means event service is disabled and no events will actually be sent.
|
43
54
|
|
44
55
|
|
@@ -49,6 +60,9 @@ Artsy::EventService.post_event(topic: 'testing', event: event_model)
|
|
49
60
|
```
|
50
61
|
|
51
62
|
|
63
|
+
### Update to Version 1.0
|
64
|
+
In previous versions this gem was using Environment variables for configuration. On version 1.0, configuration step is now mandatory and it will no longer read environment variables directly. Make sure to go to configuration step.
|
65
|
+
|
52
66
|
# Contributing
|
53
67
|
|
54
68
|
* Fork the project.
|
data/Rakefile
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'rubygems'
|
2
3
|
require 'bundler/gem_tasks'
|
3
4
|
Bundler.setup(:default, :development)
|
@@ -9,4 +10,4 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
9
10
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
10
11
|
end
|
11
12
|
|
12
|
-
task :
|
13
|
+
task default: :spec
|
data/artsy-eventservice.gemspec
CHANGED
data/lib/artsy-eventservice.rb
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'base64'
|
3
|
+
|
1
4
|
require 'artsy-eventservice/version'
|
2
|
-
require 'artsy-eventservice/
|
5
|
+
require 'artsy-eventservice/config'
|
3
6
|
require 'artsy-eventservice/presenters/events/base_event'
|
7
|
+
|
8
|
+
require 'artsy-eventservice/artsy/event_service'
|
9
|
+
require 'artsy-eventservice/artsy/event_service/rabbitmq_connection.rb'
|
10
|
+
require 'artsy-eventservice/artsy/event_service/publisher'
|
@@ -1,40 +1,18 @@
|
|
1
|
-
require 'bunny'
|
2
1
|
|
2
|
+
# frozen_string_literal: true
|
3
3
|
module Artsy
|
4
4
|
module EventService
|
5
|
-
def self.
|
6
|
-
|
7
|
-
|
8
|
-
if tls
|
9
|
-
params.merge!({
|
10
|
-
tls_cert: Base64.decode64(ENV['RABBITMQ_CLIENT_CERT']),
|
11
|
-
tls_key: Base64.decode64(ENV['RABBITMQ_CLIENT_KEY']),
|
12
|
-
tls_ca_certificates: [Base64.decode64(ENV['RABBITMQ_CA_CERT'])],
|
13
|
-
verify_peer: ENV['RABBITMQ_NO_VERIFY_PEER'] == 'true' ? false : true
|
14
|
-
})
|
15
|
-
end
|
16
|
-
Bunny.new(ENV['RABBITMQ_URL'], **params)
|
5
|
+
def self.post_event(topic:, event:)
|
6
|
+
return unless event_stream_enabled?
|
7
|
+
Publisher.publish(topic: topic, event: event)
|
17
8
|
end
|
18
9
|
|
19
|
-
def self.
|
20
|
-
|
21
|
-
raise 'Event missing topic or verb.' if event.verb.to_s.empty? || topic.to_s.empty?
|
22
|
-
conn = create_conn
|
23
|
-
conn.start
|
24
|
-
channel = conn.create_channel
|
25
|
-
exchange = channel.topic(topic, durable: true)
|
26
|
-
exchange.publish(
|
27
|
-
event.json,
|
28
|
-
routing_key: event.verb,
|
29
|
-
persistent: true,
|
30
|
-
content_type: 'application/json',
|
31
|
-
app_id: app_name
|
32
|
-
)
|
33
|
-
conn.close
|
10
|
+
def self.consume(**args)
|
11
|
+
raise 'Not implemented- try Sneakers'
|
34
12
|
end
|
35
13
|
|
36
|
-
def self.
|
37
|
-
|
14
|
+
def self.event_stream_enabled?
|
15
|
+
Artsy::EventService.config.event_stream_enabled
|
38
16
|
end
|
39
17
|
end
|
40
|
-
end
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Artsy
|
3
|
+
module EventService
|
4
|
+
class Publisher
|
5
|
+
include RabbitMQConnection
|
6
|
+
|
7
|
+
def self.publish(topic:, event:)
|
8
|
+
new.post_event(topic: topic, event: event)
|
9
|
+
end
|
10
|
+
|
11
|
+
def post_event(topic:, event:)
|
12
|
+
raise 'Event missing topic or verb.' if event.verb.to_s.empty? || topic.to_s.empty?
|
13
|
+
connect_to_rabbit do |conn|
|
14
|
+
channel = conn.create_channel
|
15
|
+
exchange = channel.topic(topic, durable: true)
|
16
|
+
exchange.publish(
|
17
|
+
event.json,
|
18
|
+
routing_key: event.verb,
|
19
|
+
persistent: true,
|
20
|
+
content_type: 'application/json',
|
21
|
+
app_id: config.app_name
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.config
|
27
|
+
Artsy::EventService.config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'bunny'
|
3
|
+
|
4
|
+
module Artsy
|
5
|
+
module EventService
|
6
|
+
module RabbitMQConnection
|
7
|
+
# get a new RabbitMQ Client
|
8
|
+
def create_conn
|
9
|
+
Bunny.new(rabbitmq_url, **bunny_params)
|
10
|
+
end
|
11
|
+
|
12
|
+
# Connect, do something and close the connection
|
13
|
+
def connect_to_rabbit
|
14
|
+
conn = create_conn
|
15
|
+
conn.start
|
16
|
+
yield(conn)
|
17
|
+
conn.stop
|
18
|
+
end
|
19
|
+
|
20
|
+
def rabbitmq_url
|
21
|
+
config.rabbitmq_url
|
22
|
+
end
|
23
|
+
|
24
|
+
def bunny_params
|
25
|
+
config.tls ? tls_params : no_tls_params
|
26
|
+
end
|
27
|
+
|
28
|
+
def tls_params
|
29
|
+
{
|
30
|
+
tls: config.tls,
|
31
|
+
tls_cert: config.tls_cert,
|
32
|
+
tls_key: config.tls_key,
|
33
|
+
tls_ca_certificates: [config.tls_ca_certificate],
|
34
|
+
verify_peer: config.verify_peer
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def no_tls_params
|
39
|
+
raise 'not implemented- TLS only'
|
40
|
+
end
|
41
|
+
|
42
|
+
def config
|
43
|
+
Artsy::EventService.config
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Artsy
|
4
|
+
module EventService
|
5
|
+
module Config
|
6
|
+
extend self
|
7
|
+
|
8
|
+
attr_accessor :app_name
|
9
|
+
attr_accessor :event_stream_enabled
|
10
|
+
attr_accessor :rabbitmq_url
|
11
|
+
attr_accessor :tls
|
12
|
+
attr_accessor :tls_ca_certificate
|
13
|
+
attr_accessor :tls_cert
|
14
|
+
attr_accessor :tls_key
|
15
|
+
attr_accessor :verify_peer
|
16
|
+
|
17
|
+
def reset
|
18
|
+
self.app_name = nil
|
19
|
+
self.event_stream_enabled = false
|
20
|
+
self.rabbitmq_url = nil
|
21
|
+
self.tls = nil
|
22
|
+
self.tls_ca_certificate = nil
|
23
|
+
self.tls_cert = nil
|
24
|
+
self.tls_key = nil
|
25
|
+
self.verify_peer = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
reset
|
29
|
+
end
|
30
|
+
|
31
|
+
class << self
|
32
|
+
def configure
|
33
|
+
yield(Config) if block_given?
|
34
|
+
Config
|
35
|
+
end
|
36
|
+
|
37
|
+
def config
|
38
|
+
Config
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'json'
|
2
3
|
|
3
4
|
module Events
|
@@ -11,27 +12,29 @@ module Events
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def subject
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
if @subject
|
16
|
+
{
|
17
|
+
id: @subject.id.to_s || '',
|
18
|
+
display: @subject.to_s
|
19
|
+
}
|
20
|
+
end
|
18
21
|
end
|
19
22
|
|
20
23
|
def object
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
if @object
|
25
|
+
{
|
26
|
+
id: @object.id.to_s,
|
27
|
+
root_type: @object.class.to_s,
|
28
|
+
display: @object.to_s
|
29
|
+
}
|
30
|
+
end
|
26
31
|
end
|
27
32
|
|
28
33
|
def json
|
29
|
-
JSON.generate(
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
properties: properties
|
34
|
-
})
|
34
|
+
JSON.generate(verb: @verb,
|
35
|
+
subject: subject,
|
36
|
+
object: object,
|
37
|
+
properties: properties)
|
35
38
|
end
|
36
39
|
end
|
37
40
|
end
|
@@ -1,57 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'spec_helper'
|
2
|
-
require 'json'
|
3
3
|
|
4
4
|
describe Artsy::EventService do
|
5
|
-
let(:event) { double(
|
5
|
+
let(:event) { double('event', topic: 'foo', verb: 'bar') }
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
context 'event stream disabled' do
|
15
|
-
before do
|
16
|
-
ENV['EVENT_STREAM_ENABLED'] = nil
|
17
|
-
end
|
18
|
-
it 'does not call conn and post event' do
|
19
|
-
expect(Artsy::EventService).not_to receive(:conn)
|
7
|
+
context 'event stream disabled' do
|
8
|
+
before do
|
9
|
+
allow(Artsy::EventService).to receive(:config).and_return(double(event_stream_enabled: false))
|
10
|
+
end
|
11
|
+
describe '.post_event' do
|
12
|
+
it 'does not connect to rabbit' do
|
13
|
+
expect(Artsy::EventService::Publisher).not_to receive(:publish)
|
20
14
|
Artsy::EventService.post_event(topic: 'test', event: event)
|
21
15
|
end
|
22
16
|
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'event stream enabled' do
|
20
|
+
before do
|
21
|
+
allow(Artsy::EventService).to receive(:config).and_return(double(event_stream_enabled: true))
|
22
|
+
end
|
23
|
+
describe '.post_event' do
|
24
|
+
it 'does connect to rabbit' do
|
25
|
+
expect(Artsy::EventService::Publisher).to receive(:publish)
|
23
26
|
|
24
|
-
context 'event stream enabled' do
|
25
|
-
before do
|
26
|
-
ENV['EVENT_STREAM_ENABLED'] = 'true'
|
27
|
-
end
|
28
|
-
it 'fails when topic is empty' do
|
29
|
-
expect { Artsy::EventService.post_event(topic: nil, event: event)}.to raise_error 'Event missing topic or verb.'
|
30
|
-
end
|
31
|
-
it 'fails when event.verb is empty' do
|
32
|
-
allow(event).to receive(:verb).and_return('')
|
33
|
-
expect { Artsy::EventService.post_event(topic: 'test', event: event)}.to raise_error 'Event missing topic or verb.'
|
34
|
-
end
|
35
|
-
it 'fails when event.verb is nil' do
|
36
|
-
allow(event).to receive(:verb).and_return(nil)
|
37
|
-
expect { Artsy::EventService.post_event(topic: 'test', event: event)}.to raise_error 'Event missing topic or verb.'
|
38
|
-
end
|
39
|
-
it 'calls publish on the exchange with proper data' do
|
40
|
-
conn = double
|
41
|
-
expect(Artsy::EventService).to receive(:create_conn).once.and_return(conn)
|
42
|
-
expect(conn).to receive(:start).once
|
43
|
-
expect(conn).to receive(:close).once
|
44
|
-
channel = double
|
45
|
-
expect(conn).to receive(:create_channel).and_return(channel)
|
46
|
-
exchange = double
|
47
|
-
expect(channel).to receive(:topic).with('test', durable: true).and_return(exchange)
|
48
|
-
expect(exchange).to receive(:publish).with(
|
49
|
-
JSON.generate(hello: true),
|
50
|
-
routing_key: 'testing',
|
51
|
-
persistent: true,
|
52
|
-
content_type: 'application/json',
|
53
|
-
app_id: 'artsy'
|
54
|
-
)
|
55
27
|
Artsy::EventService.post_event(topic: 'test', event: event)
|
56
28
|
end
|
57
29
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
describe Artsy::EventService::Publisher do
|
6
|
+
let(:event) { double('event', topic: 'foo') }
|
7
|
+
|
8
|
+
before do
|
9
|
+
Artsy::EventService.configure do |config|
|
10
|
+
config.app_name = 'artsy'
|
11
|
+
config.event_stream_enabled = true
|
12
|
+
config.tls = true
|
13
|
+
end
|
14
|
+
allow(event).to receive_messages(
|
15
|
+
verb: 'testing',
|
16
|
+
json: JSON.generate(hello: true)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '.publish' do
|
21
|
+
it 'fails when topic is empty' do
|
22
|
+
expect { Artsy::EventService::Publisher.publish(topic: nil, event: event) }.to raise_error 'Event missing topic or verb.'
|
23
|
+
end
|
24
|
+
it 'fails when event.verb is empty' do
|
25
|
+
allow(event).to receive(:verb).and_return('')
|
26
|
+
expect { Artsy::EventService::Publisher.publish(topic: 'test', event: event) }.to raise_error 'Event missing topic or verb.'
|
27
|
+
end
|
28
|
+
it 'fails when event.verb is nil' do
|
29
|
+
allow(event).to receive(:verb).and_return(nil)
|
30
|
+
expect { Artsy::EventService::Publisher.publish(topic: 'test', event: event) }.to raise_error 'Event missing topic or verb.'
|
31
|
+
end
|
32
|
+
it 'calls publish on the exchange with proper data' do
|
33
|
+
conn = double
|
34
|
+
channel = double
|
35
|
+
exchange = double
|
36
|
+
allow(Bunny).to receive(:new).and_return(conn)
|
37
|
+
expect(conn).to receive(:start).once
|
38
|
+
expect(conn).to receive(:stop).once
|
39
|
+
allow(conn).to receive(:create_channel).and_return(channel)
|
40
|
+
allow(channel).to receive(:topic).with('test', durable: true).and_return(exchange)
|
41
|
+
|
42
|
+
expect(exchange).to receive(:publish).with(
|
43
|
+
JSON.generate(hello: true),
|
44
|
+
routing_key: 'testing',
|
45
|
+
persistent: true,
|
46
|
+
content_type: 'application/json',
|
47
|
+
app_id: 'artsy'
|
48
|
+
)
|
49
|
+
Artsy::EventService::Publisher.publish(topic: 'test', event: event)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'spec_helper'
|
3
|
+
describe Artsy::EventService::Config do
|
4
|
+
describe '.configure' do
|
5
|
+
%w(app_name event_stream_enabled rabbitmq_url tls tls_ca_certificate tls_cert tls_key verify_peer).each do |option|
|
6
|
+
it "allows setting and getting #{option}" do
|
7
|
+
Artsy::EventService.configure { |config| config.send("#{option}=", false) }
|
8
|
+
expect(Artsy::EventService.config.send(option.to_sym)).to be false
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: artsy-eventservice
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: '1.0'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashkan Nasseri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bunny
|
@@ -32,6 +32,9 @@ extra_rdoc_files: []
|
|
32
32
|
files:
|
33
33
|
- ".gitignore"
|
34
34
|
- ".rspec"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- ".rubocop_todo.yml"
|
37
|
+
- ".ruby-version"
|
35
38
|
- ".travis.yml"
|
36
39
|
- CHANGELOG.md
|
37
40
|
- Gemfile
|
@@ -42,9 +45,14 @@ files:
|
|
42
45
|
- artsy-eventservice.gemspec
|
43
46
|
- lib/artsy-eventservice.rb
|
44
47
|
- lib/artsy-eventservice/artsy/event_service.rb
|
48
|
+
- lib/artsy-eventservice/artsy/event_service/publisher.rb
|
49
|
+
- lib/artsy-eventservice/artsy/event_service/rabbitmq_connection.rb
|
50
|
+
- lib/artsy-eventservice/config.rb
|
45
51
|
- lib/artsy-eventservice/presenters/events/base_event.rb
|
46
52
|
- lib/artsy-eventservice/version.rb
|
47
53
|
- spec/artsy-eventstream/artsy/event_service_spec.rb
|
54
|
+
- spec/artsy-eventstream/artsy/publisher_spec.rb
|
55
|
+
- spec/artsy-eventstream/config_spec.rb
|
48
56
|
- spec/artsy-eventstream/presenters/events/base_event_spec.rb
|
49
57
|
- spec/artsy-eventstream/version_spec.rb
|
50
58
|
- spec/spec_helper.rb
|