minep-request 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -13
- data/Rakefile +1 -0
- data/bin/mine-request +70 -29
- data/lib/minep-request.rb +5 -1
- data/lib/minep-request/minep.rb +26 -0
- data/lib/minep-request/requests.rb +102 -0
- data/lib/minep-request/version.rb +6 -0
- data/minep-request.gemspec +23 -0
- metadata +40 -17
- data/lib/minep.rb +0 -8
- data/lib/minep/connection.rb +0 -44
- data/lib/minep/exceptions.rb +0 -18
- data/lib/minep/minep.rb +0 -17
- data/lib/minep/request.rb +0 -108
- data/lib/minep/utils.rb +0 -70
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Matthieu "Ezveus" Ciappara
|
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
CHANGED
@@ -1,17 +1,63 @@
|
|
1
|
-
minep-request
|
2
|
-
=============
|
1
|
+
# minep-request
|
3
2
|
|
4
|
-
minep-request is a
|
3
|
+
minep-request is a binary allowing to communicate with a [MINE
|
4
|
+
server](http://github.com/Ezveus/Mine).
|
5
5
|
|
6
|
-
|
6
|
+
It uses MINE Protocol over TCP using Ruby Standard Sockets or over
|
7
|
+
WebSocket Protocol using
|
8
|
+
[WEBSocket](https://rubygems.org/gems/WEBSocket).
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
+
It works as an interactive 'shell' reading some commands (or requests
|
11
|
+
arguments) on the standard input and sends valid MINEP requests to the
|
12
|
+
server.
|
10
13
|
|
11
|
-
|
12
|
-
-
|
13
|
-
-
|
14
|
-
-
|
15
|
-
-
|
16
|
-
-
|
17
|
-
-
|
14
|
+
The known commands are :
|
15
|
+
- authenticate : Send an AUTHENTICATE request
|
16
|
+
- backspace : Send a BACKSPACE request
|
17
|
+
- delete : Send a DELETE request
|
18
|
+
- exec : Send an EXEC request
|
19
|
+
- exit : Disconnect and quit the program
|
20
|
+
- help : Show the list of the commands
|
21
|
+
- insert : Send an INSERT request
|
22
|
+
- move : Send a MOVE request
|
23
|
+
- quit : Disconnect and quit the program
|
24
|
+
- signup : Send a SIGNUP request
|
25
|
+
|
26
|
+
Without arguments, the default communication type is TCP trying to
|
27
|
+
connect to localhost on port 8080 (default MINE over TCP port). See
|
28
|
+
the [usage](#usage) to change the host, port or communication type.
|
29
|
+
|
30
|
+
If the communication type is WebSocket and no port is given, the port
|
31
|
+
is updated to Mine over WebSocket default port (8081).
|
32
|
+
|
33
|
+
## Installation
|
34
|
+
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
gem 'minep-request'
|
38
|
+
|
39
|
+
And then execute:
|
40
|
+
|
41
|
+
$ bundle
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
$ gem install minep-request
|
46
|
+
|
47
|
+
## <div id="usage">Usage</div>
|
48
|
+
|
49
|
+
Usage: mine-request [OPTIONS]
|
50
|
+
Send a request to a MINE server
|
51
|
+
-h, --host HOST server hostname
|
52
|
+
-p, --port PORT server port
|
53
|
+
-t, --type TYPE type of communication (tcp |
|
54
|
+
websocket)
|
55
|
+
--help displays this screen
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
1. Fork it
|
60
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
61
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
62
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
63
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/mine-request
CHANGED
@@ -1,32 +1,65 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
+
# lib = "lib"
|
4
|
+
# $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
3
6
|
require 'optparse'
|
4
|
-
require '
|
5
|
-
require '
|
7
|
+
require 'socket'
|
8
|
+
require 'WEBSocket'
|
9
|
+
require 'minep-request'
|
10
|
+
|
11
|
+
$options = {
|
12
|
+
:host => "localhost",
|
13
|
+
:port => 8080,
|
14
|
+
:type => :tcp
|
15
|
+
}
|
16
|
+
$goon = true
|
17
|
+
|
18
|
+
$commands = {
|
19
|
+
"authenticate" => Proc.new { |s| Minep::Request.authenticate s},
|
20
|
+
"signup" => Proc.new { |s| Minep::Request.signup s},
|
21
|
+
"exec" => Proc.new { |s| Minep::Request.exec s},
|
22
|
+
"insert" => Proc.new { |s| Minep::Request.insert s},
|
23
|
+
"move" => Proc.new { |s| Minep::Request.move s},
|
24
|
+
"backspace" => Proc.new { |s| Minep::Request.backspace s},
|
25
|
+
"delete" => Proc.new { |s| Minep::Request.delete s},
|
26
|
+
"file" => Proc.new { |s| Minep::Request.file s},
|
27
|
+
"help" => Proc.new { |s| help s},
|
28
|
+
"exit" => Proc.new { |s| quit s},
|
29
|
+
"quit" => Proc.new { |s| quit s}
|
30
|
+
}
|
31
|
+
|
32
|
+
def quit socket = nil
|
33
|
+
$goon = false
|
34
|
+
puts "Exiting"
|
35
|
+
end
|
6
36
|
|
7
|
-
|
8
|
-
|
37
|
+
def help socket = nil
|
38
|
+
puts "Valid commands are :"
|
39
|
+
$commands.keys.sort.each do |c|
|
40
|
+
puts "#{c} : #{Minep::Request::CommandsInfo[c]}"
|
41
|
+
end
|
42
|
+
end
|
9
43
|
|
10
44
|
optparse = OptionParser.new do |opts|
|
45
|
+
portSpecified = false
|
11
46
|
opts.banner = <<-eos
|
12
|
-
Usage: mine-request
|
47
|
+
Usage: mine-request [OPTIONS]
|
13
48
|
Send a request to a MINE server
|
14
|
-
|
15
|
-
Valid requests are :
|
16
|
-
#{MINEP.requestList}
|
17
49
|
eos
|
18
50
|
|
19
|
-
opts.on("-r", "--request REQUEST",
|
20
|
-
MINEP.requestList, "request to send") do |r|
|
21
|
-
request = r
|
22
|
-
end
|
23
|
-
|
24
51
|
opts.on "-h", "--host HOST", "server hostname" do |h|
|
25
|
-
options[:host] = h
|
52
|
+
$options[:host] = h
|
26
53
|
end
|
27
54
|
|
28
55
|
opts.on "-p", "--port PORT", Integer, "server port" do |p|
|
29
|
-
options[:port] = p
|
56
|
+
$options[:port] = p
|
57
|
+
portSpecified = true
|
58
|
+
end
|
59
|
+
|
60
|
+
opts.on "-t", "--type TYPE", [:tcp, :websocket], "type of communication (tcp | websocket)" do |t|
|
61
|
+
$options[:type] = t
|
62
|
+
$options[:port] = 8081 unless portSpecified
|
30
63
|
end
|
31
64
|
|
32
65
|
opts.on "--help", "displays this screen" do |h|
|
@@ -37,25 +70,33 @@ end
|
|
37
70
|
|
38
71
|
begin
|
39
72
|
optparse.parse!
|
40
|
-
if request.nil?
|
41
|
-
puts "Missing option : request\n"
|
42
|
-
puts optparse
|
43
|
-
exit 1
|
44
|
-
end
|
45
73
|
rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::InvalidArgument
|
46
74
|
puts $!.to_s
|
47
75
|
puts optparse
|
48
76
|
exit 1
|
49
|
-
end
|
50
|
-
|
51
|
-
ARGV.each do |arg|
|
52
|
-
args = arg.split '='
|
53
|
-
options[args[0].to_sym] = args[1]
|
54
77
|
end
|
55
|
-
|
78
|
+
|
56
79
|
begin
|
57
|
-
|
58
|
-
|
59
|
-
$
|
80
|
+
if $options[:type] == :tcp
|
81
|
+
socket = TCPSocket.new $options[:host], $options[:port]
|
82
|
+
elsif $options[:type] == :websocket
|
83
|
+
socket = WEBSocket::Socket.new $options[:host], $options[:port]
|
84
|
+
end
|
85
|
+
rescue => e
|
86
|
+
$stderr.puts "Error : #{e} <#{$options[:host]} - #{$options[:port]}>"
|
60
87
|
exit 1
|
61
88
|
end
|
89
|
+
|
90
|
+
while $goon do
|
91
|
+
printf ">> "
|
92
|
+
begin
|
93
|
+
cmd = Minep.read
|
94
|
+
if $commands.key? cmd
|
95
|
+
$commands[cmd].call socket
|
96
|
+
else
|
97
|
+
$stderr.puts "Error : unknown command"
|
98
|
+
end
|
99
|
+
rescue EOFError
|
100
|
+
quit
|
101
|
+
end
|
102
|
+
end
|
data/lib/minep-request.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Minep
|
2
|
+
def self.read
|
3
|
+
input = $stdin.readline
|
4
|
+
input = input.strip
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.makeMsg request, args
|
8
|
+
args.each_key do |k|
|
9
|
+
if k == :args
|
10
|
+
printf "#{k} as a space-separated list : "
|
11
|
+
input = read
|
12
|
+
args[k] = input.split
|
13
|
+
elsif k == :direction
|
14
|
+
printf "#{k} (up, right, down, left, origin, end) : "
|
15
|
+
args[k] = read
|
16
|
+
elsif k == :number
|
17
|
+
printf "#{k} : "
|
18
|
+
args[k] = read.to_i
|
19
|
+
else
|
20
|
+
printf "#{k} : "
|
21
|
+
args[k] = read
|
22
|
+
end
|
23
|
+
end
|
24
|
+
"#{request.upcase}=#{JSON.dump args}"
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
module Minep
|
2
|
+
module Request
|
3
|
+
CommandsInfo ||= {
|
4
|
+
"authenticate" => "Send an AUTHENTICATE request",
|
5
|
+
"signup" => "Send a SIGNUP request",
|
6
|
+
"exec" => "Send an EXEC request",
|
7
|
+
"insert" =>"Send an INSERT request",
|
8
|
+
"move" => "Send a MOVE request",
|
9
|
+
"backspace" => "Send a BACKSPACE request",
|
10
|
+
"delete" => "Send a DELETE request",
|
11
|
+
"file" => "Send a FILE request",
|
12
|
+
"help" => "Show the list of the commands",
|
13
|
+
"exit" => "Disconnect and quit the program",
|
14
|
+
"quit" => "Disconnect and quit the program"
|
15
|
+
}
|
16
|
+
|
17
|
+
def self.CommandsInfo
|
18
|
+
CommandsInfo
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.file socket
|
22
|
+
args = {
|
23
|
+
:path => "",
|
24
|
+
:size => 0,
|
25
|
+
:line => 0,
|
26
|
+
:port => 0
|
27
|
+
}
|
28
|
+
printf "path : "
|
29
|
+
args[:path] = read
|
30
|
+
printf "line number : "
|
31
|
+
args[:line] = read.to_i
|
32
|
+
if File.exist? args[:path]
|
33
|
+
f = File.new args[:path]
|
34
|
+
args[:size] = f.size
|
35
|
+
args[:content] = f.read
|
36
|
+
socket.write "FILE=#{JSON.dump args}"
|
37
|
+
else
|
38
|
+
$stderr.puts "File #{args[:path]} doesn't exists"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.authenticate socket
|
43
|
+
args = {
|
44
|
+
:name => "",
|
45
|
+
:pass => ""
|
46
|
+
}
|
47
|
+
socket.write Minep.makeMsg("AUTHENTICATE", args)
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.signup socket
|
51
|
+
args = {
|
52
|
+
:name => "",
|
53
|
+
:pass => "",
|
54
|
+
:email => "",
|
55
|
+
:website => ""
|
56
|
+
}
|
57
|
+
socket.write Minep.makeMsg("SIGNUP", args)
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.exec socket
|
61
|
+
args = {
|
62
|
+
:buffer => "",
|
63
|
+
:command => "",
|
64
|
+
:args => []
|
65
|
+
}
|
66
|
+
socket.write Minep.makeMsg("EXEC", args)
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.insert socket
|
70
|
+
args = {
|
71
|
+
:buffer => "",
|
72
|
+
:text => ""
|
73
|
+
}
|
74
|
+
socket.write Minep.makeMsg("INSERT", args)
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.move socket
|
78
|
+
args = {
|
79
|
+
:buffer => "",
|
80
|
+
:direction => "",
|
81
|
+
:number => 0
|
82
|
+
}
|
83
|
+
socket.write Minep.makeMsg("MOVE", args)
|
84
|
+
end
|
85
|
+
|
86
|
+
def self.backspace socket
|
87
|
+
args = {
|
88
|
+
:buffer => "",
|
89
|
+
:number => 0
|
90
|
+
}
|
91
|
+
socket.write Minep.makeMsg("BACKSPACE", args)
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.delete socket
|
95
|
+
args = {
|
96
|
+
:buffer => "",
|
97
|
+
:number => 0
|
98
|
+
}
|
99
|
+
socket.write Minep.makeMsg("DELETE", args)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'minep-request/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "minep-request"
|
8
|
+
gem.version = Minep::Request::VERSION
|
9
|
+
gem.date = Minep::Request::DATE
|
10
|
+
gem.authors = ["Matthieu \"Ezveus\" Ciappara"]
|
11
|
+
gem.email = ["ciappam@gmail.com"]
|
12
|
+
gem.description = <<-EOS
|
13
|
+
Binary allowing the communication with a MINE server. It support both MINE Protocol over TCP and over WebSocket.
|
14
|
+
EOS
|
15
|
+
gem.summary = %q{Binary allowing the communication with a MINE server}
|
16
|
+
gem.homepage = "https://github.com/Ezveus/minep-request"
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
gem.add_runtime_dependency 'WEBSocket', '~> 0.0.1', '>= 0.0.1'
|
23
|
+
end
|
metadata
CHANGED
@@ -1,37 +1,60 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minep-request
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
- Matthieu Ciappara
|
8
|
+
- Matthieu "Ezveus" Ciappara
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
date: 2013-01-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: WEBSocket
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.0.1
|
22
|
+
- - ! '>='
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 0.0.1
|
25
|
+
type: :runtime
|
26
|
+
prerelease: false
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.0.1
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 0.0.1
|
36
|
+
description: ! 'Binary allowing the communication with a MINE server. It support both
|
37
|
+
MINE Protocol over TCP and over WebSocket.
|
18
38
|
|
19
39
|
'
|
20
|
-
email:
|
40
|
+
email:
|
41
|
+
- ciappam@gmail.com
|
21
42
|
executables:
|
22
43
|
- mine-request
|
23
44
|
extensions: []
|
24
45
|
extra_rdoc_files: []
|
25
46
|
files:
|
26
|
-
-
|
27
|
-
-
|
28
|
-
-
|
29
|
-
- lib/minep/exceptions.rb
|
30
|
-
- lib/minep/minep.rb
|
31
|
-
- lib/minep/request.rb
|
32
|
-
- lib/minep/utils.rb
|
47
|
+
- .gitignore
|
48
|
+
- Gemfile
|
49
|
+
- LICENSE.txt
|
33
50
|
- README.md
|
51
|
+
- Rakefile
|
34
52
|
- bin/mine-request
|
53
|
+
- lib/minep-request.rb
|
54
|
+
- lib/minep-request/minep.rb
|
55
|
+
- lib/minep-request/requests.rb
|
56
|
+
- lib/minep-request/version.rb
|
57
|
+
- minep-request.gemspec
|
35
58
|
homepage: https://github.com/Ezveus/minep-request
|
36
59
|
licenses: []
|
37
60
|
post_install_message:
|
@@ -55,6 +78,6 @@ rubyforge_project:
|
|
55
78
|
rubygems_version: 1.8.24
|
56
79
|
signing_key:
|
57
80
|
specification_version: 3
|
58
|
-
summary:
|
81
|
+
summary: Binary allowing the communication with a MINE server
|
59
82
|
test_files: []
|
60
83
|
has_rdoc:
|
data/lib/minep.rb
DELETED
data/lib/minep/connection.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
module MINEP
|
2
|
-
# This class extends EM::Connection (the connection class of EventMachine)
|
3
|
-
class Connection < EM::Connection
|
4
|
-
# Initialize the object
|
5
|
-
#
|
6
|
-
# @param [Hash] request is the request, The text request is built from this
|
7
|
-
def initialize request, closeLoop=false
|
8
|
-
super
|
9
|
-
@request = ["#{request[:header][:head]}\n"]
|
10
|
-
@closeLoop = closeLoop
|
11
|
-
request[:header].each do |key, value|
|
12
|
-
if key == :head
|
13
|
-
next
|
14
|
-
elsif key == :contentType
|
15
|
-
@request << "Content-Type: #{value}\n"
|
16
|
-
elsif key == :contentLength
|
17
|
-
@request << "Content-Length: #{value}\n"
|
18
|
-
else
|
19
|
-
@request << "#{key.to_s.capitalize}: #{value}\n"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
@request << "\n"
|
23
|
-
key = request[:body].keys[0]
|
24
|
-
rqst = "#{key.to_s.upcase}="
|
25
|
-
rqst += "#{JSON.dump request[:body][key]}\n"
|
26
|
-
@request << rqst
|
27
|
-
@request = @request.join
|
28
|
-
end
|
29
|
-
|
30
|
-
# Send the request after the connection initialization
|
31
|
-
def post_init
|
32
|
-
send_data @request
|
33
|
-
end
|
34
|
-
|
35
|
-
# Receive the answer of the server and puts it on standard output
|
36
|
-
def receive_data data
|
37
|
-
puts data
|
38
|
-
end
|
39
|
-
|
40
|
-
def unbind
|
41
|
-
EventMachine::stop_event_loop if @closeLoop
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
data/lib/minep/exceptions.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
module MINEP
|
2
|
-
class ArgumentsError < Exception
|
3
|
-
end
|
4
|
-
|
5
|
-
# Raise a ArgumentsError with "Not enough arguments for request" as an argument
|
6
|
-
#
|
7
|
-
# @param [String] rqst the request which called the function
|
8
|
-
def self.notEnoughArgs rqst
|
9
|
-
raise ArgumentsError, "Not enough arguments for request #{rqst}"
|
10
|
-
end
|
11
|
-
|
12
|
-
# Raise a ArgumentsError with "Missing mandatory arguments for request" as an argument
|
13
|
-
#
|
14
|
-
# @param [String] rqst the request which called the function
|
15
|
-
def self.missingArgs rqst
|
16
|
-
raise ArgumentsError, "Missing mandatory arguments for request #{rqst}"
|
17
|
-
end
|
18
|
-
end
|
data/lib/minep/minep.rb
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
module MINEP
|
2
|
-
# Send a request to opts\[:host\] or localhost on port
|
3
|
-
# opts\[:port\] or 8080
|
4
|
-
#
|
5
|
-
# @param [Symbol] type the request type, see main page for the requests list
|
6
|
-
# @param [Hash] opts stores the parameters of the request and host and port, see main page for the requests parameters
|
7
|
-
# @param [Bool] closeLoop define if the event loop has to be closed when unbinding
|
8
|
-
def self.send type, opts={}
|
9
|
-
request = {}
|
10
|
-
return false unless Utils.makeRequest request, type, opts
|
11
|
-
EM.run do
|
12
|
-
EM.connect(opts[:host] || "localhost",
|
13
|
-
opts[:port] || 8080, Connection, request,
|
14
|
-
opts[:closeLoop])
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
data/lib/minep/request.rb
DELETED
@@ -1,108 +0,0 @@
|
|
1
|
-
module MINEP
|
2
|
-
RequestList = [
|
3
|
-
:authenticate,
|
4
|
-
:signup,
|
5
|
-
:exec,
|
6
|
-
:insert,
|
7
|
-
:move,
|
8
|
-
:backspace,
|
9
|
-
:delete
|
10
|
-
]
|
11
|
-
|
12
|
-
# Returns an array containing the request list
|
13
|
-
def self.requestList
|
14
|
-
RequestList
|
15
|
-
end
|
16
|
-
|
17
|
-
# Returns a string containing the request list under the form "\t-> request\n..."
|
18
|
-
def RequestList.to_s
|
19
|
-
"\t-> authenticate\n\t-> signup\n\t-> exec\n\t-> insert\n\t-> move\n\t-> backspace\n\t-> delete\n"
|
20
|
-
end
|
21
|
-
|
22
|
-
# Module containing methods for checking requests arguments
|
23
|
-
module Request
|
24
|
-
|
25
|
-
# Checks arguments for authenticate and create the request body
|
26
|
-
#
|
27
|
-
# @param [Hash] request will contain the request (headers and body)
|
28
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
29
|
-
# @option opts [String] :name The user login/email
|
30
|
-
# @option opts [String] :pass The user password
|
31
|
-
def self.authenticate request, opts={}
|
32
|
-
MINEP.notEnoughArgs "AUTHENTICATE" if opts.empty?
|
33
|
-
MINEP.missingArgs "AUTHENTICATE" if opts[:name].nil? or opts[:pass].nil?
|
34
|
-
Utils.makeAuthenticate request, opts
|
35
|
-
end
|
36
|
-
|
37
|
-
# Checks arguments for signup and create the request body
|
38
|
-
# @param [String] website is the url of the website of the new user, passed through opts
|
39
|
-
#
|
40
|
-
# @param [Hash] request will contain the request (headers and body)
|
41
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
42
|
-
# @option opts [String] :name The login of the new user
|
43
|
-
# @option opts [String] :pass The password of the new user
|
44
|
-
# @option opts [String] :email The email of the new user
|
45
|
-
def self.signup request, opts={}
|
46
|
-
MINEP.notEnoughArgs "SIGNUP" if opts.empty?
|
47
|
-
MINEP.missingArgs "SIGNUP" if opts[:name].nil? or opts[:pass].nil? or opts[:email].nil?
|
48
|
-
Utils.makeSignup request, opts
|
49
|
-
end
|
50
|
-
|
51
|
-
# Checks arguments for exec and create the request body
|
52
|
-
#
|
53
|
-
# @param [Hash] request will contain the request (headers and body)
|
54
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
55
|
-
# @option opts [String] :buffer The buffer on which you apply the command
|
56
|
-
# @option opts [String] :command The command to apply
|
57
|
-
# @option opts [Array] :args The (potentially empty) array containing arguments for the command
|
58
|
-
def self.exec request, opts={}
|
59
|
-
MINEP.notEnoughArgs "EXEC" if opts.empty?
|
60
|
-
MINEP.missingArgs "SIGNUP" if opts[:buffer].nil? or opts[:command].nil? or opts[:args].nil?
|
61
|
-
end
|
62
|
-
|
63
|
-
# Checks arguments for insert and create the request body
|
64
|
-
#
|
65
|
-
# @param [Hash] request will contain the request (headers and body)
|
66
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
67
|
-
# @option opts [String] :buffer The buffer on which you insert text
|
68
|
-
# @option opts [String] :text The text to insert
|
69
|
-
def self.insert request, opts={}
|
70
|
-
MINEP.notEnoughArgs "INSERT" if opts.empty?
|
71
|
-
MINEP.missingArgs "INSERT" if opts[:buffer].nil? or opts[:text].nil?
|
72
|
-
end
|
73
|
-
|
74
|
-
# Checks arguments for move and create the request body
|
75
|
-
#
|
76
|
-
# @param [Hash] request will contain the request (headers and body)
|
77
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
78
|
-
# @option opts [String] :buffer The buffer on which you move the cursor
|
79
|
-
# @option opts [Symbol] :direction The direction of the move
|
80
|
-
# @option opts [Integer] :number The range of the move (unused on :origin or :end move)
|
81
|
-
def self.move request, opts={}
|
82
|
-
MINEP.notEnoughArgs "MOVE" if opts.empty?
|
83
|
-
MINEP.missingArgs "MOVE" if opts[:buffer].nil? or opts[:direction].nil? or opts[:number].nil?
|
84
|
-
end
|
85
|
-
|
86
|
-
# Checks arguments for backspace and create the request body
|
87
|
-
#
|
88
|
-
# @param [Hash] request will contain the request (headers and body)
|
89
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
90
|
-
# @option opts [String] :buffer The buffer on which you suppress
|
91
|
-
# @option opts [Integer] :number The range of the suppression
|
92
|
-
def self.backspace request, opts={}
|
93
|
-
MINEP.notEnoughArgs "BACKSPACE" if opts.empty?
|
94
|
-
MINEP.missingArgs "BACKSPACE" if opts[:buffer].nil? or opts[:number].nil?
|
95
|
-
end
|
96
|
-
|
97
|
-
# Checks arguments for delete and create the request body
|
98
|
-
#
|
99
|
-
# @param [Hash] request will contain the request (headers and body)
|
100
|
-
# @param [Hash] opts contains the options and arguments get from MINEP.send
|
101
|
-
# @option opts [String] :buffer The buffer on which you delete
|
102
|
-
# @option opts [Integer] :number The range of the deletion
|
103
|
-
def self.delete request, opts={}
|
104
|
-
MINEP.notEnoughArgs "DELETE" if opts.empty?
|
105
|
-
MINEP.missingArgs "DELETE" if opts[:buffer].nil? or opts[:number].nil?
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
data/lib/minep/utils.rb
DELETED
@@ -1,70 +0,0 @@
|
|
1
|
-
module MINEP
|
2
|
-
module Utils
|
3
|
-
# Make the request (header + body)
|
4
|
-
def self.makeRequest request, type, opts
|
5
|
-
Utils.getHeader request, opts[:host], opts[:port]
|
6
|
-
case type
|
7
|
-
when :authenticate then Request.authenticate request, opts
|
8
|
-
when :signup then Request.signup request, opts
|
9
|
-
when :exec then Request.exec request, opts
|
10
|
-
when :insert then Request.insert request, opts
|
11
|
-
when :move then Request.move request, opts
|
12
|
-
when :backspace then Request.backspace request, opts
|
13
|
-
when :delete then Request.delete request, opts
|
14
|
-
else
|
15
|
-
false
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
# Make the request header
|
20
|
-
def self.getHeader request, host=nil, port=nil
|
21
|
-
host = "localhost" if host.nil?
|
22
|
-
port = 8080 if host.nil?
|
23
|
-
request[:header] = {
|
24
|
-
:head => "POST /mine/protocol/request HTTP/1.1",
|
25
|
-
:host => "#{host}:#{port}",
|
26
|
-
:connection => "Keep-Alive",
|
27
|
-
:contentType => "application/x-www-form-urlencoded",
|
28
|
-
:contentLength => 0
|
29
|
-
}
|
30
|
-
true
|
31
|
-
end
|
32
|
-
|
33
|
-
# Calculate the body length
|
34
|
-
def self.calcLength request
|
35
|
-
rqst = request[:body].keys[0]
|
36
|
-
cL = rqst.size # rqst
|
37
|
-
cL += 3 # ={..}
|
38
|
-
request[:body][rqst].each do |key, value| # ..
|
39
|
-
cL += 6 + key.size + value.size # "key":"value",
|
40
|
-
end
|
41
|
-
cL -= 1 # the loop counts one inexistant ','
|
42
|
-
end
|
43
|
-
|
44
|
-
# Make the body for AUTHENTICATE request
|
45
|
-
def self.makeAuthenticate request, options
|
46
|
-
request[:body] = {
|
47
|
-
:authenticate => {
|
48
|
-
:name => options[:name],
|
49
|
-
:pass => options[:pass]
|
50
|
-
}
|
51
|
-
}
|
52
|
-
request[:header][:contentLength] = calcLength request
|
53
|
-
true
|
54
|
-
end
|
55
|
-
|
56
|
-
# Make the body for SIGNUP request
|
57
|
-
def self.makeSignup request, options
|
58
|
-
request[:body] = {
|
59
|
-
:signup => {
|
60
|
-
:name => options[:name],
|
61
|
-
:pass => options[:pass],
|
62
|
-
:email => options[:email],
|
63
|
-
:website => options[:website].to_s
|
64
|
-
}
|
65
|
-
}
|
66
|
-
request[:header][:contentLength] = calcLength request
|
67
|
-
true
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|