neighborparrot 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -11,3 +11,8 @@ group :development do
11
11
  gem "jeweler", "~> 1.6.4"
12
12
  gem "rcov", ">= 0"
13
13
  end
14
+
15
+
16
+ group :test do
17
+ gem 'faker'
18
+ end
data/Gemfile.lock CHANGED
@@ -2,7 +2,10 @@ GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
4
  diff-lcs (1.1.3)
5
+ faker (1.0.1)
6
+ i18n (~> 0.4)
5
7
  git (1.2.5)
8
+ i18n (0.6.0)
6
9
  jeweler (1.6.4)
7
10
  bundler (~> 1.0)
8
11
  git (>= 1.2.5)
@@ -23,6 +26,7 @@ PLATFORMS
23
26
 
24
27
  DEPENDENCIES
25
28
  bundler (~> 1.0.0)
29
+ faker
26
30
  jeweler (~> 1.6.4)
27
31
  rcov
28
32
  rspec (~> 2.3.0)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -0,0 +1,92 @@
1
+ require 'pp'
2
+ class ESParrot
3
+ include Neighborparrot
4
+
5
+ # Open a persistent connection to the Neighbor in a new
6
+ # thread and return true if all works unless :foreground
7
+ # options is true.
8
+ # Current options to the connectio are:
9
+ #
10
+ # @param [String] channel to connect
11
+ # @param [Hash] Params for the connection. this params can be:
12
+ # * :foreground [Boolean] run the connection in the foreground
13
+ # stoping the clode flow until the connection is closed by server or
14
+ # another thread call close
15
+ # * :api_id => Your api ID in neighborparrot.com
16
+ # * :api_key => Your api key
17
+ # * :server => Server to connect (Only for development)
18
+ def open(channel, params={})
19
+ params = Neighborparrot.configuration.merge(params)
20
+ return false if connected?
21
+ if ! params[:foreground] == true
22
+ close if @current_thread # If previus thread but closed connection, kill it
23
+ @current_thread = Thread.new(channel, params) do | channel, params|
24
+ open_connection channel, params
25
+ end
26
+ return true
27
+ else
28
+ open_connection channel, params
29
+ end
30
+ end
31
+
32
+ # @return true if a connection exists and is started
33
+ def connected?
34
+ @connection && @connection.started?
35
+ end
36
+
37
+ # close the active connection
38
+ def close
39
+ return unless connected?
40
+ @connection.finish()
41
+ @current_thread.kill
42
+ @current_thread = nil
43
+ end
44
+
45
+ # Define a block called on message received
46
+ # The received message is passed to the block as a var
47
+ def on_message(&block)
48
+ @on_message_blk = block
49
+ end
50
+
51
+ # Define a block called on error
52
+ # An optional param with the error should be pass if present
53
+ def on_error(&block)
54
+ @on_error_blk = block
55
+ end
56
+
57
+ # Define a block called on connection closed
58
+ def on_close(&block)
59
+ @on_close_blk = block
60
+ end
61
+
62
+ # Define a block called on connect
63
+ def on_connect(&block)
64
+ @on_connect_blk = block
65
+ end
66
+
67
+ private
68
+
69
+ # Open a persistent connection to the neighbor
70
+ # TODO: Refactor, EM??
71
+ def open_connection(channel, params={})
72
+ begin
73
+ uri = URI(params[:server])
74
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
75
+ http.read_timeout = 9999999999999999 # TODO Fix this
76
+ request = Net::HTTP::Get.new URI.escape("/open?channel=#{channel}")
77
+ @connection = http
78
+ @on_connect_blk.call if @on_connect_blk
79
+ http.request request do |response|
80
+ response.read_body do |chunk|
81
+ if chunk.start_with? "data:"
82
+ @on_message_blk.call(chunk[5..-3]) if @on_message_blk # Remove data: and \n\n
83
+ end
84
+ end
85
+ end
86
+ end
87
+ rescue
88
+ @on_error_blk.call($!) if @on_error_blk
89
+ end
90
+ @on_close_blk.call if @on_close_blk
91
+ end
92
+ end
@@ -0,0 +1,31 @@
1
+ module Neighborparrot
2
+
3
+ # Post a message to a channel
4
+ # Raise exception if channel is not setted
5
+ # If empty data, refuse to send nothing
6
+ # @param [String] channel: The channel name
7
+ # @param [String] string to send
8
+ # @param [Hash] params
9
+ # * :api_id => Your api ID in neighborparrot.com
10
+ # * :api_key => Your api key
11
+ # * :server => Server to connect (Only for development)
12
+ # @return [Boolean] true if sended
13
+ def self.post(channel, data, params={})
14
+ raise "Channel can't be nil" if channel.nil? || channel.length == 0
15
+ return false if data.nil? || data.length == 0
16
+ params = self.configuration.merge params
17
+ self.check_params params
18
+
19
+ uri = URI(params[:server])
20
+ Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
21
+ request = Net::HTTP::Post.new('/post')
22
+ request.set_form_data(params.merge({ :channel => channel, :data => data }))
23
+ response = http.request(request)
24
+ return true if response.body == "Ok"
25
+ end
26
+ end
27
+
28
+ def post(channel, data, params={})
29
+ Neighborparrot.post(channel, data, params)
30
+ end
31
+ end
@@ -1,115 +1,32 @@
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")
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'uri'
8
4
 
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
5
+ module Neighborparrot
16
6
 
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
7
+ # Setup the configuration options
8
+ # * :api_id => Your api ID in neighborparrot.com
9
+ # * :api_key => Your api key
10
+ # * :server => Server to connect (Only for development)
11
+ def self.configure(params={})
12
+ @@config.merge! params
30
13
  end
