neighborparrot 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +28 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/neighborparrot.rb +115 -0
- data/spec/neighborparrot_spec.rb +108 -0
- data/spec/spec_helper.rb +12 -0
- metadata +105 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.4"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.3)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.4)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rcov (0.9.11)
|
12
|
+
rspec (2.3.0)
|
13
|
+
rspec-core (~> 2.3.0)
|
14
|
+
rspec-expectations (~> 2.3.0)
|
15
|
+
rspec-mocks (~> 2.3.0)
|
16
|
+
rspec-core (2.3.1)
|
17
|
+
rspec-expectations (2.3.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.3.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 1.0.0)
|
26
|
+
jeweler (~> 1.6.4)
|
27
|
+
rcov
|
28
|
+
rspec (~> 2.3.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Eloy Gomez
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= neighborparrot
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Contributing to neighborparrot
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Eloy Gomez. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "neighborparrot"
|
18
|
+
gem.homepage = "http://neighborparrot.com"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Ruby client for the Neighborparrot}
|
21
|
+
gem.description = %Q{Send messages to the neighborparrot event source service}
|
22
|
+
gem.email = "eloy@indeos.es"
|
23
|
+
gem.authors = ["Eloy Gomez"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "neighborparrot #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.2.0
|
@@ -0,0 +1,115 @@
|
|
1
|
+
class Neighborparrot
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
NEIGHBOR_PROTOCOL = "http"
|
5
|
+
NEIGHBOR_HOST = "neighborparrot.net"
|
6
|
+
NEIGHBOR_PORT = 80
|
7
|
+
POST_URL = URI.parse("#{NEIGHBOR_PROTOCOL}://#{NEIGHBOR_HOST}:#{NEIGHBOR_PORT}/post")
|
8
|
+
|
9
|
+
# Create a new instance of the client
|
10
|
+
# @param [String] key: The key assigned to your account
|
11
|
+
# in neighborparrot.com site
|
12
|
+
def initialize(key)
|
13
|
+
raise "Invalid key" if key.nil? || key.length == 0
|
14
|
+
@key = key
|
15
|
+
end
|
16
|
+
|
17
|
+
# Post a message to a channel
|
18
|
+
# Raise exception if channel is not setted
|
19
|
+
# If empty data, refuse to send nothing
|
20
|
+
# Raise exception if error
|
21
|
+
# @param [String] channel: The channel name
|
22
|
+
# @param [String] string to send
|
23
|
+
def post(channel, data)
|
24
|
+
raise "Channel can't be nil" if channel.nil? || channel.length == 0
|
25
|
+
return false if data.nil? || data.length == 0
|
26
|
+
params = { :key => @key, :channel => channel, :data => data }
|
27
|
+
res = Net::HTTP.post_form(POST_URL, params)
|
28
|
+
raise "Error when post to the neighborparrot: #{res.value}" unless res.nil? || res.is_a?(Net::HTTPSuccess)
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
|
32
|
+
# Open a persistent connection to the Neighbor in a new
|
33
|
+
# thread and return true if all works unless :foreground
|
34
|
+
# options is true.
|
35
|
+
# Current options to the connectio are:
|
36
|
+
# :foreground [Boolean] run the connection in the foreground
|
37
|
+
# stoping the clode flow until the connection is closed by server or
|
38
|
+
# another thread call close
|
39
|
+
#
|
40
|
+
# @param [String] channel to connect
|
41
|
+
# @param [Hash] Options to the connection
|
42
|
+
def open(channel, options={})
|
43
|
+
return false if connected?
|
44
|
+
|
45
|
+
if ! options[:foreground] == true
|
46
|
+
close if @current_thread # If previus thread but closed connection kill it
|
47
|
+
@current_thread = Thread.new(channel, options) do | channel, options|
|
48
|
+
open_connection channel, options
|
49
|
+
end
|
50
|
+
return true
|
51
|
+
else
|
52
|
+
open_connection channel, options
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# @return true if a connection exists and is started
|
57
|
+
def connected?
|
58
|
+
@connection && @connection.started?
|
59
|
+
end
|
60
|
+
|
61
|
+
# close and active connection
|
62
|
+
def close
|
63
|
+
return unless connected?
|
64
|
+
@connection.finish()
|
65
|
+
@current_thread.kill
|
66
|
+
@current_thread = nil
|
67
|
+
end
|
68
|
+
|
69
|
+
# Define a block called on message received
|
70
|
+
# The received message is passed to the block as a var
|
71
|
+
def on_message(&block)
|
72
|
+
@on_message_blk = block
|
73
|
+
end
|
74
|
+
|
75
|
+
# Define a block called on error
|
76
|
+
# An optional param with the error should be pass if present
|
77
|
+
def on_error(&block)
|
78
|
+
@on_error_blk = block
|
79
|
+
end
|
80
|
+
|
81
|
+
# Define a block called on connection closed
|
82
|
+
def on_close(&block)
|
83
|
+
@on_close_blk = block
|
84
|
+
end
|
85
|
+
|
86
|
+
# Define a block called on connect
|
87
|
+
def on_connect(&block)
|
88
|
+
@on_connect_blk = block
|
89
|
+
end
|
90
|
+
|
91
|
+
# Open a persistent connection to the neighbor
|
92
|
+
#
|
93
|
+
def open_connection(channel, options={})
|
94
|
+
begin
|
95
|
+
Net::HTTP.start(NEIGHBOR_HOST, NEIGHBOR_PORT) do |http|
|
96
|
+
http.read_timeout = 9999999999999999 # TODO Fix this
|
97
|
+
request = Net::HTTP::Get.new "/open?channel=#{channel}"
|
98
|
+
@connection = http
|
99
|
+
@on_connect_blk.call if @on_connect_blk
|
100
|
+
http.request request do |response|
|
101
|
+
response.read_body do |chunk|
|
102
|
+
if chunk.start_with? "data:"
|
103
|
+
data = chunk[5..-3]
|
104
|
+
@on_message_blk.call(data) if @on_message_blk
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
rescue
|
110
|
+
@on_error_blk.call($!) if @on_error_blk
|
111
|
+
return
|
112
|
+
end
|
113
|
+
@on_close_blk.call if @on_close_blk
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Neighborparrot" do
|
4
|
+
before :each do
|
5
|
+
@key = 'test_key'
|
6
|
+
@parrot = Neighborparrot.new(@key)
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "Neigborparrot#post" do
|
10
|
+
it "should rails exception when instantiate without key" do
|
11
|
+
expect { Neighborparrot.new() }.to raise_error
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should rails exception when nil key" do
|
15
|
+
expect { Neighborparrot.new(nil) }.to raise_error
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should rails exception when empty key" do
|
19
|
+
expect { Neighborparrot.new('') }.to raise_error
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should send post request with valid parameters" do
|
23
|
+
expec_params = { :key => @key, :channel => 'test', :data => 'test string' }
|
24
|
+
url = URI.parse('http://neighborparrot.net/post')
|
25
|
+
Net::HTTP.should_receive(:post_form).with(url, expec_params)
|
26
|
+
@parrot.post(expec_params[:channel], expec_params[:data])
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise exception with nill channel" do
|
30
|
+
expect { @parrot.post(nil, 'test string') }.to raise_error
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should raise exception with empty channel" do
|
34
|
+
expect { @parrot.post('', 'test string') }.to raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not send message with nil data" do
|
38
|
+
Net::HTTP.should_not_receive(:post_form)
|
39
|
+
@parrot.post('test-channel', nil)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should not send message with empty data" do
|
43
|
+
Net::HTTP.should_not_receive(:post_form)
|
44
|
+
@parrot.post('test-channel', '')
|
45
|
+
end
|
46
|
+
|
47
|
+
# TODO
|
48
|
+
it "should raise exception if can't send the request"
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "Neigborparrot#open" do
|
53
|
+
after :each do
|
54
|
+
@parrot.close
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should open a connection with correct values" do
|
58
|
+
connected = false
|
59
|
+
@parrot.on_connect do
|
60
|
+
connected = true
|
61
|
+
end
|
62
|
+
@parrot.open('test')
|
63
|
+
sleep(2)
|
64
|
+
connected.should be_true
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should receive messages" do
|
68
|
+
received = nil
|
69
|
+
@parrot.on_message do |msg|
|
70
|
+
received = msg
|
71
|
+
end
|
72
|
+
@parrot.open('test')
|
73
|
+
sleep(2)
|
74
|
+
@parrot.post('test', 'message')
|
75
|
+
sleep(1)
|
76
|
+
received.should eq 'message'
|
77
|
+
end
|
78
|
+
|
79
|
+
it "should return false if already a connection active" do
|
80
|
+
@parrot.open('test')
|
81
|
+
sleep(2)
|
82
|
+
@parrot.open('other test').should be_false
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "Neigborparrot#close" do
|
87
|
+
it "should close a connection" do
|
88
|
+
@parrot.open('test')
|
89
|
+
sleep(2)
|
90
|
+
@parrot.close()
|
91
|
+
@parrot.connected?.should be_false
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "Neigborparrot#connected?" do
|
96
|
+
it "should be false before connected" do
|
97
|
+
@parrot.connected?.should be_false
|
98
|
+
@parrot.close
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should be true when connected" do
|
102
|
+
@parrot.open('test')
|
103
|
+
sleep(2)
|
104
|
+
@parrot.connected?.should be_true
|
105
|
+
@parrot.close
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'neighborparrot'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: neighborparrot
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Eloy Gomez
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-02 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &15247940 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.3.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *15247940
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &15247200 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.0.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *15247200
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: jeweler
|
38
|
+
requirement: &15246640 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.6.4
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *15246640
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rcov
|
49
|
+
requirement: &15245900 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *15245900
|
58
|
+
description: Send messages to the neighborparrot event source service
|
59
|
+
email: eloy@indeos.es
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files:
|
63
|
+
- LICENSE.txt
|
64
|
+
- README.rdoc
|
65
|
+
files:
|
66
|
+
- .document
|
67
|
+
- .rspec
|
68
|
+
- Gemfile
|
69
|
+
- Gemfile.lock
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.rdoc
|
72
|
+
- Rakefile
|
73
|
+
- VERSION
|
74
|
+
- lib/neighborparrot.rb
|
75
|
+
- spec/neighborparrot_spec.rb
|
76
|
+
- spec/spec_helper.rb
|
77
|
+
homepage: http://neighborparrot.com
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
hash: 3143593421196085716
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.10
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Ruby client for the Neighborparrot
|
105
|
+
test_files: []
|