sanford-protocol 0.5.6 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -11
- data/lib/sanford-protocol/request.rb +11 -15
- data/lib/sanford-protocol/version.rb +1 -1
- data/lib/sanford-protocol.rb +1 -1
- data/sanford-protocol.gemspec +1 -1
- data/test/helper.rb +1 -1
- data/test/unit/protocol_tests.rb +1 -1
- data/test/unit/request_tests.rb +14 -24
- metadata +12 -12
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Ruby implementation of Sanford TCP communication protocol.
|
|
4
4
|
|
5
5
|
## The Protocol
|
6
6
|
|
7
|
-
**Version**: `
|
7
|
+
**Version**: `2`
|
8
8
|
|
9
9
|
Sanford communicates using binary encoded messages. Sanford messages are two headers and a body:
|
10
10
|
|
@@ -29,25 +29,22 @@ The Body is the content of the message. It is a [BSON](http://bsonspec.org/) en
|
|
29
29
|
|
30
30
|
## Request
|
31
31
|
|
32
|
-
A request is made up of
|
32
|
+
A request is made up of 2 required parts: the name, and the params.
|
33
33
|
|
34
|
-
* **
|
35
|
-
* **
|
36
|
-
* **params** - (document) data for the service call - must be a BSON document (ruby Hash, python dict, Javascript Object).
|
34
|
+
* **name** - (string) name of the requested API service.
|
35
|
+
* **params** - (document) data for the service call - must be a BSON document (ruby Hash, python dict, Javascript Object).
|
37
36
|
|
38
37
|
Requests are encoded as BSON hashes when transmitted in messages.
|
39
38
|
|
40
39
|
```ruby
|
41
|
-
{ '
|
42
|
-
'
|
43
|
-
'params' => { 'key' => 'value' }
|
40
|
+
{ 'name' => 'some_service',
|
41
|
+
'params' => { 'key' => 'value' }
|
44
42
|
}
|
45
43
|
|
46
44
|
request = Sanford::Protocol::Request.parse(a_bson_request_hash)
|
47
|
-
request.version #=> "v1"
|
48
45
|
request.name #=> "some_service"
|
49
46
|
request.params #=> { 'key' => 'value' }
|
50
|
-
request.to_s #=> "
|
47
|
+
request.to_s #=> "some_service"
|
51
48
|
```
|
52
49
|
|
53
50
|
## Response
|
@@ -138,7 +135,7 @@ server_connection.write(outgoing_response.to_hash)
|
|
138
135
|
|
139
136
|
# For a client...
|
140
137
|
# use Request#to_hash to send a request
|
141
|
-
outgoing_request = Sanford::Protocol::Request.new(name,
|
138
|
+
outgoing_request = Sanford::Protocol::Request.new(name, params)
|
142
139
|
client_connection.write(outgoing_request.to_hash)
|
143
140
|
# use Response#parse to build an incoming response
|
144
141
|
data_hash = client_connection.read
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# The Request class models a specific type of Sanford message body and provides
|
2
2
|
# a defined structure for it. A request requires a message body to contain a
|
3
|
-
#
|
3
|
+
# name and params.
|
4
4
|
|
5
5
|
module Sanford; end
|
6
6
|
module Sanford::Protocol
|
@@ -10,39 +10,35 @@ module Sanford::Protocol
|
|
10
10
|
class Request
|
11
11
|
|
12
12
|
def self.parse(body)
|
13
|
-
self.new(body['
|
13
|
+
self.new(body['name'], body['params'])
|
14
14
|
end
|
15
15
|
|
16
|
-
attr_reader :
|
16
|
+
attr_reader :name, :params
|
17
17
|
|
18
|
-
def initialize(
|
19
|
-
self.validate!(
|
20
|
-
@
|
18
|
+
def initialize(name, params)
|
19
|
+
self.validate!(name, params)
|
20
|
+
@name, @params = name, params
|
21
21
|
end
|
22
22
|
|
23
23
|
def to_hash
|
24
|
-
{ '
|
25
|
-
'
|
26
|
-
'params' => self.stringify(params)
|
24
|
+
{ 'name' => name,
|
25
|
+
'params' => self.stringify(params)
|
27
26
|
}
|
28
27
|
end
|
29
28
|
|
30
|
-
def to_s;
|
29
|
+
def to_s; name; end
|
31
30
|
|
32
31
|
def inspect
|
33
32
|
reference = '0x0%x' % (self.object_id << 1)
|
34
33
|
"#<#{self.class}:#{reference}"\
|
35
|
-
" @version=#{version.inspect}"\
|
36
34
|
" @name=#{name.inspect}"\
|
37
35
|
" @params=#{params.inspect}>"
|
38
36
|
end
|
39
37
|
|
40
38
|
protected
|
41
39
|
|
42
|
-
def validate!(
|
43
|
-
problem = if !
|
44
|
-
"The request doesn't contain a version."
|
45
|
-
elsif !name
|
40
|
+
def validate!(name, params)
|
41
|
+
problem = if !name
|
46
42
|
"The request doesn't contain a name."
|
47
43
|
elsif !params.kind_of?(Hash)
|
48
44
|
"The request's params are not a valid BSON document."
|
data/lib/sanford-protocol.rb
CHANGED
@@ -14,7 +14,7 @@ module Sanford
|
|
14
14
|
# README needs to be updated to display the current version and needs to
|
15
15
|
# describe everything in this file.
|
16
16
|
|
17
|
-
VERSION =
|
17
|
+
VERSION = 2
|
18
18
|
|
19
19
|
# The message version is the 1B encoding of the `VERSION` above. It is
|
20
20
|
# encoded using Array#pack 'C' (8-bit unsigned integer). The max value it
|
data/sanford-protocol.gemspec
CHANGED
data/test/helper.rb
CHANGED
@@ -25,7 +25,7 @@ class Assert::Context
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def setup_some_request_data
|
28
|
-
@request_params = ['
|
28
|
+
@request_params = ['a_service', {:some => 'data'}]
|
29
29
|
@request = Sanford::Protocol::Request.new(*@request_params)
|
30
30
|
setup_some_msg_data(@request.to_hash)
|
31
31
|
end
|
data/test/unit/protocol_tests.rb
CHANGED
@@ -13,7 +13,7 @@ module Sanford::Protocol
|
|
13
13
|
should have_instance_methods :msg_version, :msg_size, :msg_body
|
14
14
|
|
15
15
|
should "define the protocol version" do
|
16
|
-
assert_equal
|
16
|
+
assert_equal 2, subject::VERSION
|
17
17
|
end
|
18
18
|
|
19
19
|
should "encode the protocol version to a 1B binary string" do
|
data/test/unit/request_tests.rb
CHANGED
@@ -6,44 +6,40 @@ class Sanford::Protocol::Request
|
|
6
6
|
class BaseTests < Assert::Context
|
7
7
|
desc "Sanford::Protocol::Request"
|
8
8
|
setup do
|
9
|
-
@request = Sanford::Protocol::Request.new('
|
9
|
+
@request = Sanford::Protocol::Request.new('some_service', { 'key' => 'value' })
|
10
10
|
end
|
11
11
|
subject{ @request }
|
12
12
|
|
13
|
-
should have_instance_methods :
|
13
|
+
should have_instance_methods :name, :params, :to_hash
|
14
14
|
should have_class_methods :parse
|
15
15
|
|
16
|
-
should "return it's
|
17
|
-
assert_equal
|
16
|
+
should "return it's name with #to_s" do
|
17
|
+
assert_equal subject.name, subject.to_s
|
18
18
|
end
|
19
19
|
|
20
20
|
should "return an instance of a Sanford::Protocol::Request given a hash using #parse" do
|
21
21
|
# using BSON messages are hashes
|
22
22
|
hash = {
|
23
|
-
'name'
|
24
|
-
'
|
25
|
-
'params' => { 'service_params' => 'yes' }
|
23
|
+
'name' => 'service_name',
|
24
|
+
'params' => { 'service_params' => 'yes' }
|
26
25
|
}
|
27
26
|
request = Sanford::Protocol::Request.parse(hash)
|
28
27
|
|
29
28
|
assert_instance_of Sanford::Protocol::Request, request
|
30
|
-
assert_equal hash['name'],
|
31
|
-
assert_equal hash['
|
32
|
-
assert_equal hash['params'], request.params
|
29
|
+
assert_equal hash['name'], request.name
|
30
|
+
assert_equal hash['params'], request.params
|
33
31
|
end
|
34
32
|
|
35
33
|
should "return the request as a hash with stringified params with #to_hash" do
|
36
34
|
# using BSON, messages are hashes
|
37
|
-
request = Sanford::Protocol::Request.new('
|
35
|
+
request = Sanford::Protocol::Request.new('service', {
|
38
36
|
1 => 1,
|
39
37
|
:symbol => :symbol
|
40
38
|
})
|
41
39
|
expected = {
|
42
|
-
'
|
43
|
-
'
|
44
|
-
'params' => { '1' => 1, 'symbol' => :symbol }
|
40
|
+
'name' => 'service',
|
41
|
+
'params' => { '1' => 1, 'symbol' => :symbol }
|
45
42
|
}
|
46
|
-
|
47
43
|
assert_equal expected, request.to_hash
|
48
44
|
end
|
49
45
|
|
@@ -53,25 +49,19 @@ class Sanford::Protocol::Request
|
|
53
49
|
|
54
50
|
should "not raise an exception with valid request args" do
|
55
51
|
assert_nothing_raised do
|
56
|
-
Sanford::Protocol::Request.new('
|
52
|
+
Sanford::Protocol::Request.new('name', {})
|
57
53
|
end
|
58
54
|
end
|
59
55
|
|
60
56
|
should "raise an exception when there isn't a name arg" do
|
61
57
|
assert_raises(Sanford::Protocol::BadRequestError) do
|
62
|
-
Sanford::Protocol::Request.new(
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
should "return false and a message when there isn't a version" do
|
67
|
-
assert_raises(Sanford::Protocol::BadRequestError) do
|
68
|
-
Sanford::Protocol::Request.new(nil, 'name', {})
|
58
|
+
Sanford::Protocol::Request.new(nil, {})
|
69
59
|
end
|
70
60
|
end
|
71
61
|
|
72
62
|
should "return false and a message when the params are not a Hash" do
|
73
63
|
assert_raises(Sanford::Protocol::BadRequestError) do
|
74
|
-
Sanford::Protocol::Request.new('
|
64
|
+
Sanford::Protocol::Request.new('name', true)
|
75
65
|
end
|
76
66
|
end
|
77
67
|
end
|
metadata
CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
- 5
|
9
8
|
- 6
|
10
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.6.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Collin Redding
|
@@ -16,10 +16,9 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2013-
|
19
|
+
date: 2013-10-14 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
prerelease: false
|
23
22
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
23
|
none: false
|
25
24
|
requirements:
|
@@ -30,26 +29,26 @@ dependencies:
|
|
30
29
|
- 1
|
31
30
|
- 7
|
32
31
|
version: "1.7"
|
32
|
+
type: :runtime
|
33
33
|
requirement: *id001
|
34
|
+
prerelease: false
|
34
35
|
name: bson
|
35
|
-
type: :runtime
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
prerelease: false
|
38
37
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
38
|
none: false
|
40
39
|
requirements:
|
41
40
|
- - ~>
|
42
41
|
- !ruby/object:Gem::Version
|
43
|
-
hash:
|
42
|
+
hash: 5
|
44
43
|
segments:
|
45
44
|
- 2
|
46
|
-
-
|
47
|
-
version: "2.
|
45
|
+
- 3
|
46
|
+
version: "2.3"
|
47
|
+
type: :development
|
48
48
|
requirement: *id002
|
49
|
+
prerelease: false
|
49
50
|
name: assert
|
50
|
-
type: :development
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
-
prerelease: false
|
53
52
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
53
|
none: false
|
55
54
|
requirements:
|
@@ -60,9 +59,10 @@ dependencies:
|
|
60
59
|
- 1
|
61
60
|
- 0
|
62
61
|
version: "1.0"
|
62
|
+
type: :development
|
63
63
|
requirement: *id003
|
64
|
+
prerelease: false
|
64
65
|
name: assert-mocha
|
65
|
-
type: :development
|
66
66
|
description: Ruby implementation of the Sanford TCP communication protocol.
|
67
67
|
email:
|
68
68
|
- collin.redding@me.com
|