sanford-protocol 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -4
- data/lib/sanford-protocol.rb +2 -0
- data/lib/sanford-protocol/request.rb +10 -18
- data/lib/sanford-protocol/response.rb +9 -13
- data/lib/sanford-protocol/version.rb +1 -1
- data/sanford-protocol.gemspec +1 -1
- data/test/unit/request_tests.rb +1 -1
- data/test/unit/response_tests.rb +8 -8
- metadata +15 -15
data/README.md
CHANGED
@@ -46,16 +46,16 @@ Requests are encoded as BSON hashes when transmitted in messages.
|
|
46
46
|
|
47
47
|
## Response
|
48
48
|
|
49
|
-
A response is made up of 2 parts: the status and the
|
49
|
+
A response is made up of 2 parts: the status and the data.
|
50
50
|
|
51
51
|
* **status** - (tuple, required) A code and message describing the result of the service call.
|
52
|
-
* **
|
52
|
+
* **data** - (object, optional) Return value of the service call. This can be any BSON serializable object. Typically won't be set if the request is not successful.
|
53
53
|
|
54
54
|
Responses are encoded as BSON hashes when transmitted in messages.
|
55
55
|
|
56
56
|
```ruby
|
57
57
|
{ 'status': [ 200, 'The request was successful.' ]
|
58
|
-
'
|
58
|
+
'data': true
|
59
59
|
}
|
60
60
|
```
|
61
61
|
|
@@ -104,7 +104,7 @@ Request and response objects have helpers for sending and receiving data using a
|
|
104
104
|
data_hash = server_connection.read
|
105
105
|
incoming_request = Sanford::Protocol::Request.parse(data_hash)
|
106
106
|
# use Response#to_hash to send a response
|
107
|
-
outgoing_response = Sanford::Protocol::Response.new(status,
|
107
|
+
outgoing_response = Sanford::Protocol::Response.new(status, data)
|
108
108
|
server_connection.write(outgoing_response.to_hash)
|
109
109
|
|
110
110
|
# For a client...
|
data/lib/sanford-protocol.rb
CHANGED
@@ -1,34 +1,26 @@
|
|
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
|
-
# version, name and params.
|
4
|
-
# (hashes) with `parse` and `to_hash`. In addition to this, a request has a
|
5
|
-
# `valid?` method, that returns whether it is valid and if it isn't why.
|
3
|
+
# version, name and params.
|
6
4
|
|
7
5
|
module Sanford::Protocol
|
8
6
|
|
9
|
-
class Request
|
7
|
+
class Request < Struct.new(:version, :name, :params)
|
10
8
|
|
11
9
|
def self.parse(body)
|
12
10
|
self.new(body['version'], body['name'], body['params'])
|
13
11
|
end
|
14
12
|
|
15
|
-
attr_reader :version, :name, :params
|
16
|
-
|
17
|
-
def initialize(version, name, params)
|
18
|
-
@version, @name, @params = version, name, params
|
19
|
-
end
|
20
|
-
|
21
13
|
def to_hash
|
22
|
-
{ 'version' =>
|
23
|
-
'name' =>
|
24
|
-
'params' =>
|
14
|
+
{ 'version' => version,
|
15
|
+
'name' => name,
|
16
|
+
'params' => params
|
25
17
|
}
|
26
18
|
end
|
27
19
|
|
28
20
|
def valid?
|
29
|
-
if
|
21
|
+
if !version
|
30
22
|
[ false, "The request doesn't contain a version." ]
|
31
|
-
elsif
|
23
|
+
elsif !name
|
32
24
|
[ false, "The request doesn't contain a name." ]
|
33
25
|
else
|
34
26
|
[ true ]
|
@@ -38,9 +30,9 @@ module Sanford::Protocol
|
|
38
30
|
def inspect
|
39
31
|
reference = '0x0%x' % (self.object_id << 1)
|
40
32
|
"#<#{self.class}:#{reference}"\
|
41
|
-
" @version=#{
|
42
|
-
" @name=#{
|
43
|
-
" @params=#{
|
33
|
+
" @version=#{version.inspect}"\
|
34
|
+
" @name=#{name.inspect}"\
|
35
|
+
" @params=#{params.inspect}>"
|
44
36
|
end
|
45
37
|
|
46
38
|
end
|
@@ -2,41 +2,37 @@ require 'sanford-protocol/response_status'
|
|
2
2
|
|
3
3
|
# The Response class models a specific type of Sanford message body and provides
|
4
4
|
# a defined structure for it. A response requires a message body to contain a
|
5
|
-
# status
|
6
|
-
# methods for working with message bodies (hashes) with `parse` and `to_hash`.
|
5
|
+
# status and some data.
|
7
6
|
|
8
7
|
module Sanford::Protocol
|
9
8
|
|
10
|
-
class Response
|
9
|
+
class Response < Struct.new(:status, :data)
|
11
10
|
|
12
11
|
def self.parse(hash)
|
13
|
-
self.new(hash['status'], hash['
|
12
|
+
self.new(hash['status'], hash['data'])
|
14
13
|
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
def initialize(status, result = nil)
|
19
|
-
@status, @result = build_status(status), result
|
15
|
+
def initialize(status, data=nil)
|
16
|
+
super(build_status(status), data)
|
20
17
|
end
|
21
18
|
|
22
19
|
def to_hash
|
23
|
-
{ 'status'
|
24
|
-
'
|
20
|
+
{ 'status' => [ status.code, status.message ],
|
21
|
+
'data' => data
|
25
22
|
}
|
26
23
|
end
|
27
24
|
|
28
25
|
def inspect
|
29
26
|
reference = '0x0%x' % (self.object_id << 1)
|
30
27
|
"#<#{self.class}:#{reference}"\
|
31
|
-
" @status=#{
|
32
|
-
" @
|
28
|
+
" @status=#{status.inspect}"\
|
29
|
+
" @data=#{data.inspect}>"
|
33
30
|
end
|
34
31
|
|
35
32
|
private
|
36
33
|
|
37
34
|
def build_status(status)
|
38
35
|
return status if status.kind_of?(ResponseStatus)
|
39
|
-
|
40
36
|
ResponseStatus.new(*status)
|
41
37
|
end
|
42
38
|
|
data/sanford-protocol.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |gem|
|
|
10
10
|
gem.email = ["collin.redding@me.com", "kelly@kellyredding.com"]
|
11
11
|
gem.description = "Ruby implementation of Sanford's communication protocol."
|
12
12
|
gem.summary = "Ruby implementation of Sanford's communication protocol."
|
13
|
-
gem.homepage = ""
|
13
|
+
gem.homepage = "https://github.com/redding/sanford-protocol"
|
14
14
|
|
15
15
|
gem.files = `git ls-files`.split($/)
|
16
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
data/test/unit/request_tests.rb
CHANGED
@@ -10,7 +10,7 @@ class Sanford::Protocol::Request
|
|
10
10
|
end
|
11
11
|
subject{ @request }
|
12
12
|
|
13
|
-
should have_instance_methods :
|
13
|
+
should have_instance_methods :version, :name, :params, :to_hash, :valid?
|
14
14
|
should have_class_methods :parse
|
15
15
|
|
16
16
|
should "return an instance of a Sanford::Protocol::Request given a hash using #parse" do
|
data/test/unit/response_tests.rb
CHANGED
@@ -10,28 +10,28 @@ class Sanford::Protocol::Response
|
|
10
10
|
end
|
11
11
|
subject{ @response }
|
12
12
|
|
13
|
-
should have_instance_methods :status, :
|
13
|
+
should have_instance_methods :status, :data, :to_hash
|
14
14
|
should have_class_methods :parse
|
15
15
|
|
16
16
|
should "return an instance of a Sanford::Protocol::Response given a hash using #parse" do
|
17
17
|
# using BSON messages are hashes
|
18
18
|
hash = {
|
19
|
-
'status'
|
20
|
-
'
|
19
|
+
'status' => [ 200, 'OK' ],
|
20
|
+
'data' => 'yes'
|
21
21
|
}
|
22
22
|
request = Sanford::Protocol::Response.parse(hash)
|
23
23
|
|
24
24
|
assert_instance_of Sanford::Protocol::Response, request
|
25
|
-
assert_equal hash['status'].first,
|
26
|
-
assert_equal hash['status'].last,
|
27
|
-
assert_equal hash['
|
25
|
+
assert_equal hash['status'].first, request.status.code
|
26
|
+
assert_equal hash['status'].last, request.status.message
|
27
|
+
assert_equal hash['data'], request.data
|
28
28
|
end
|
29
29
|
|
30
30
|
should "return the request as a hash with #to_hash" do
|
31
31
|
# using BSON messages are hashes
|
32
32
|
expected = {
|
33
|
-
'status'
|
34
|
-
'
|
33
|
+
'status' => [ 672, 'YAR!' ],
|
34
|
+
'data' => { 'something' => true }
|
35
35
|
}
|
36
36
|
|
37
37
|
assert_equal expected, subject.to_hash
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sanford-protocol
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Collin Redding
|
@@ -16,10 +16,10 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2012-11-
|
19
|
+
date: 2012-11-15 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
|
22
|
+
prerelease: false
|
23
23
|
version_requirements: &id001 !ruby/object:Gem::Requirement
|
24
24
|
none: false
|
25
25
|
requirements:
|
@@ -30,11 +30,11 @@ dependencies:
|
|
30
30
|
- 1
|
31
31
|
- 7
|
32
32
|
version: "1.7"
|
33
|
-
type: :runtime
|
34
33
|
requirement: *id001
|
35
|
-
|
34
|
+
name: bson
|
35
|
+
type: :runtime
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
|
37
|
+
prerelease: false
|
38
38
|
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
@@ -45,11 +45,11 @@ dependencies:
|
|
45
45
|
- 0
|
46
46
|
- 8
|
47
47
|
version: "0.8"
|
48
|
-
type: :development
|
49
48
|
requirement: *id002
|
50
|
-
|
49
|
+
name: assert
|
50
|
+
type: :development
|
51
51
|
- !ruby/object:Gem::Dependency
|
52
|
-
|
52
|
+
prerelease: false
|
53
53
|
version_requirements: &id003 !ruby/object:Gem::Requirement
|
54
54
|
none: false
|
55
55
|
requirements:
|
@@ -60,9 +60,9 @@ dependencies:
|
|
60
60
|
- 0
|
61
61
|
- 1
|
62
62
|
version: "0.1"
|
63
|
-
type: :development
|
64
63
|
requirement: *id003
|
65
|
-
|
64
|
+
name: assert-mocha
|
65
|
+
type: :development
|
66
66
|
description: Ruby implementation of Sanford's communication protocol.
|
67
67
|
email:
|
68
68
|
- collin.redding@me.com
|
@@ -98,7 +98,7 @@ files:
|
|
98
98
|
- test/unit/response_status_tests.rb
|
99
99
|
- test/unit/response_tests.rb
|
100
100
|
- test/unit/test_helpers_tests.rb
|
101
|
-
homepage:
|
101
|
+
homepage: https://github.com/redding/sanford-protocol
|
102
102
|
licenses: []
|
103
103
|
|
104
104
|
post_install_message:
|
@@ -127,7 +127,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
127
127
|
requirements: []
|
128
128
|
|
129
129
|
rubyforge_project:
|
130
|
-
rubygems_version: 1.8.
|
130
|
+
rubygems_version: 1.8.15
|
131
131
|
signing_key:
|
132
132
|
specification_version: 3
|
133
133
|
summary: Ruby implementation of Sanford's communication protocol.
|