cpi-event-connector 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .rvmrc
2
+ .bundle/
3
+
4
+ /.tags
5
+ /.tags_sorted_by_file
6
+ /tags
7
+ /pkg
8
+
9
+ /bin
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cpi-event-connector.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cpi-event-connector (0.0.2)
5
+ activesupport (~> 3.1.1)
6
+ bunny
7
+ uuidtools (~> 2.1.2)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ activesupport (3.1.4)
13
+ multi_json (~> 1.0)
14
+ bunny (0.7.9)
15
+ diff-lcs (1.1.3)
16
+ justinf-unification_assertion (0.0.2)
17
+ multi_json (1.1.0)
18
+ rspec (2.8.0)
19
+ rspec-core (~> 2.8.0)
20
+ rspec-expectations (~> 2.8.0)
21
+ rspec-mocks (~> 2.8.0)
22
+ rspec-core (2.8.0)
23
+ rspec-expectations (2.8.0)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.8.0)
26
+ uuidtools (2.1.2)
27
+
28
+ PLATFORMS
29
+ ruby
30
+
31
+ DEPENDENCIES
32
+ cpi-event-connector!
33
+ justinf-unification_assertion
34
+ rspec
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new :spec do |t|
6
+ end
7
+
8
+ task :default => [ :spec ]
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "cpi_event_connector/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "cpi-event-connector"
7
+ s.version = Cpi::VERSION
8
+ s.authors = ["Nulogy"]
9
+ s.email = ["engineering@nulogy.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Sends events to CPI}
12
+ s.description = %q{see summary}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ s.add_development_dependency "rspec"
21
+ s.add_development_dependency "justinf-unification_assertion"
22
+
23
+ s.add_runtime_dependency "bunny"
24
+ s.add_runtime_dependency "activesupport", "~> 3.1.1"
25
+ s.add_runtime_dependency "uuidtools", "~> 2.1.2"
26
+ end
@@ -0,0 +1,7 @@
1
+ require "cpi_event_connector/version"
2
+ require "cpi_event_connector/generator"
3
+ require "cpi_event_connector/connector"
4
+ require "cpi_event_connector/event"
5
+
6
+ module Cpi
7
+ end
@@ -0,0 +1,33 @@
1
+ require 'bunny'
2
+
3
+ module Cpi
4
+ class Connector
5
+ def initialize params={}
6
+ @queue_name = params.delete("queue")
7
+ @params = params
8
+ end
9
+
10
+ def start
11
+ @bunny = Bunny.new @params
12
+ @bunny.start
13
+ @bunny.tx_select
14
+ @exchange = @bunny.exchange('')
15
+
16
+ @queue = @bunny.queue(@queue_name, :durable => true)
17
+ end
18
+
19
+ def transmit(message)
20
+ begin
21
+ @exchange.publish(message, :key => @queue_name)
22
+ @bunny.tx_commit
23
+ rescue Bunny::ServerDownError, Bunny::ForcedConnectionCloseError, Bunny::ConnectionError
24
+ start
25
+ retry
26
+ end
27
+ end
28
+
29
+ def stop
30
+ @bunny.stop
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,19 @@
1
+ module Cpi
2
+ class Event
3
+
4
+ @@enabled = false
5
+
6
+ def self.configure(params)
7
+ @@enabled = !!params.delete("enabled")
8
+ return unless @@enabled
9
+ @@connector = Cpi::Connector.new(params)
10
+ @@connector.start
11
+ end
12
+
13
+ def self.transmit(event_type, tenant_uid, root_path)
14
+ return unless @@enabled
15
+ event_json = Cpi::Generator.create_event(event_type, tenant_uid, root_path)
16
+ @@connector.transmit(event_json)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support'
2
+ require 'uuidtools'
3
+
4
+ module Cpi
5
+ class Generator
6
+
7
+ def self.create_event(event_type, tenant_uid, root_path)
8
+ uid = UUIDTools::UUID.timestamp_create().to_s
9
+
10
+ hash = {"outbound_event" =>
11
+ {"event_type" => event_type, "tenant_uid" => tenant_uid, "root_path" => root_path, "uid" => uid}
12
+ }
13
+
14
+ json = ActiveSupport::JSON.encode(hash)
15
+
16
+ return json
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module Cpi
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+ require 'lib/cpi_event_connector/connector'
3
+ require 'bunny'
4
+
5
+ describe Cpi::Connector do
6
+
7
+ let(:bunny) { double(Bunny) }
8
+ let(:params) { {"host" => "", "port" => "", "username" => "", "password" => "", "queue" => "cpi"} }
9
+ let(:queue) { double("a queue")}
10
+ let(:exchange) { double("an exchange")}
11
+
12
+
13
+ before do
14
+ Bunny.stub(:new).and_return(bunny)
15
+ bunny.stub(:start)
16
+ bunny.stub(:queue).and_return(queue)
17
+ bunny.stub(:tx_select)
18
+ bunny.stub(:exchange).and_return(exchange)
19
+ end
20
+
21
+ context "before established connection" do
22
+ it "connects to an amqp server" do
23
+ Bunny.should_receive(:new).with(params).and_return(bunny)
24
+ bunny.should_receive(:start)
25
+
26
+ connector = Cpi::Connector.new params
27
+ connector.start
28
+ end
29
+
30
+ it "connects to the specified queue" do
31
+ bunny.should_receive(:queue).with("cpi", :durable => true).and_return(queue)
32
+
33
+ connector = Cpi::Connector.new params
34
+ connector.start
35
+ end
36
+
37
+ it "creates a transaction before sending an event" do
38
+ bunny.should_receive(:tx_select)
39
+
40
+ connector = Cpi::Connector.new params
41
+ connector.start
42
+ end
43
+
44
+ it "gets an exhange" do
45
+ bunny.should_receive(:exchange).with('').and_return(exchange)
46
+
47
+ connector = Cpi::Connector.new params
48
+ connector.start
49
+ end
50
+ end
51
+
52
+ context "with established connection" do
53
+ before do
54
+ @connector = Cpi::Connector.new params
55
+ @connector.start
56
+
57
+ exchange.stub(:publish)
58
+ bunny.stub(:tx_commit)
59
+ end
60
+
61
+ it "disconnects from an amqp server" do
62
+ bunny.should_receive(:stop)
63
+
64
+ @connector.stop
65
+ end
66
+
67
+ it "sends a message to an amqp server" do
68
+ event = "an event"
69
+ exchange.should_receive(:publish).with(event, :key => "cpi")
70
+
71
+ @connector.transmit(event)
72
+ end
73
+
74
+ it "commits the transaction after a message has been published" do
75
+ bunny.should_receive(:tx_commit)
76
+
77
+ @connector.transmit("event")
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'lib/cpi_event_connector'
3
+ require 'yaml'
4
+ require 'active_support'
5
+ require 'unification_assertion'
6
+
7
+ describe Cpi::Event do
8
+ let(:yaml_config) { "
9
+ ---
10
+ host: localhost
11
+ port: 5672
12
+ username: guest
13
+ password: guest
14
+ queue: connector-test
15
+ enabled: true
16
+ "}
17
+
18
+ describe ".configure" do
19
+
20
+ let(:connector) { double(Cpi::Connector) }
21
+
22
+ it "accepts a hash of connection information" do
23
+ expected_config = YAML::load(yaml_config)
24
+ expected_config.delete("enabled")
25
+ Cpi::Connector.should_receive(:new).with(expected_config).and_return(connector)
26
+ connector.should_receive(:start)
27
+
28
+ Cpi::Event.configure(YAML::load(yaml_config))
29
+ end
30
+
31
+ it "does nothing if enabled is false" do
32
+ yaml_config = "---
33
+ enabled: false"
34
+ Cpi::Connector.should_not_receive(:new)
35
+ connector.should_not_receive(:start)
36
+
37
+ Cpi::Event.configure(YAML::load(yaml_config))
38
+ end
39
+ end
40
+
41
+ describe ".transmit" do
42
+
43
+ let(:root_path) { "http://example.com/shipment/1" }
44
+
45
+ it "generates and sends an event" do
46
+ Cpi::Event.configure(YAML::load(yaml_config))
47
+
48
+ Cpi::Event.transmit("shipment_shipped", "stuff", root_path)
49
+
50
+ b = Bunny.new
51
+ b.start
52
+ q = b.queue("connector-test", :durable => true)
53
+ message = q.pop
54
+ h = ActiveSupport::JSON.decode(message[:payload])
55
+ b.stop
56
+
57
+ h.should unify({"outbound_event" => {"event_type" => "shipment_shipped", "tenant_uid" => "stuff", "root_path" => root_path, "uid" => :_x}})
58
+ end
59
+
60
+ it "does not transmit when it is not enabled" do
61
+ config = YAML::load(yaml_config)
62
+ config["enabled"] = false
63
+
64
+ Cpi::Event.configure(config)
65
+ Cpi::Generator.should_not_receive(:create_event)
66
+ # And there shouldn't be an exception
67
+
68
+ Cpi::Event.transmit("message", "tenant_uid", "root_path")
69
+ end
70
+
71
+ it "does nothing when called before being configured" do
72
+ Cpi::Generator.should_not_receive(:create_event)
73
+ Cpi::Event.transmit("message", "tenant_uid", "root_path")
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'lib/cpi_event_connector/generator'
3
+ require 'active_support'
4
+
5
+ describe Cpi::Generator do
6
+
7
+ it "generates json for an event" do
8
+ event_type = "shipment shipped"
9
+ tenant_uid = "123123"
10
+ root_path = "http://example.com"
11
+
12
+ expected = {"outbound_event" => {"event_type" => event_type, "tenant_uid" => tenant_uid, "root_path" => root_path, "uid" => "123123123"}}
13
+
14
+ event = Cpi::Generator.create_event(event_type, tenant_uid, root_path)
15
+
16
+ result = ActiveSupport::JSON.decode(event)
17
+
18
+ result["outbound_event"]["event_type"].should == event_type
19
+ result["outbound_event"]["tenant_uid"].should == tenant_uid
20
+ result["outbound_event"]["root_path"].should == root_path
21
+ result["outbound_event"]["uid"].should_not be_nil
22
+ end
23
+
24
+ it "generates a different uid for each event" do
25
+ event_type = "shipment shipped"
26
+ tenant_uid = "123123"
27
+ root_path = "http://example.com"
28
+
29
+ event1 = ActiveSupport::JSON.decode(Cpi::Generator.create_event(event_type, tenant_uid, root_path))
30
+ event2 = ActiveSupport::JSON.decode(Cpi::Generator.create_event(event_type, tenant_uid, root_path))
31
+
32
+ event1["outbound_event"]["uid"].should_not == event2["outbound_event"]["uid"]
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'lib/cpi_event_connector'
3
+ require 'yaml'
4
+ require 'bunny'
5
+
6
+ describe "Integration Tests" do
7
+
8
+ let(:yaml_config) { "
9
+ ---
10
+ host: localhost
11
+ port: 5672
12
+ username: guest
13
+ password: guest
14
+ queue: connector-test"}
15
+
16
+ it "sends a message to an amqp server" do
17
+ connector = Cpi::Connector.new(YAML::load(yaml_config))
18
+ connector.start
19
+ connector.transmit("message")
20
+ connector.stop
21
+
22
+ b = Bunny.new
23
+ b.start
24
+ q = b.queue("connector-test", :durable => true)
25
+ message = q.pop
26
+ message[:payload].should == "message"
27
+ b.stop
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ end
data/spec_helper.rb ADDED
@@ -0,0 +1,7 @@
1
+ Dir[File.dirname(__FILE__) + "/spec/support/**/*.rb"].each {|f| require f}
2
+
3
+ require 'rspec'
4
+
5
+ RSpec.configure do |config|
6
+ config.color_enabled = true
7
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cpi-event-connector
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Nulogy
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-03-02 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: justinf-unification_assertion
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: bunny
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ type: :runtime
61
+ version_requirements: *id003
62
+ - !ruby/object:Gem::Dependency
63
+ name: activesupport
64
+ prerelease: false
65
+ requirement: &id004 !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ~>
69
+ - !ruby/object:Gem::Version
70
+ hash: 1
71
+ segments:
72
+ - 3
73
+ - 1
74
+ - 1
75
+ version: 3.1.1
76
+ type: :runtime
77
+ version_requirements: *id004
78
+ - !ruby/object:Gem::Dependency
79
+ name: uuidtools
80
+ prerelease: false
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ~>
85
+ - !ruby/object:Gem::Version
86
+ hash: 15
87
+ segments:
88
+ - 2
89
+ - 1
90
+ - 2
91
+ version: 2.1.2
92
+ type: :runtime
93
+ version_requirements: *id005
94
+ description: see summary
95
+ email:
96
+ - engineering@nulogy.com
97
+ executables: []
98
+
99
+ extensions: []
100
+
101
+ extra_rdoc_files: []
102
+
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - Rakefile
108
+ - cpi-event-connector.gemspec
109
+ - lib/cpi_event_connector.rb
110
+ - lib/cpi_event_connector/connector.rb
111
+ - lib/cpi_event_connector/event.rb
112
+ - lib/cpi_event_connector/generator.rb
113
+ - lib/cpi_event_connector/version.rb
114
+ - spec/connector_spec.rb
115
+ - spec/event_spec.rb
116
+ - spec/generator_spec.rb
117
+ - spec/integration_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec_helper.rb
120
+ homepage: ""
121
+ licenses: []
122
+
123
+ post_install_message:
124
+ rdoc_options: []
125
+
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ hash: 3
134
+ segments:
135
+ - 0
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ hash: 3
143
+ segments:
144
+ - 0
145
+ version: "0"
146
+ requirements: []
147
+
148
+ rubyforge_project:
149
+ rubygems_version: 1.8.10
150
+ signing_key:
151
+ specification_version: 3
152
+ summary: Sends events to CPI
153
+ test_files: []
154
+