miu 0.2.1 → 0.2.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: 1443afae69b50846c4ac4aa4e14c88a54e99cfa8
4
- data.tar.gz: a9b76bb0de214c17db275392a1595bbed00e4d40
3
+ metadata.gz: 58c2dddc2ca86a827f04b870dcee1bea659bae1d
4
+ data.tar.gz: a9a44a16e4c1bd40ec58ad638777152a2914d4c3
5
5
  SHA512:
6
- metadata.gz: 9ef756f4b8fd9d3b1763559cfe27d4e001a0243b3ae072b7b150f202baca1e79c1b73bcfc038cb783dc68620567a00005272d9007f80eaf274137d466941959b
7
- data.tar.gz: f7adbd99ffe06db55ed71c07654623ddec8ee49098c0d674ad67645a0d3a0c2ecfa9c581f487be3e2c5cd077e6aca58ba4001556f41dce60d74142fcdd49e86b
6
+ metadata.gz: df03bea1e55a265700af27b46a1917de3bf2ac62640a4434269d8c9ac1d34ac89a3b39241ee8f10aea5431c17638fc2a5920aa2d3007c4df065b9afdddd339a3
7
+ data.tar.gz: bd9d1f074c1acf7a79575ef4cd4555bd727ff30c2b031568ace6d77b5563fdcd79df8a30450a05d63551f7840461c355604970c6ebb6f62b1362af0bc46f0531
data/lib/miu/command.rb CHANGED
@@ -22,24 +22,17 @@ module Miu
22
22
  super task, namespace, subcommand || options.fetch(:subcommand, true)
23
23
  end
24
24
 
25
- def add_miu_pub_options(name)
26
- tag = ['miu', 'input', name].select { |s| s && !s.empty? }.join('.') + '.'
25
+ def add_miu_pub_options(tag)
27
26
  option 'pub-host', :type => :string, :default => '127.0.0.1', :desc => 'miu pub host'
28
27
  option 'pub-port', :type => :numeric, :default => Miu.default_sub_port, :desc => 'miu pub port'
29
28
  option 'pub-tag', :type => :string, :default => tag, :desc => 'miu pub tag'
30
29
  end
31
30
 
32
- def add_miu_sub_options(name)
33
- tag = ['miu', 'output', name].select { |s| s && !s.empty? }.join('.') + '.'
31
+ def add_miu_sub_options(tag)
34
32
  option 'sub-host', :type => :string, :default => '127.0.0.1', :desc => 'miu sub host'
35
33
  option 'sub-port', :type => :numeric, :default => Miu.default_pub_port, :desc => 'miu sub port'
36
34
  option 'sub-tag', :type => :string, :default => tag, :desc => 'miu sub tag'
37
35
  end
38
-
39
- def add_miu_pub_sub_options(name)
40
- add_miu_pub_options name
41
- add_miu_sub_options name
42
- end
43
36
  end
44
37
 
45
38
  unless respond_to? :no_commands
data/lib/miu/publisher.rb CHANGED
@@ -13,13 +13,41 @@ module Miu
13
13
 
14
14
  klass = Class.new(socket, &block)
15
15
  klass.send :include, Writable
16
- klass.send :include, self
17
16
 
18
17
  klass.new.tap do |pub|
19
18
  address = Miu::Socket.build_address host, port
20
19
  pub.connect address
21
20
  end
22
21
  end
22
+
23
+ def included(base)
24
+ base.extend ClassMethods
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def socket_type(socket = nil)
30
+ if socket
31
+ @socket_type = socket
32
+ else
33
+ @socket_type
34
+ end
35
+ end
36
+ end
37
+
38
+ attr_reader :publisher
39
+
40
+ def initialize(host, port, tag)
41
+ @publisher = Miu::Publisher.new host, port, :socket => self.class.socket_type
42
+ @tag = tag
43
+ end
44
+
45
+ def close
46
+ @publisher.close
47
+ end
48
+
49
+ def write(message)
50
+ @publisher.write @tag, message
23
51
  end
24
52
  end
25
53
  end
@@ -13,13 +13,68 @@ module Miu
13
13
 
14
14
  klass = Class.new(socket, &block)
15
15
  klass.send :include, Readable
16
- klass.send :include, self
17
16
 
18
17
  klass.new.tap do |sub|
19
18
  address = Miu::Socket.build_address host, port
20
19
  sub.connect address
21
20
  end
22
21
  end
22
+
23
+ def included(base)
24
+ base.extend ClassMethods
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def socket_type(socket = nil)
30
+ if socket
31
+ @socket_type = socket
32
+ else
33
+ @socket_type
34
+ end
35
+ end
36
+ end
37
+
38
+ attr_reader :subscriber
39
+
40
+ def initialize(host, port, tag)
41
+ @subscriber = Miu::Subscriber.new host, port, :socket => self.class.socket_type
42
+ self.tag = tag
43
+ end
44
+
45
+ def close
46
+ @subscriber.close
47
+ end
48
+
49
+ def tag=(value)
50
+ @subscriber.unsubscribe @tag if @tag
51
+ @subscriber.subscribe value
52
+ @tag = value
53
+ end
54
+
55
+ def tag
56
+ @tag
57
+ end
58
+
59
+ def run
60
+ @subscriber.each do |packet|
61
+ begin
62
+ on_packet packet
63
+ rescue Exception => e
64
+ Miu::Logger.exception e
65
+ end
66
+ end
67
+ end
68
+
69
+ protected
70
+
71
+ def on_packet(packet)
72
+ name = method_name packet
73
+ __send__ name, packet.tag, packet.data if respond_to?(name)
74
+ end
75
+
76
+ def method_name(packet)
77
+ "on_#{packet.data.type}"
23
78
  end
24
79
  end
25
80
  end
data/lib/miu/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Miu
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -20,7 +20,6 @@ describe Miu::Command do
20
20
  its(:namespace) { should eq 'test_node' }
21
21
  it { should respond_to :add_miu_pub_options }
22
22
  it { should respond_to :add_miu_sub_options }
23
- it { should respond_to :add_miu_pub_sub_options }
24
23
  it { should respond_to :hello }
25
24
  end
26
25
  end
@@ -24,4 +24,20 @@ describe Miu::Publisher do
24
24
  let(:base) { MyPubSocket }
25
25
  end
26
26
  end
27
+
28
+ context 'include' do
29
+ before do
30
+ publisher = stub(Miu::Publisher)
31
+ Miu::Publisher.stub!(:new).and_return(publisher)
32
+
33
+ @klass = Class.new do
34
+ include Miu::Publisher
35
+ socket_type Miu::PubSocket
36
+ end
37
+ end
38
+
39
+ describe '#socket_type' do
40
+ it { expect(@klass.socket_type).to eq Miu::PubSocket }
41
+ end
42
+ end
27
43
  end
@@ -24,4 +24,29 @@ describe Miu::Subscriber do
24
24
  let(:base) { MySubSocket }
25
25
  end
26
26
  end
27
+
28
+ context 'include' do
29
+ before do
30
+ subscriber = stub(Miu::Subscriber)
31
+ subscriber.stub(:subscribe)
32
+ subscriber.stub(:unsubscribe)
33
+ Miu::Subscriber.stub!(:new).and_return(subscriber)
34
+
35
+ @klass = Class.new do
36
+ include Miu::Subscriber
37
+ socket_type Miu::SubSocket
38
+ end
39
+ end
40
+
41
+ describe '#socket_type' do
42
+ it { expect(@klass.socket_type).to eq Miu::SubSocket }
43
+ end
44
+
45
+ describe '#method_name' do
46
+ let(:subscriber) { @klass.new 'dummy', 1234, 'tag' }
47
+ it { expect(subscriber.__send__ :method_name, Miu::Packet.new('tag', Miu::Messages::Text.new)).to eq 'on_text' }
48
+ it { expect(subscriber.__send__ :method_name, Miu::Packet.new('tag', Miu::Messages::Enter.new)).to eq 'on_enter' }
49
+ it { expect(subscriber.__send__ :method_name, Miu::Packet.new('tag', Miu::Messages::Leave.new)).to eq 'on_leave' }
50
+ end
51
+ end
27
52
  end
@@ -23,7 +23,6 @@ shared_examples 'publishable socket' do
23
23
  let(:pub) { Miu::Publisher.new(:socket => base) }
24
24
  it { expect(pub).to be_kind_of(base) }
25
25
  it { expect(pub).to be_kind_of(Miu::Writable) }
26
- it { expect(pub).to be_kind_of(Miu::Publisher) }
27
26
  it { expect(pub).to be_respond_to(:connect, :write) }
28
27
  end
29
28
 
@@ -31,6 +30,5 @@ shared_examples 'subscribable socket' do
31
30
  let(:sub) { Miu::Subscriber.new(:socket => base) }
32
31
  it { expect(sub).to be_kind_of(base) }
33
32
  it { expect(sub).to be_kind_of(Miu::Readable) }
34
- it { expect(sub).to be_kind_of(Miu::Subscriber) }
35
33
  it { expect(sub).to be_respond_to(:connect, :read) }
36
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - mashiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-10 00:00:00.000000000 Z
11
+ date: 2013-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor