codenjoy_connection 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 73d9766f6477069b250642227a286390ca5460f7
4
+ data.tar.gz: cbbe2261419d55be792cfec957136463ea513d0a
5
+ SHA512:
6
+ metadata.gz: 12992ed74b1d83e2cd12ba341d6c5260f9db54cc6272e80cc2d39473da53572ea981afd6cb20658ddd94c9baf58966cb7fba5ad29a8404e6ba78bd3fa32c17c4
7
+ data.tar.gz: 84987dcd442c3a64ef39224731c911264133c36b8f7ffa0f2f597b3bcc9b88f6230e952f4754d516eca06d09dd23fd2aa53479a861700a6f8b50ecda1a41319d
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in codenjoy_connection.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 anatoliliotych
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # CodenjoyConnection
2
+
3
+ Gem handling ws connection and simple communication for numerous games in Codenjoy.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'codenjoy_connection'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install codenjoy_connection
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/anatoliliotych/codenjoy_connection/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'codenjoy_connection/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'codenjoy_connection'
8
+ spec.version = CodenjoyConnection::VERSION
9
+ spec.authors = ['anatoliliotych']
10
+ spec.email = ['anatoli.liotych@gmail.com']
11
+ spec.summary = %q{Gem handling ws connection and simple communication for numerous games in Codenjoy.}
12
+ spec.homepage = 'https://github.com/anatoliliotych/codenjoy_connection'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'eventmachine'
21
+ spec.add_dependency 'faye-websocket'
22
+
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake'
25
+ spec.add_development_dependency 'rspec'
26
+ end
@@ -0,0 +1,38 @@
1
+ require "codenjoy_connection/version"
2
+ require "codenjoy_connection/exceptions"
3
+ require "codenjoy_connection/player"
4
+ require 'eventmachine'
5
+ require 'faye/websocket'
6
+
7
+ module CodenjoyConnection
8
+ def self.prepare_url(opts = {})
9
+
10
+ raise CodenjoyConnection::GenericError.new("Please, specify connection params.") if opts == {}
11
+
12
+ host, port, username, player = opts[:host], opts[:port], opts[:username], opts[:player]
13
+ game_url = opts[:game_url] ||'tetris-contest/ws?'
14
+
15
+ raise CodenjoyConnection::GenericError.new("Please, specify a host for connection.") unless host
16
+ raise CodenjoyConnection::GenericError.new("Please, specify a port for connection.") unless port
17
+ raise CodenjoyConnection::GenericError.new("Please, specify a username for connection.") unless username
18
+
19
+ "ws://#{host}:#{port}/#{game_url}user=#{username}"
20
+ end
21
+
22
+ def self.play(player, opts)
23
+ url = prepare_url(opts)
24
+ player = CodenjoyConnection::Player.new(player)
25
+ set_connection(player,url)
26
+ end
27
+
28
+ def self.set_connection(player,url)
29
+ EM.run do
30
+ ws = Faye::WebSocket::Client.new(url)
31
+ ws.on :message do |event|
32
+ p [:message, event.data]
33
+ player.process_data(event.data)
34
+ ws.send(player.make_step)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ module CodenjoyConnection
2
+ class GenericError < StandardError
3
+ def initialize(msg)
4
+ super(msg)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module CodenjoyConnection
2
+ class Player
3
+ def initialize(player)
4
+ raise CodenjoyConnection::GenericError.new('Please, override #process_data in your player class.') unless player.respond_to?(:process_data)
5
+ raise CodenjoyConnection::GenericError.new('Please, override #make_step in your player class.') unless player.respond_to?(:make_step)
6
+
7
+ @player = player
8
+ end
9
+
10
+ def process_data(data)
11
+ @player.process_data(data)
12
+ end
13
+
14
+ def make_step
15
+ @player.make_step
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module CodenjoyConnection
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe CodenjoyConnection do
4
+ before(:each) do
5
+ @params = {:host => 'localhost', :port => '8080', :username => 'anatoli'}
6
+ end
7
+
8
+ describe "#prepare_url" do
9
+ describe 'params validation' do
10
+ it "should fail without required params" do
11
+ expect{ CodenjoyConnection.prepare_url }.to raise_error(CodenjoyConnection::GenericError, "Please, specify connection params.")
12
+ end
13
+
14
+ it "should fail without host param" do
15
+ params = @params.tap{|x| x.delete(:host)}
16
+ expect{ CodenjoyConnection.prepare_url(params) }.to raise_error(CodenjoyConnection::GenericError, "Please, specify a host for connection.")
17
+ end
18
+
19
+ it "should fail without port param" do
20
+ params = @params.tap{|x| x.delete(:port)}
21
+ expect{ CodenjoyConnection.prepare_url(params) }.to raise_error(CodenjoyConnection::GenericError, "Please, specify a port for connection.")
22
+ end
23
+
24
+ it "should fail without username param" do
25
+ params = @params.tap{|x| x.delete(:username)}
26
+ expect{ CodenjoyConnection.prepare_url(params) }.to raise_error(CodenjoyConnection::GenericError, "Please, specify a username for connection.")
27
+ end
28
+ end
29
+
30
+ describe 'url validation' do
31
+ it "should prepair correct url without game_url" do
32
+ url = CodenjoyConnection.prepare_url(@params)
33
+ expect(url).to eq('ws://localhost:8080/tetris-contest/ws?user=anatoli')
34
+ end
35
+
36
+ it "should prepair correct url with game_url" do
37
+ url = CodenjoyConnection.prepare_url(@params.merge(:game_url => 'mine/ws?'))
38
+ expect(url).to eq('ws://localhost:8080/mine/ws?user=anatoli')
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "#play" do
44
+ before(:each) do
45
+ @player = double({})
46
+ allow(@player).to receive(:process_data)
47
+ allow(@player).to receive(:make_step)
48
+
49
+ expect(CodenjoyConnection).to receive(:prepare_url).with(@params)
50
+ expect(CodenjoyConnection).to receive(:set_connection)
51
+ end
52
+
53
+ it "should call #prepare_url and #set_connection" do
54
+ CodenjoyConnection.play(@player, @params)
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ describe CodenjoyConnection::Player do
3
+ describe "validation" do
4
+ before(:each) do
5
+ @player = double({})
6
+ end
7
+ it "should fail if player object has no #process_data" do
8
+ expect{ CodenjoyConnection::Player.new(@player) }.to raise_error(CodenjoyConnection::GenericError, 'Please, override #process_data in your player class.')
9
+ end
10
+
11
+ it "should fail if player object has no #make_step" do
12
+ allow(@player).to receive(:process_data)
13
+ expect{ CodenjoyConnection::Player.new(@player) }.to raise_error(CodenjoyConnection::GenericError, 'Please, override #make_step in your player class.')
14
+ end
15
+ end
16
+
17
+ describe 'methods' do
18
+ before(:each) do
19
+ @player = double({})
20
+ allow(@player).to receive(:process_data)
21
+ allow(@player).to receive(:make_step)
22
+ @cj_player = CodenjoyConnection::Player.new(@player)
23
+ end
24
+
25
+ describe "#process_data" do
26
+ it 'should call #process_data on provided object with params' do
27
+ expect(@player).to receive(:process_data).with("")
28
+ @cj_player.process_data("")
29
+ end
30
+ end
31
+
32
+ describe "#make_step" do
33
+ it 'should call #make_step on provided object' do
34
+ expect(@player).to receive(:make_step)
35
+ @cj_player.make_step
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+
@@ -0,0 +1,12 @@
1
+ require 'codenjoy_connection'
2
+
3
+ RSpec.configure do |config|
4
+ # Use color in STDOUT
5
+ config.color = true
6
+
7
+ # Use color not only in STDOUT but also in pagers and files
8
+ config.tty = true
9
+
10
+ # Use the specified formatter
11
+ config.formatter = :documentation # :progress, :html, :textmate
12
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codenjoy_connection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - anatoliliotych
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: eventmachine
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faye-websocket
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - anatoli.liotych@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - codenjoy_connection.gemspec
96
+ - lib/codenjoy_connection.rb
97
+ - lib/codenjoy_connection/exceptions.rb
98
+ - lib/codenjoy_connection/player.rb
99
+ - lib/codenjoy_connection/version.rb
100
+ - spec/lib/codenjoy_connection_spec.rb
101
+ - spec/lib/player_spec.rb
102
+ - spec/spec_helper.rb
103
+ homepage: https://github.com/anatoliliotych/codenjoy_connection
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.2
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Gem handling ws connection and simple communication for numerous games in
127
+ Codenjoy.
128
+ test_files:
129
+ - spec/lib/codenjoy_connection_spec.rb
130
+ - spec/lib/player_spec.rb
131
+ - spec/spec_helper.rb