neighborparrot 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/neighborparrot.rb +9 -3
- data/lib/neighborparrot/esparrot.rb +18 -18
- data/lib/neighborparrot/{post.rb → send.rb} +6 -7
- data/neighborparrot.gemspec +3 -3
- data/spec/esparrot_spec.rb +8 -7
- data/spec/neighborparrot_spec.rb +7 -7
- data/spec/spec_helper.rb +3 -0
- data/spec/support/helpers.rb +3 -3
- metadata +12 -12
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/lib/neighborparrot.rb
CHANGED
@@ -22,11 +22,17 @@ module Neighborparrot
|
|
22
22
|
DEFAULT_SERVER = 'https://neighborparrot.net'
|
23
23
|
@@config = { :server => DEFAULT_SERVER }
|
24
24
|
|
25
|
-
|
25
|
+
# Chen mandatory parameters
|
26
|
+
# @param [Hash] parameters
|
27
|
+
# @param [Symbol] action [:send/:open]
|
28
|
+
def self.check_params(p, action=:send)
|
29
|
+
raise "Channel can't be nil" if p[:channel].nil? || p[:channel].length == 0
|
26
30
|
raise "ERROR# Neighborparrot: api_id can't be nil" if p[:api_id].nil? || p[:api_id].length == 0
|
27
|
-
|
31
|
+
if action == :send
|
32
|
+
raise "ERROR# Neighborparrot: api_key can't be nil" if p[:api_key].nil? || p[:api_key].length == 0
|
33
|
+
end
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
31
|
-
require 'neighborparrot/
|
37
|
+
require 'neighborparrot/send'
|
32
38
|
require 'neighborparrot/esparrot'
|
@@ -1,7 +1,6 @@
|
|
1
|
-
require 'pp'
|
2
1
|
class ESParrot
|
3
2
|
include Neighborparrot
|
4
|
-
|
3
|
+
|
5
4
|
# Open a persistent connection to the Neighbor in a new
|
6
5
|
# thread and return true if all works unless :foreground
|
7
6
|
# options is true.
|
@@ -15,25 +14,26 @@ class ESParrot
|
|
15
14
|
# * :api_id => Your api ID in neighborparrot.com
|
16
15
|
# * :api_key => Your api key
|
17
16
|
# * :server => Server to connect (Only for development)
|
18
|
-
def open(
|
17
|
+
def open(params={})
|
19
18
|
params = Neighborparrot.configuration.merge(params)
|
19
|
+
Neighborparrot.check_params params, :open
|
20
20
|
return false if connected?
|
21
21
|
if ! params[:foreground] == true
|
22
22
|
close if @current_thread # If previus thread but closed connection, kill it
|
23
|
-
@current_thread = Thread.new(
|
24
|
-
open_connection
|
23
|
+
@current_thread = Thread.new(params) do | params|
|
24
|
+
open_connection params
|
25
25
|
end
|
26
26
|
return true
|
27
27
|
else
|
28
|
-
open_connection
|
28
|
+
open_connection params
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
# @return true if a connection exists and is started
|
33
33
|
def connected?
|
34
34
|
@connection && @connection.started?
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
# close the active connection
|
38
38
|
def close
|
39
39
|
return unless connected?
|
@@ -41,39 +41,39 @@ class ESParrot
|
|
41
41
|
@current_thread.kill
|
42
42
|
@current_thread = nil
|
43
43
|
end
|
44
|
-
|
44
|
+
|
45
45
|
# Define a block called on message received
|
46
46
|
# The received message is passed to the block as a var
|
47
47
|
def on_message(&block)
|
48
48
|
@on_message_blk = block
|
49
49
|
end
|
50
|
-
|
50
|
+
|
51
51
|
# Define a block called on error
|
52
52
|
# An optional param with the error should be pass if present
|
53
53
|
def on_error(&block)
|
54
54
|
@on_error_blk = block
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
# Define a block called on connection closed
|
58
58
|
def on_close(&block)
|
59
59
|
@on_close_blk = block
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
# Define a block called on connect
|
63
63
|
def on_connect(&block)
|
64
64
|
@on_connect_blk = block
|
65
65
|
end
|
66
66
|
|
67
67
|
private
|
68
|
-
|
68
|
+
|
69
69
|
# Open a persistent connection to the neighbor
|
70
70
|
# TODO: Refactor, EM??
|
71
|
-
def open_connection(
|
71
|
+
def open_connection(params)
|
72
72
|
begin
|
73
73
|
uri = URI(params[:server])
|
74
74
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
75
75
|
http.read_timeout = 9999999999999999 # TODO Fix this
|
76
|
-
request = Net::HTTP::Get.new URI.escape("/open?channel=#{channel}")
|
76
|
+
request = Net::HTTP::Get.new URI.escape("/open?channel=#{params[:channel]}")
|
77
77
|
@connection = http
|
78
78
|
@on_connect_blk.call if @on_connect_blk
|
79
79
|
http.request request do |response|
|
@@ -84,9 +84,9 @@ class ESParrot
|
|
84
84
|
end
|
85
85
|
end
|
86
86
|
end
|
87
|
-
rescue
|
88
|
-
@on_error_blk.call($!) if @on_error_blk
|
89
|
-
end
|
87
|
+
rescue
|
88
|
+
@on_error_blk.call($!) if @on_error_blk
|
89
|
+
end
|
90
90
|
@on_close_blk.call if @on_close_blk
|
91
91
|
end
|
92
92
|
end
|
@@ -3,23 +3,22 @@ module Neighborparrot
|
|
3
3
|
# Post a message to a channel
|
4
4
|
# Raise exception if channel is not setted
|
5
5
|
# If empty data, refuse to send nothing
|
6
|
-
# @param [String] channel: The channel name
|
7
|
-
# @param [String] string to send
|
8
6
|
# @param [Hash] params
|
9
7
|
# * :api_id => Your api ID in neighborparrot.com
|
10
8
|
# * :api_key => Your api key
|
11
9
|
# * :server => Server to connect (Only for development)
|
10
|
+
# * :channel => The channel name
|
11
|
+
# * :data => Your payload
|
12
12
|
# @return [Boolean] true if sended
|
13
|
-
def self.
|
14
|
-
raise "Channel can't be nil" if channel.nil? || channel.length == 0
|
15
|
-
return false if data.nil? || data.length == 0
|
13
|
+
def self.send(params={})
|
16
14
|
params = self.configuration.merge params
|
17
15
|
self.check_params params
|
16
|
+
return false if params[:data].nil? || params[:data].length == 0
|
18
17
|
|
19
18
|
uri = URI(params[:server])
|
20
19
|
Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
|
21
|
-
request = Net::HTTP::Post.new('/
|
22
|
-
request.set_form_data(params
|
20
|
+
request = Net::HTTP::Post.new('/send')
|
21
|
+
request.set_form_data(params)
|
23
22
|
response = http.request(request)
|
24
23
|
return true if response.body == "Ok"
|
25
24
|
end
|
data/neighborparrot.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "neighborparrot"
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Eloy Gomez"]
|
12
|
-
s.date = "2012-01-
|
12
|
+
s.date = "2012-01-07"
|
13
13
|
s.description = "Send messages to the neighborparrot event source service"
|
14
14
|
s.email = "eloy@indeos.es"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
|
|
27
27
|
"VERSION",
|
28
28
|
"lib/neighborparrot.rb",
|
29
29
|
"lib/neighborparrot/esparrot.rb",
|
30
|
-
"lib/neighborparrot/
|
30
|
+
"lib/neighborparrot/send.rb",
|
31
31
|
"neighborparrot.gemspec",
|
32
32
|
"spec/esparrot_spec.rb",
|
33
33
|
"spec/neighborparrot_spec.rb",
|
data/spec/esparrot_spec.rb
CHANGED
@@ -5,6 +5,7 @@ describe "Neighborparrot::ESParrot" do
|
|
5
5
|
api_id = 'test-id'
|
6
6
|
api_key = 'api_key'
|
7
7
|
Neighborparrot.configure({ :api_id => api_id, :api_key => :api_key })
|
8
|
+
@channel = 'test'
|
8
9
|
end
|
9
10
|
|
10
11
|
before :each do
|
@@ -21,7 +22,7 @@ describe "Neighborparrot::ESParrot" do
|
|
21
22
|
@parrot.on_connect do
|
22
23
|
connected = true
|
23
24
|
end
|
24
|
-
@parrot.open(
|
25
|
+
@parrot.open(:channel => @channel)
|
25
26
|
sleep(2)
|
26
27
|
connected.should be_true
|
27
28
|
end
|
@@ -31,24 +32,24 @@ describe "Neighborparrot::ESParrot" do
|
|
31
32
|
@parrot.on_message do |msg|
|
32
33
|
received = msg
|
33
34
|
end
|
34
|
-
@parrot.open(
|
35
|
+
@parrot.open(:channel => @channel)
|
35
36
|
sleep(2)
|
36
37
|
text = Faker::Lorem.paragraph(30)
|
37
|
-
|
38
|
+
Neighborparrot.send(:channel => 'test', :data => text)
|
38
39
|
sleep(1)
|
39
40
|
received.should eq text
|
40
41
|
end
|
41
42
|
|
42
43
|
it "should return false if already a connection active" do
|
43
|
-
@parrot.open(
|
44
|
+
@parrot.open(:channel => @channel)
|
44
45
|
sleep(2)
|
45
|
-
@parrot.open('other test').should be_false
|
46
|
+
@parrot.open(:channel => 'other test').should be_false
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
50
|
describe "Neighborparrot::ESParrot#close" do
|
50
51
|
it "should close a connection" do
|
51
|
-
@parrot.open(
|
52
|
+
@parrot.open(:channel => @channel)
|
52
53
|
sleep(2)
|
53
54
|
@parrot.close()
|
54
55
|
@parrot.connected?.should be_false
|
@@ -62,7 +63,7 @@ describe "Neighborparrot::ESParrot" do
|
|
62
63
|
end
|
63
64
|
|
64
65
|
it "should be true when connected" do
|
65
|
-
@parrot.open(
|
66
|
+
@parrot.open(:channel => @channel)
|
66
67
|
sleep(2)
|
67
68
|
@parrot.connected?.should be_true
|
68
69
|
@parrot.close
|
data/spec/neighborparrot_spec.rb
CHANGED
@@ -9,33 +9,33 @@ describe "Neighborparrot" do
|
|
9
9
|
|
10
10
|
describe "Neigborparrot#post" do
|
11
11
|
it 'should return true if no errors' do
|
12
|
-
|
12
|
+
send_test.should be_true
|
13
13
|
end
|
14
14
|
|
15
15
|
it "should rails exception without id" do
|
16
16
|
Neighborparrot.configure({ :api_key => nil })
|
17
|
-
expect {
|
17
|
+
expect { send_test }.to raise_error
|
18
18
|
end
|
19
19
|
|
20
20
|
it "should rails exception without key" do
|
21
21
|
Neighborparrot.configure({:api_id => nil})
|
22
|
-
expect {
|
22
|
+
expect { send_test }.to raise_error
|
23
23
|
end
|
24
24
|
|
25
25
|
it "should raise exception with nill channel" do
|
26
|
-
expect { Neighborparrot.
|
26
|
+
expect { Neighborparrot.send(:channel => nil, :data => 'test string') }.to raise_error
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should raise exception with empty channel" do
|
30
|
-
expect { Neighborparrot.
|
30
|
+
expect { Neighborparrot.send(:channel => '', :data => 'test string') }.to raise_error
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should not send message with nil data" do
|
34
|
-
Neighborparrot.
|
34
|
+
Neighborparrot.send(:channel => 'test-channel', :data => nil).should be_false
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should not send message with empty data" do
|
38
|
-
Neighborparrot.
|
38
|
+
Neighborparrot.send(:channel => 'test-channel', :data => '').should be_false
|
39
39
|
end
|
40
40
|
end
|
41
41
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -9,6 +9,9 @@ require File.expand_path(File.dirname(__FILE__) + '/../lib/neighborparrot')
|
|
9
9
|
# in ./support/ and its subdirectories.
|
10
10
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
11
11
|
|
12
|
+
# Setup server on localhost
|
13
|
+
Neighborparrot.configure :server => 'http://127.0.0.1:9000'
|
14
|
+
|
12
15
|
RSpec.configure do |config|
|
13
16
|
|
14
17
|
end
|
data/spec/support/helpers.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
2
|
-
def
|
3
|
-
Neighborparrot.
|
1
|
+
# Send 'test' string to 'test_channel'
|
2
|
+
def send_test
|
3
|
+
Neighborparrot.send(:channel => 'test_channel', :data => 'test')
|
4
4
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: neighborparrot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-07 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &13007020 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 2.3.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *13007020
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &13006480 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: 1.0.0
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *13006480
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: jeweler
|
38
|
-
requirement: &
|
38
|
+
requirement: &13005740 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 1.6.4
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *13005740
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rcov
|
49
|
-
requirement: &
|
49
|
+
requirement: &13004940 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,7 +54,7 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *13004940
|
58
58
|
description: Send messages to the neighborparrot event source service
|
59
59
|
email: eloy@indeos.es
|
60
60
|
executables: []
|
@@ -73,7 +73,7 @@ files:
|
|
73
73
|
- VERSION
|
74
74
|
- lib/neighborparrot.rb
|
75
75
|
- lib/neighborparrot/esparrot.rb
|
76
|
-
- lib/neighborparrot/
|
76
|
+
- lib/neighborparrot/send.rb
|
77
77
|
- neighborparrot.gemspec
|
78
78
|
- spec/esparrot_spec.rb
|
79
79
|
- spec/neighborparrot_spec.rb
|
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
94
|
version: '0'
|
95
95
|
segments:
|
96
96
|
- 0
|
97
|
-
hash: -
|
97
|
+
hash: -1488788034171426421
|
98
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
99
|
none: false
|
100
100
|
requirements:
|