31
14
 
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
15
+ # Return settings
16
+ def self.configuration
17
+ @@config
54
18
  end
55
19
 
56
- # @return true if a connection exists and is started
57
- def connected?
58
- @connection && @connection.started?
59
- end
20
+ private
60
21
 
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
22
+ DEFAULT_SERVER = 'https://neighborparrot.net'
23
+ @@config = { :server => DEFAULT_SERVER }
68
24
 
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
25
+ def self.check_params(p)
26
+ raise "ERROR# Neighborparrot: api_id can't be nil" if p[:api_id].nil? || p[:api_id].length == 0
27
+ raise "ERROR# Neighborparrot: api_key can't be nil" if p[:api_key].nil? || p[:api_key].length == 0
114
28
  end
115
29
  end
30
+
31
+ require 'neighborparrot/post'
32
+ require 'neighborparrot/esparrot'
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "neighborparrot"
8
+ s.version = "0.3.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Eloy Gomez"]
12
+ s.date = "2012-01-06"
13
+ s.description = "Send messages to the neighborparrot event source service"
14
+ s.email = "eloy@indeos.es"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "lib/neighborparrot.rb",
29
+ "lib/neighborparrot/esparrot.rb",
30
+ "lib/neighborparrot/post.rb",
31
+ "neighborparrot.gemspec",
32
+ "spec/esparrot_spec.rb",
33
+ "spec/neighborparrot_spec.rb",
34
+ "spec/spec_helper.rb",
35
+ "spec/support/helpers.rb"
36
+ ]
37
+ s.homepage = "http://neighborparrot.com"
38
+ s.licenses = ["MIT"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = "1.8.10"
41
+ s.summary = "Ruby client for the Neighborparrot"
42
+
43
+ if s.respond_to? :specification_version then
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Neighborparrot::ESParrot" do
4
+ before :all do
5
+ api_id = 'test-id'
6
+ api_key = 'api_key'
7
+ Neighborparrot.configure({ :api_id => api_id, :api_key => :api_key })
8
+ end
9
+
10
+ before :each do
11
+ @parrot = ESParrot.new
12
+ end
13
+
14
+ describe "Neighborparrot::ESParrot#open" do
15
+ after :each do
16
+ @parrot.close
17
+ end
18
+
19
+ it "should open a connection with correct values" do
20
+ connected = false
21
+ @parrot.on_connect do
22
+ connected = true
23
+ end
24
+ @parrot.open('test')
25
+ sleep(2)
26
+ connected.should be_true
27
+ end
28
+
29
+ it "should receive messages" do
30
+ received = nil
31
+ @parrot.on_message do |msg|
32
+ received = msg
33
+ end
34
+ @parrot.open('test')
35
+ sleep(2)
36
+ text = Faker::Lorem.paragraph(30)
37
+ @parrot.post('test', text)
38
+ sleep(1)
39
+ received.should eq text
40
+ end
41
+
42
+ it "should return false if already a connection active" do
43
+ @parrot.open('test')
44
+ sleep(2)
45
+ @parrot.open('other test').should be_false
46
+ end
47
+ end
48
+
49
+ describe "Neighborparrot::ESParrot#close" do
50
+ it "should close a connection" do
51
+ @parrot.open('test')
52
+ sleep(2)
53
+ @parrot.close()
54
+ @parrot.connected?.should be_false
55
+ end
56
+ end
57
+
58
+ describe "Neighborparrot::ESParrot#connected?" do
59
+ it "should be false before connected" do
60
+ @parrot.connected?.should be_false
61
+ @parrot.close
62
+ end
63
+
64
+ it "should be true when connected" do
65
+ @parrot.open('test')
66
+ sleep(2)
67
+ @parrot.connected?.should be_true
68
+ @parrot.close
69
+ end
70
+ end
71
+ end
@@ -2,107 +2,40 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Neighborparrot" do
4
4
  before :each do
5
- @key = 'test_key'
6
- @parrot = Neighborparrot.new(@key)
5
+ api_id = 'test-id'
6
+ api_key = 'api_key'
7
+ Neighborparrot.configure({ :api_id => api_id, :api_key => :api_key })
7
8
  end
8
9
 
9
10
  describe "Neigborparrot#post" do
10
- it "should rails exception when instantiate without key" do
11
- expect { Neighborparrot.new() }.to raise_error
11
+ it 'should return true if no errors' do
12
+ test_post.should be_true
12
13
  end
13
14
 
14
- it "should rails exception when nil key" do
15
- expect { Neighborparrot.new(nil) }.to raise_error
15
+ it "should rails exception without id" do
16
+ Neighborparrot.configure({ :api_key => nil })
17
+ expect { test_post }.to raise_error
16
18
  end
17
19
 
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])
20
+ it "should rails exception without key" do
21
+ Neighborparrot.configure({:api_id => nil})
22
+ expect { test_post }.to raise_error
27
23
  end
28
24
 
29
25
  it "should raise exception with nill channel" do
30
- expect { @parrot.post(nil, 'test string') }.to raise_error
26
+ expect { Neighborparrot.post(nil, 'test string') }.to raise_error
31
27
  end
32
28
 
33
29
  it "should raise exception with empty channel" do
34
- expect { @parrot.post('', 'test string') }.to raise_error
30
+ expect { Neighborparrot.post('', 'test string') }.to raise_error
35
31
  end
36
32
 
37
33
  it "should not send message with nil data" do
38
- Net::HTTP.should_not_receive(:post_form)
39
- @parrot.post('test-channel', nil)
34
+ Neighborparrot.post('test-channel', nil).should be_false
40
35
  end
41
36
 
42
37
  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
38
+ Neighborparrot.post('test-channel', '').should be_false
106
39
  end
107
40
  end
108
41
  end
data/spec/spec_helper.rb CHANGED
@@ -1,12 +1,14 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
  $LOAD_PATH.unshift(File.dirname(__FILE__))
3
3
  require 'rspec'
4
- require 'neighborparrot'
4
+ require 'faker'
5
+
6
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/neighborparrot')
5
7
 
6
8
  # Requires supporting files with custom matchers and macros, etc,
7
9
  # in ./support/ and its subdirectories.
8
10
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
11
 
10
12
  RSpec.configure do |config|
11
-
13
+
12
14
  end
@@ -0,0 +1,4 @@
1
+ # Post 'test' string to 'test_channel'
2
+ def test_post
3
+ Neighborparrot.post('test_channel', 'test')
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.2.0
4
+ version: 0.3.0
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-02 00:00:00.000000000Z
12
+ date: 2012-01-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &15247940 !ruby/object:Gem::Requirement
16
+ requirement: &26510820 !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: *15247940
24
+ version_requirements: *26510820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &15247200 !ruby/object:Gem::Requirement
27
+ requirement: &26510280 !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: *15247200
35
+ version_requirements: *26510280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &15246640 !ruby/object:Gem::Requirement
38
+ requirement: &26509780 !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: *15246640
46
+ version_requirements: *26509780
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rcov
49
- requirement: &15245900 !ruby/object:Gem::Requirement
49
+ requirement: &26509200 !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: *15245900
57
+ version_requirements: *26509200
58
58
  description: Send messages to the neighborparrot event source service
59
59
  email: eloy@indeos.es
60
60
  executables: []
@@ -72,8 +72,13 @@ files:
72
72
  - Rakefile
73
73
  - VERSION
74
74
  - lib/neighborparrot.rb
75
+ - lib/neighborparrot/esparrot.rb
76
+ - lib/neighborparrot/post.rb
77
+ - neighborparrot.gemspec
78
+ - spec/esparrot_spec.rb
75
79
  - spec/neighborparrot_spec.rb
76
80
  - spec/spec_helper.rb
81
+ - spec/support/helpers.rb
77
82
  homepage: http://neighborparrot.com
78
83
  licenses:
79
84
  - MIT
@@ -89,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
94
  version: '0'
90
95
  segments:
91
96
  - 0
92
- hash: 3143593421196085716
97
+ hash: -3144654155739837993
93
98
  required_rubygems_version: !ruby/object:Gem::Requirement
94
99
  none: false
95
100
  requirements: