minep-request 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,17 @@
1
+ minep-request
2
+ =============
3
+
4
+ minep-request is a ruby gem allowing to communicate with a [MINE server](http://github.com/Ezveus/Mine)
5
+
6
+ Requests are AUTHENTICATE, SIGNUP, EXEC, INSERT, MOVE, BACKSPACE and DELETE.
7
+
8
+ Arguments required :
9
+ --------------------
10
+
11
+ - AUTHENTICATE : *name* and *pass*
12
+ - SIGNUP : *name*, *pass*, *email* and (optionaly) *website*
13
+ - EXEC : Not implemented yet
14
+ - INSERT : Not implemented yet
15
+ - MOVE : Not implemented yet
16
+ - BACKSPACE : Not implemented yet
17
+ - DELETE : Not implemented yet
data/bin/mine-request ADDED
@@ -0,0 +1,60 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'rubygems'
5
+ require 'minep'
6
+
7
+ request = nil
8
+ options = {}
9
+
10
+ optparse = OptionParser.new do |opts|
11
+ opts.banner = <<-eos
12
+ Usage: mine-request -r REQUEST [OPTION]... REQUEST_ARGUMENTS
13
+ Send a request to a MINE server
14
+
15
+ Valid requests are :
16
+ #{MINEP.requestList}
17
+ eos
18
+
19
+ opts.on("-r", "--request REQUEST",
20
+ MINEP.requestList, "request to send") do |r|
21
+ request = r
22
+ end
23
+
24
+ opts.on "-h", "--host HOST", "server hostname" do |h|
25
+ options[:host] = h
26
+ end
27
+
28
+ opts.on "-p", "--port PORT", Integer, "server port" do |p|
29
+ options[:port] = p
30
+ end
31
+
32
+ opts.on "--help", "displays this screen" do |h|
33
+ puts opts
34
+ exit 1
35
+ end
36
+ end
37
+
38
+ begin
39
+ optparse.parse!
40
+ if request.nil?
41
+ puts "Missing option : request\n"
42
+ puts optparse
43
+ exit 1
44
+ end
45
+ rescue OptionParser::InvalidOption, OptionParser::MissingArgument, OptionParser::InvalidArgument
46
+ puts $!.to_s
47
+ puts optparse
48
+ exit 1
49
+ end
50
+
51
+ ARGV.each do |arg|
52
+ args = arg.split '='
53
+ options[args[0].to_sym] = args[1]
54
+ end
55
+ begin
56
+ MINEP.send request, options
57
+ rescue MINEP::ArgumentsError => e
58
+ $stderr.puts "Error : #{e}"
59
+ exit 1
60
+ end
@@ -1,5 +1,9 @@
1
1
  module MINEP
2
+ # This class extends EM::Connection (the connection class of EventMachine)
2
3
  class Connection < EM::Connection
4
+ # Initialize the object
5
+ #
6
+ # @param [Hash] request is the request, The text request is built from this
3
7
  def initialize request
4
8
  super
5
9
  @request = ["#{request[:header][:head]}\n"]
@@ -22,10 +26,12 @@ module MINEP
22
26
  @request = @request.join
23
27
  end
24
28
 
29
+ # Send the request after the connection initialization
25
30
  def post_init
26
31
  send_data @request
27
32
  end
28
33
 
34
+ # Receive the answer of the server and puts it on standard output
29
35
  def receive_data data
30
36
  puts data
31
37
  end
@@ -0,0 +1,18 @@
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 CHANGED
@@ -1,4 +1,9 @@
1
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
2
7
  def self.send type, opts={}
3
8
  request = {}
4
9
  return false unless Utils.makeRequest request, type, opts
data/lib/minep/request.rb CHANGED
@@ -1,39 +1,108 @@
1
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
2
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
3
31
  def self.authenticate request, opts={}
4
- return false if opts.empty?
5
- return false if opts[:name].nil? or opts[:pass].nil?
32
+ MINEP.notEnoughArgs "AUTHENTICATE" if opts.empty?
33
+ MINEP.missingArgs "AUTHENTICATE" if opts[:name].nil? or opts[:pass].nil?
6
34
  Utils.makeAuthenticate request, opts
7
35
  end
8
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
9
45
  def self.signup request, opts={}
10
- return false if opts.empty?
11
- return false if opts[:name].nil? or opts[:pass].nil? or opts[:email].nil?
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
12
49
  end
13
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
14
58
  def self.exec request, opts={}
15
- return false if opts.empty?
16
- return false if opts[:buffer].nil? or opts[:command].nil? or opts[:args].nil?
59
+ MINEP.notEnoughArgs "EXEC" if opts.empty?
60
+ MINEP.missingArgs "SIGNUP" if opts[:buffer].nil? or opts[:command].nil? or opts[:args].nil?
17
61
  end
18
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
19
69
  def self.insert request, opts={}
20
- return false if opts.empty?
21
- return false if opts[:buffer].nil? or opts[:text].nil?
70
+ MINEP.notEnoughArgs "INSERT" if opts.empty?
71
+ MINEP.missingArgs "INSERT" if opts[:buffer].nil? or opts[:text].nil?
22
72
  end
23
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)
24
81
  def self.move request, opts={}
25
- return false if opts.empty?
26
- return false if opts[:buffer].nil? or opts[:direction].nil? or opts[:number].nil?
82
+ MINEP.notEnoughArgs "MOVE" if opts.empty?
83
+ MINEP.missingArgs "MOVE" if opts[:buffer].nil? or opts[:direction].nil? or opts[:number].nil?
27
84
  end
28
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
29
92
  def self.backspace request, opts={}
30
- return false if opts.empty?
31
- return false if opts[:buffer].nil? or opts[:number].nil?
93
+ MINEP.notEnoughArgs "BACKSPACE" if opts.empty?
94
+ MINEP.missingArgs "BACKSPACE" if opts[:buffer].nil? or opts[:number].nil?
32
95
  end
33
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
34
103
  def self.delete request, opts={}
35
- return false if opts.empty?
36
- return false if opts[:buffer].nil? or opts[:number].nil?
104
+ MINEP.notEnoughArgs "DELETE" if opts.empty?
105
+ MINEP.missingArgs "DELETE" if opts[:buffer].nil? or opts[:number].nil?
37
106
  end
38
107
  end
39
108
  end
data/lib/minep/utils.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module MINEP
2
2
  module Utils
3
+ # Make the request (header + body)
3
4
  def self.makeRequest request, type, opts
4
5
  Utils.getHeader request, opts[:host], opts[:port]
5
6
  case type
@@ -14,7 +15,8 @@ module MINEP
14
15
  false
15
16
  end
16
17
  end
17
-
18
+
19
+ # Make the request header
18
20
  def self.getHeader request, host=nil, port=nil
19
21
  host = "localhost" if host.nil?
20
22
  port = 8080 if host.nil?
@@ -28,6 +30,7 @@ module MINEP
28
30
  true
29
31
  end
30
32
 
33
+ # Calculate the body length
31
34
  def self.calcLength request
32
35
  rqst = request[:body].keys[0]
33
36
  cL = rqst.size # rqst
@@ -38,6 +41,7 @@ module MINEP
38
41
  cL -= 1 # the loop counts one inexistant ','
39
42
  end
40
43
 
44
+ # Make the body for AUTHENTICATE request
41
45
  def self.makeAuthenticate request, options
42
46
  request[:body] = {
43
47
  :authenticate => {
@@ -48,5 +52,19 @@ module MINEP
48
52
  request[:header][:contentLength] = calcLength request
49
53
  true
50
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
51
69
  end
52
70
  end
data/lib/minep.rb CHANGED
@@ -3,7 +3,6 @@ require 'eventmachine'
3
3
  require 'json'
4
4
 
5
5
  # Requiring internal files
6
- require 'minep/minep'
7
- require 'minep/request'
8
- require 'minep/utils'
9
- require 'minep/connection'
6
+ %w[minep request utils connection exceptions].each do |file|
7
+ require "minep/#{file}"
8
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minep-request
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,20 +9,29 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-09 00:00:00.000000000 Z
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: Allows to communicate with a MINE server
14
+ description: ! 'Allows to communicate with a MINE server.
15
+
16
+ Supported requests are the same as the implemented requests in the server : AUTHENTICATE
17
+ and SIGNUP.
18
+
19
+ '
15
20
  email: ciappam@gmail.com
16
- executables: []
21
+ executables:
22
+ - mine-request
17
23
  extensions: []
18
24
  extra_rdoc_files: []
19
25
  files:
20
26
  - lib/minep.rb
21
27
  - lib/minep-request.rb
22
28
  - lib/minep/connection.rb
29
+ - lib/minep/exceptions.rb
23
30
  - lib/minep/minep.rb
24
31
  - lib/minep/request.rb
25
32
  - lib/minep/utils.rb
33
+ - README.md
34
+ - bin/mine-request
26
35
  homepage: https://github.com/Ezveus/minep-request
27
36
  licenses: []
28
37
  post_install_message:
@@ -48,3 +57,4 @@ signing_key:
48
57
  specification_version: 3
49
58
  summary: Allows to communicate with a MINE server
50
59
  test_files: []
60
+ has_rdoc: