sanford-protocol 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -82,6 +82,7 @@ This is the list of defined status codes.
82
82
  * `200` - `ok` - The request was successful.
83
83
  * `400` - `bad_request` - The request couldn't be read. This is usually because it was not formed correctly.
84
84
  * `404` - `not_found` - The server couldn't find something requested.
85
+ * `408` - `timeout` - A client connected but didn't write a request before the server timeod out waiting for one.
85
86
  * `500` - `error` - The server errored responding to the request.
86
87
 
87
88
  In addition to these, a service can return custom status codes, but they should use a number greater than or equal to `600` to avoid collisions with Sanford's defined status codes.
@@ -33,6 +33,10 @@ module Sanford::Protocol
33
33
  @socket.write(msg_version, size, body)
34
34
  end
35
35
 
36
+ def close
37
+ @socket.close
38
+ end
39
+
36
40
  private
37
41
 
38
42
  def wait_for_data(timeout)
@@ -59,6 +63,10 @@ module Sanford::Protocol
59
63
  def write(*binary_strings)
60
64
  tcp_socket.send(binary_strings.join, 0)
61
65
  end
66
+
67
+ def close
68
+ tcp_socket.close rescue false
69
+ end
62
70
  end
63
71
 
64
72
  class TimeoutError < RuntimeError
@@ -4,12 +4,21 @@
4
4
 
5
5
  module Sanford::Protocol
6
6
 
7
- class Request < Struct.new(:version, :name, :params)
7
+ BadRequestError = Class.new(RuntimeError)
8
+
9
+ class Request
8
10
 
9
11
  def self.parse(body)
10
12
  self.new(body['version'], body['name'], body['params'])
11
13
  end
12
14
 
15
+ attr_reader :version, :name, :params
16
+
17
+ def initialize(version, name, params)
18
+ self.validate!(version, name, params)
19
+ @version, @name, @params = version, name, self.stringify(params)
20
+ end
21
+
13
22
  def to_hash
14
23
  { 'version' => version,
15
24
  'name' => name,
@@ -19,18 +28,6 @@ module Sanford::Protocol
19
28
 
20
29
  def to_s; "[#{version}] #{name}"; end
21
30
 
22
- def valid?
23
- if !version
24
- [ false, "The request doesn't contain a version." ]
25
- elsif !name
26
- [ false, "The request doesn't contain a name." ]
27
- elsif !params.kind_of?(Hash)
28
- [ false, "The request's params are not a valid BSON document." ]
29
- else
30
- [ true ]
31
- end
32
- end
33
-
34
31
  def inspect
35
32
  reference = '0x0%x' % (self.object_id << 1)
36
33
  "#<#{self.class}:#{reference}"\
@@ -39,6 +36,30 @@ module Sanford::Protocol
39
36
  " @params=#{params.inspect}>"
40
37
  end
41
38
 
39
+ protected
40
+
41
+ def validate!(version, name, params)
42
+ problem = if !version
43
+ "The request doesn't contain a version."
44
+ elsif !name
45
+ "The request doesn't contain a name."
46
+ elsif !params.kind_of?(Hash)
47
+ "The request's params are not a valid BSON document."
48
+ end
49
+ raise(BadRequestError, problem) if problem
50
+ end
51
+
52
+ def stringify(object)
53
+ case(object)
54
+ when Hash
55
+ object.inject({}){|h, (k, v)| h.merge({ k.to_s => self.stringify(v) }) }
56
+ when Array
57
+ object.map{|item| self.stringify(item) }
58
+ else
59
+ object
60
+ end
61
+ end
62
+
42
63
  end
43
64
 
44
65
  end
@@ -24,6 +24,7 @@ module Sanford::Protocol
24
24
  'ok' => 200,
25
25
  'bad_request' => 400,
26
26
  'not_found' => 404,
27
+ 'timeout' => 408,
27
28
  'error' => 500
28
29
  }.freeze
29
30
 
@@ -47,5 +47,13 @@ module Sanford::Protocol::Test
47
47
  @out << bytes
48
48
  end
49
49
 
50
+ def close
51
+ @closed = true
52
+ end
53
+
54
+ def closed?
55
+ !!@closed
56
+ end
57
+
50
58
  end
51
59
  end
@@ -1,5 +1,5 @@
1
1
  module Sanford
2
2
  module Protocol
3
- GEM_VERSION = "0.4.1"
3
+ GEM_VERSION = "0.5.0"
4
4
  end
5
5
  end
@@ -19,6 +19,6 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency("bson", ["~>1.7"])
21
21
 
22
- gem.add_development_dependency("assert", ["~> 0.8"])
23
- gem.add_development_dependency("assert-mocha", ["~> 0.1"])
22
+ gem.add_development_dependency("assert", ["~> 1.0"])
23
+ gem.add_development_dependency("assert-mocha", ["~> 1.0"])
24
24
  end
