artsy-eventservice 0.1.0

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
+ SHA1:
3
+ metadata.gz: f9768b5adc5dfcc97e16ce3cbfeb432d0e383ebb
4
+ data.tar.gz: 29ea4b53bdfb2a911707be6e2fa3ee71983c4efc
5
+ SHA512:
6
+ metadata.gz: 751b9e5b2e459f028cc6ca17ca821459688cea0b028c26fa1b884b5f2a99acb6a364f90a6bffbba90e87928c43bddf06bd86989ef91237594a90476e8be39755
7
+ data.tar.gz: 7adf8370810145efd73ac6e877dede30e30a5a14a473f3a68d824bae4ba0c8f7108121289e12c27164c7b7bc492b36ed45a330daeb06fcdb800de49ae9927242
data/.gitignore ADDED
@@ -0,0 +1,50 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ # Gemfile.lock
46
+ # .ruby-version
47
+ # .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+
3
+ cache: bundler
4
+
5
+ rvm:
6
+ - 2.3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.1.0 (12/29/2016)
2
+
3
+ * Initial public release - [@ashkan18](https://github.com/askan18).
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'http://rubygems.org'
2
+
3
+ ruby '2.3.0'
4
+
5
+ gemspec
6
+
7
+ gem 'rake'
8
+ gem 'bunny' # for producing/consuming evets from RabbitMQ
9
+
10
+
11
+ group :test do
12
+ gem 'rspec'
13
+ gem 'rspec-mocks'
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ artsy-eventservice (0.1.0)
5
+ bunny (~> 2.6.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ amq-protocol (2.0.1)
11
+ bunny (2.6.2)
12
+ amq-protocol (>= 2.0.1)
13
+ diff-lcs (1.2.5)
14
+ rake (12.0.0)
15
+ rspec (3.5.0)
16
+ rspec-core (~> 3.5.0)
17
+ rspec-expectations (~> 3.5.0)
18
+ rspec-mocks (~> 3.5.0)
19
+ rspec-core (3.5.4)
20
+ rspec-support (~> 3.5.0)
21
+ rspec-expectations (3.5.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.5.0)
24
+ rspec-mocks (3.5.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.5.0)
27
+ rspec-support (3.5.0)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ artsy-eventservice!
34
+ bunny
35
+ rake
36
+ rspec
37
+ rspec-mocks
38
+
39
+ RUBY VERSION
40
+ ruby 2.3.0p0
41
+
42
+ BUNDLED WITH
43
+ 1.12.5
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Artsy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Artsy EventService [![Build Status](https://travis-ci.org/artsy/artsy-eventservice.svg?branch=master)](https://travis-ci.org/artsy/artsy-eventservice)
2
+ Ruby Gem for producing events in Artsy's event stream.
3
+
4
+ ## Installation
5
+ Add following line to your Gemfile
6
+
7
+ ```
8
+ gem 'artsy-eventservice'
9
+ ```
10
+
11
+ ## Usage
12
+ Create events by inheriting from `Events::BaseEvent`. Override the properties that are different than `BaseEvent` and set extra `properties`.
13
+
14
+ ```ruby
15
+ module Events
16
+ class ConversationEvent < Events::BaseEvent
17
+ def subject
18
+ {
19
+ id: @object.to_id,
20
+ display: @object.to_name
21
+ }
22
+ end
23
+ end
24
+
25
+ def properties
26
+ {
27
+ test_prop: 'testing'
28
+ }
29
+ end
30
+ end
31
+ end
32
+ ```
33
+
34
+ `Artsy::EventService` uses [Bunny](http://rubybunny.info/) to securly connect to RabbitMQ over ssl, make sure following environment variables are set in your project:
35
+ ```
36
+ RABBITMQ_URL="something like amqps://<user>:<pass>@rabbitmq.artsy.net:<port>/<vhost>"
37
+ RABBITMQ_CLIENT_CERT=base64 strict decoded
38
+ RABBTIMQ_CLIENT_KEY=base64 strict decoded
39
+ RABBITMQ_CA_CERT=base64 strict decoded
40
+ ```
41
+
42
+ Call `post_event` with proper `topic` and `event`:
43
+ ```ruby
44
+ Artsy::EventService.post_event(topic: 'testing', event: event_model)
45
+ ```
46
+
47
+
48
+ # Contributing
49
+
50
+ * Fork the project.
51
+ * Make your feature addition or bug fix with tests.
52
+ * Update CHANGELOG.
53
+ * Send a pull request. Bonus points for topic branches.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'bundler/gem_tasks'
3
+ Bundler.setup(:default, :development)
4
+
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ task :default => :spec
@@ -0,0 +1,18 @@
1
+ $:.push File.expand_path('../lib', __FILE__)
2
+
3
+ require 'artsy-eventservice/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'artsy-eventservice'
7
+ s.version = Artsy::EventService::VERSION
8
+ s.authors = ['Ashkan Nasseri']
9
+ s.email = 'ashkan.nasseri@gmail.com'
10
+ s.platform = Gem::Platform::RUBY
11
+ s.required_rubygems_version = '>= 1.3.6'
12
+ s.files = `git ls-files`.split("\n")
13
+ s.require_paths = ['lib']
14
+ s.homepage = 'http://github.com/artsy/artsy-eventservice'
15
+ s.licenses = ['MIT']
16
+ s.summary = "Ruby Gem for producing events in Artsy's event stream"
17
+ s.add_dependency 'bunny', '~> 2.6.2'
18
+ end
@@ -0,0 +1,3 @@
1
+ require 'artsy-eventservice/version'
2
+ require 'artsy-eventservice/artsy/event_service'
3
+ require 'artsy-eventservice/presenters/events/base_event'
@@ -0,0 +1,36 @@
1
+ require 'bunny'
2
+
3
+ module Artsy
4
+ module EventService
5
+ def self.conn
6
+ @conn ||= Bunny.new(
7
+ ENV['RABBITMQ_URL'],
8
+ tls: true,
9
+ tls_cert: Base64.decode64(ENV['RABBITMQ_CLIENT_CERT'] || ''),
10
+ tls_key: Base64.decode64(ENV['RABBTIMQ_CLIENT_KEY'] || ''),
11
+ tls_ca_certificates: [Base64.decode64(ENV['RABBITMQ_CA_CERT'] || '')],
12
+ verify_peer: true
13
+ )
14
+ end
15
+
16
+ def self.post_event(topic:, event:)
17
+ return unless ENV['EVENT_STREAM_ENABLED']
18
+ raise 'Event missing topic or verb.' if event.verb.to_s.empty? || topic.to_s.empty?
19
+ conn.start
20
+ channel = conn.create_channel
21
+ exchange = channel.topic(topic, durable: true)
22
+ exchange.publish(
23
+ event.json,
24
+ routing_key: event.verb,
25
+ persistent: true,
26
+ content_type: 'application/json',
27
+ app_id: app_name
28
+ )
29
+ conn.close
30
+ end
31
+
32
+ def self.app_name
33
+ defined?(Rails) ? Rails.application.class.to_s.split('::').first : 'artsy'
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ require 'json'
2
+
3
+ module Events
4
+ class BaseEvent
5
+ attr_reader :verb, :subject, :object, :properties
6
+ def initialize(user: nil, action:, model:)
7
+ @subject = user
8
+ @verb = action
9
+ @object = model
10
+ @properties = nil
11
+ end
12
+
13
+ def subject
14
+ {
15
+ id: @subject.id.to_s || '',
16
+ display: @subject.to_s
17
+ } if @subject
18
+ end
19
+
20
+ def object
21
+ {
22
+ id: @object.id.to_s,
23
+ root_type: @object.class.to_s,
24
+ display: @object.to_s
25
+ } if @object
26
+ end
27
+
28
+ def json
29
+ JSON.generate({
30
+ verb: @verb,
31
+ subject: subject,
32
+ object: object,
33
+ properties: properties
34
+ })
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,6 @@
1
+ module Artsy
2
+ module EventService
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
6
+
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+ require 'json'
3
+
4
+ describe Artsy::EventService do
5
+ let(:event) { double("event") }
6
+
7
+ before do
8
+ allow(event).to receive_messages(
9
+ verb: 'testing',
10
+ json: JSON.generate(hello: true)
11
+ )
12
+ end
13
+ describe '#post_event' do
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)
20
+ Artsy::EventService.post_event(topic: 'test', event: event)
21
+ end
22
+ end
23
+
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(:conn).at_least(3).times.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
+ Artsy::EventService.post_event(topic: 'test', event: event)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+
3
+ describe Events::BaseEvent do
4
+ let(:model) { double('model') }
5
+ let(:user) { double('user') }
6
+ let(:event) { Events::BaseEvent.new(user: user, action: 'test', model: model) }
7
+ before do
8
+ allow(model).to receive_messages(
9
+ id: 'model-1',
10
+ to_s: 'Model 1'
11
+ )
12
+ allow(user).to receive_messages(
13
+ id: 'user-1',
14
+ to_s: 'User 1'
15
+ )
16
+ end
17
+ describe '#json' do
18
+ it 'returns proper json' do
19
+ expect(JSON.parse(event.json)).to match(
20
+ 'verb' => 'test',
21
+ 'subject' => {
22
+ 'id' => 'user-1',
23
+ 'display' => 'User 1'
24
+ },
25
+ 'object' => {
26
+ 'id' => 'model-1',
27
+ 'root_type' => 'RSpec::Mocks::Double',
28
+ 'display' => 'Model 1'
29
+ },
30
+ 'properties' => nil
31
+ )
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+ describe Artsy::EventService do
3
+ it 'has a version' do
4
+ Artsy::EventService::VERSION.should_not be_nil
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rubygems'
4
+ require 'rspec'
5
+ require 'artsy-eventservice'
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artsy-eventservice
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ashkan Nasseri
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.6.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.6.2
27
+ description:
28
+ email: ashkan.nasseri@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - ".rspec"
35
+ - ".travis.yml"
36
+ - CHANGELOG.md
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - artsy-eventservice.gemspec
43
+ - lib/artsy-eventservice.rb
44
+ - lib/artsy-eventservice/artsy/event_service.rb
45
+ - lib/artsy-eventservice/presenters/events/base_event.rb
46
+ - lib/artsy-eventservice/version.rb
47
+ - spec/artsy-eventstream/artsy/event_service_spec.rb
48
+ - spec/artsy-eventstream/presenters/events/base_event_spec.rb
49
+ - spec/artsy-eventstream/version_spec.rb
50
+ - spec/spec_helper.rb
51
+ homepage: http://github.com/artsy/artsy-eventservice
52
+ licenses:
53
+ - MIT
54
+ metadata: {}
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.3.6
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 2.4.8
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Ruby Gem for producing events in Artsy's event stream
75
+ test_files: []