magellan-publisher 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 7ffadcdab35bc05035387f74ffdc1069a209c56d
4
- data.tar.gz: 6cc67dca0a373f051e7cdccc1272009adacf8f78
3
+ metadata.gz: d38adff420cc28de137ce67afb59bd8109e0a3d9
4
+ data.tar.gz: edf2f4bd089b5c5ca8507ffaa022130a37685782
5
5
  SHA512:
6
- metadata.gz: 514fd121d2df5a1cc5607285da9224f16fb6c4cf561607beb52bf1da437548326e8b52e79b259b03590313fb9753fd027f69e2a4330d449357adc5917213baf0
7
- data.tar.gz: 1c6c9d8bd218176cb470d442cc0c1428616807ea2258a99191d31c4b96329a77254fc5202e510571cb21112c8917c0b0248e46a12a6e99f6d69b5b8aebae9cd0
6
+ metadata.gz: eecfefcf1d081102103ff4d317ae41f71c6b7c7bb2d969494de5b7ef78b7a17481d5da196e5786f11de4bf9b9333076d19cd744e92839bda198cfeb32b02ce2f
7
+ data.tar.gz: c12bceffff43b22e316c5398e70548ef0a0dedd8bfe1e8e67cd3cde468fc37fe571be19e637b6c14bbf8addc4f65a42862d67257f183239c4b3f23faf5d3d9db
@@ -2,15 +2,15 @@
2
2
 
3
3
  module Magellan
4
4
  class Publisher
5
- def initialize(host: "127.0.0.1", port: 5672, vhost: nil, user: nil, pass: nil, timeout: 0.3)
5
+ def initialize(host: nil, port: nil, vhost: nil, user: nil, pass: nil, timeout: nil)
6
6
  # RabbitMQへの接続設定を作成
7
7
  connection_settings = {
8
- host: host,
9
- port: port,
10
- vhost: vhost,
11
- user: user,
12
- pass: pass,
13
- timeout: timeout,
8
+ host: load_env_as_default(host, ['MAGELLAN_PUBLISH_AMQP_ADDR'], :string),
9
+ port: load_env_as_default(port, ['MAGELLAN_PUBLISH_AMQP_PORT'], :integer),
10
+ vhost: load_env_as_default(vhost, ['MAGELLAN_PUBLISH_AMQP_VHOST'], :string),
11
+ user: load_env_as_default(user, ['MAGELLAN_PUBLISH_AMQP_USER'], :string),
12
+ pass: load_env_as_default(pass, ['MAGELLAN_PUBLISH_AMQP_PASS'], :string),
13
+ timeout: load_env_as_default(timeout, ['MAGELLAN_PUBLISH_AMQP_TIMEOUT'], :float) || 0.3,
14
14
  }
15
15
 
16
16
  @conn = Bunny.new(connection_settings)
@@ -33,5 +33,42 @@ module Magellan
33
33
  def publish(topic, body)
34
34
  @exchange.publish(body, routing_key: convert_topic(topic))
35
35
  end
36
+
37
+ class << self
38
+ def publisher
39
+ @publisher ||= Magellan::Publisher.new
40
+ end
41
+
42
+ def publish(topic, body)
43
+ publisher.publish(topic, body)
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def load_env_as_default(val, envkeys, type)
50
+ if is_blank?(val)
51
+ envkeys = Array(envkeys)
52
+ envkeys.each do |key|
53
+ val = ENV[key.to_s]
54
+ break if val and not(val.empty?)
55
+ end
56
+ end
57
+
58
+ if val
59
+ case type.to_sym
60
+ when :string then val.to_s
61
+ when :integer then Integer(val)
62
+ when :float then Float(val)
63
+ else val
64
+ end
65
+ else
66
+ nil
67
+ end
68
+ end
69
+
70
+ def is_blank?(val)
71
+ val.respond_to?(:empty?) ? !!val.empty? : !val
72
+ end
36
73
  end
37
74
  end
@@ -1,5 +1,5 @@
1
1
  module Magellan
2
2
  class Publisher
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
@@ -24,5 +24,4 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.6"
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
27
- spec.add_development_dependency "bunny_mock"
28
27
  end
@@ -9,31 +9,56 @@ describe Magellan::Publisher do
9
9
 
10
10
  describe "#publish" do
11
11
 
12
- before do
13
- @topic = "topic/aaa"
14
- @rabbitmq_topic = "topic.aaa"
15
- @payload = "Hello World"
16
- @bunny = BunnyMock.new
17
- channel = @bunny.create_channel
18
- @exchange = @bunny.exchange('test_exchange', type: :direct, durable: false, auto_delete: false)
19
- expect(@exchange).to receive(:publish).with(@payload, routing_key: @rabbitmq_topic)
20
-
21
- allow(Bunny ).to receive(:new).and_return(@bunny)
22
- allow(@bunny ).to receive(:create_channel).and_return(channel)
23
- allow(channel).to receive(:exchange).with('test_exchange', no_declare: true).and_return(@exchange)
24
-
25
- ENV["MAGELLAN_PUBLISH_EXCHANGE"] = "test_exchange"
26
- @publisher = Magellan::Publisher.new(host: "127.0.0.1",
27
- port: 5672,
28
- vhost: "customer1.sample_project",
29
- user: "customer1.sample_project",
30
- pass: "pasw1")
31
- end
32
-
33
- it do
34
- @publisher.publish(@topic, @payload)
35
- end
12
+ before do
13
+ @topic = "topic/aaa"
14
+ @rabbitmq_topic = "topic.aaa"
15
+ @payload = "Hello World"
16
+ @bunny = BunnyMock.new
17
+ channel = @bunny.create_channel
18
+ @exchange = @bunny.exchange('test_exchange', type: :direct, durable: false, auto_delete: false)
19
+ expect(@exchange).to receive(:publish).with(@payload, routing_key: @rabbitmq_topic)
36
20
 
21
+ allow(Bunny ).to receive(:new).and_return(@bunny)
22
+ allow(@bunny ).to receive(:create_channel).and_return(channel)
23
+ allow(channel).to receive(:exchange).with('test_exchange', no_declare: true).and_return(@exchange)
37
24
 
25
+ ENV["MAGELLAN_PUBLISH_EXCHANGE"] = "test_exchange"
26
+ @publisher = Magellan::Publisher.new(host: "127.0.0.1",
27
+ port: 5672,
28
+ vhost: "/customer1.sample_project",
29
+ user: "customer1.sample_project",
30
+ pass: "pasw1")
31
+ end
32
+
33
+ it do
34
+ @publisher.publish(@topic, @payload)
35
+ end
36
+ end
37
+
38
+ describe ".publish" do
39
+ before do
40
+ @topic = "topic/aaa"
41
+ @rabbitmq_topic = "topic.aaa"
42
+ @payload = "Hello World"
43
+ @bunny = BunnyMock.new
44
+ channel = @bunny.create_channel
45
+ @exchange = @bunny.exchange('test_exchange', type: :direct, durable: false, auto_delete: false)
46
+ expect(@exchange).to receive(:publish).with(@payload, routing_key: @rabbitmq_topic)
47
+
48
+ allow(Bunny ).to receive(:new).and_return(@bunny)
49
+ allow(@bunny ).to receive(:create_channel).and_return(channel)
50
+ allow(channel).to receive(:exchange).with('test_exchange', no_declare: true).and_return(@exchange)
51
+
52
+ ENV["MAGELLAN_PUBLISH_EXCHANGE"] = "test_exchange"
53
+ ENV["MAGELLAN_PUBLISH_AMQP_ADDR"] = "127.0.0.1"
54
+ ENV["MAGELLAN_PUBLISH_AMQP_PORT"] = "5672"
55
+ ENV["MAGELLAN_PUBLISH_AMQP_VHOST"] = "/customer1.sample_project"
56
+ ENV["MAGELLAN_PUBLISH_AMQP_USER"] = "customer1.sample_project"
57
+ ENV["MAGELLAN_PUBLISH_AMQP_PASS"] = "pasw1"
58
+ end
59
+
60
+ it do
61
+ Magellan::Publisher.publish(@topic, @payload)
62
+ end
38
63
  end
39
64
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magellan-publisher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomoyuki Chikanaga
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-09 00:00:00.000000000 Z
11
+ date: 2014-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bunny
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: bunny_mock
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ">="
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ">="
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
83
  description: ''
98
84
  email:
99
85
  - t-chikanaga@groovenauts.jp