@@ -12,7 +12,7 @@ class Sanford::Protocol::Connection
12
12
  end
13
13
  subject{ @connection }
14
14
 
15
- should have_instance_methods :read, :write
15
+ should have_instance_methods :read, :write, :close
16
16
 
17
17
  should "read messages off the socket with #read" do
18
18
  assert_equal @data, subject.read
@@ -22,6 +22,11 @@ class Sanford::Protocol::Connection
22
22
  subject.write(@data)
23
23
  assert_equal @msg, @socket.out
24
24
  end
25
+
26
+ should "close the socket with #close" do
27
+ subject.close
28
+ assert @socket.closed?
29
+ end
25
30
  end
26
31
 
27
32
  class TimeoutTests < BaseTests
@@ -10,13 +10,22 @@ class Sanford::Protocol::Request
10
10
  end
11
11
  subject{ @request }
12
12
 
13
- should have_instance_methods :version, :name, :params, :to_hash, :valid?
13
+ should have_instance_methods :version, :name, :params, :to_hash
14
14
  should have_class_methods :parse
15
15
 
16
16
  should "return it's version and name with #to_s" do
17
17
  assert_equal "[#{subject.version}] #{subject.name}", subject.to_s
18
18
  end
19
19
 
20
+ should "stringify params keys" do
21
+ request = Sanford::Protocol::Request.new('v1', 'service', {
22
+ 1 => 1,
23
+ :symbol => :symbol
24
+ })
25
+ expected = { "1" => 1, "symbol" => :symbol }
26
+ assert_equal expected, request.params
27
+ end
28
+
20
29
  should "return an instance of a Sanford::Protocol::Request given a hash using #parse" do
21
30
  # using BSON messages are hashes
22
31
  hash = {
@@ -45,38 +54,29 @@ class Sanford::Protocol::Request
45
54
  end
46
55
 
47
56
  class ValidTests < BaseTests
48
- desc "valid?"
49
-
50
- should "return true and no message with a valid request" do
51
- request = Sanford::Protocol::Request.new('v1', 'name', {})
52
- is_valid, message = request.valid?
53
57
 
54
- assert_equal true, is_valid
55
- assert_equal nil, message
58
+ should "not raise an exception with valid request args" do
59
+ assert_nothing_raised do
60
+ Sanford::Protocol::Request.new('v1', 'name', {})
61
+ end
56
62
  end
57
63
 
58
- should "return false and a message when there isn't a name" do
59
- request = Sanford::Protocol::Request.new('v1', nil, {})
60
- is_valid, message = request.valid?
61
-
62
- assert_equal false, is_valid
63
- assert_equal "The request doesn't contain a name.", message
64
+ should "raise an exception when there isn't a name arg" do
65
+ assert_raises(Sanford::Protocol::BadRequestError) do
66
+ Sanford::Protocol::Request.new('v1', nil, {})
67
+ end
64
68
  end
65
69
 
66
70
  should "return false and a message when there isn't a version" do
67
- request = Sanford::Protocol::Request.new(nil, 'name', {})
68
- is_valid, message = request.valid?
69
-
70
- assert_equal false, is_valid
71
- assert_equal "The request doesn't contain a version.", message
71
+ assert_raises(Sanford::Protocol::BadRequestError) do
72
+ Sanford::Protocol::Request.new(nil, 'name', {})
73
+ end
72
74
  end
73
75
 
74
76
  should "return false and a message when the params are not a Hash" do
75
- request = Sanford::Protocol::Request.new('v1', 'name', true)
76
- is_valid, message = request.valid?
77
-
78
- assert_equal false, is_valid
79
- assert_equal "The request's params are not a valid BSON document.", message
77
+ assert_raises(Sanford::Protocol::BadRequestError) do
78
+ Sanford::Protocol::Request.new('v1', 'name', true)
79
+ end
80
80
  end
81
81
  end
82
82
 
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: 13
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 1
10
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Collin Redding
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-11-28 00:00:00 Z
19
+ date: 2012-11-30 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  prerelease: false
@@ -40,11 +40,11 @@ dependencies:
40
40
  requirements:
41
41
  - - ~>
42
42
  - !ruby/object:Gem::Version
43
- hash: 27
43
+ hash: 15
44
44
  segments:
45
+ - 1
45
46
  - 0
46
- - 8
47
- version: "0.8"
47
+ version: "1.0"
48
48
  requirement: *id002
49
49
  name: assert
50
50
  type: :development
@@ -55,11 +55,11 @@ dependencies:
55
55
  requirements:
56
56
  - - ~>
57
57
  - !ruby/object:Gem::Version
58
- hash: 9
58
+ hash: 15
59
59
  segments:
60
- - 0
61
60
  - 1
62
- version: "0.1"
61
+ - 0
62
+ version: "1.0"
63
63
  requirement: *id003
64
64
  name: assert-mocha
65
65
  type: :development