sanford-protocol 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,8 @@
1
- # The Request class models a specific type of Sanford message body and provides
2
- # a defined structure for it. A request requires a message body to contain a
3
- # name and params.
1
+ require 'sanford-protocol'
4
2
 
5
3
  module Sanford; end
6
4
  module Sanford::Protocol
7
5
 
8
- BadRequestError = Class.new(RuntimeError)
9
-
10
6
  class Request
11
7
 
12
8
  def self.parse(body)
@@ -17,12 +13,13 @@ module Sanford::Protocol
17
13
 
18
14
  def initialize(name, params)
19
15
  self.validate!(name, params)
20
- @name, @params = name.to_s, params
16
+ @name = name.to_s
17
+ @params = params
21
18
  end
22
19
 
23
20
  def to_hash
24
21
  { 'name' => name,
25
- 'params' => self.stringify(params)
22
+ 'params' => Sanford::Protocol::StringifyParams.new(params)
26
23
  }
27
24
  end
28
25
 
@@ -49,21 +46,12 @@ module Sanford::Protocol
49
46
  problem = if !name
50
47
  "The request doesn't contain a name."
51
48
  elsif !params.kind_of?(::Hash)
52
- "The request's params are not a valid BSON document."
49
+ "The request's params are not valid."
53
50
  end
54
- raise(BadRequestError, problem) if problem
51
+ raise(InvalidError, problem) if problem
55
52
  end
56
53
 
57
- def stringify(object)
58
- case(object)
59
- when Hash
60
- object.inject({}){|h, (k, v)| h.merge({ k.to_s => self.stringify(v) }) }
61
- when Array
62
- object.map{|item| self.stringify(item) }
63
- else
64
- object
65
- end
66
- end
54
+ InvalidError = Class.new(ArgumentError)
67
55
 
68
56
  end
69
57
 
@@ -1,5 +1,5 @@
1
1
  module Sanford
2
2
  module Protocol
3
- GEM_VERSION = "0.10.0"
3
+ GEM_VERSION = "0.11.0"
4
4
  end
5
5
  end
@@ -49,5 +49,18 @@ module Sanford
49
49
  def decode(binary); ::BSON.deserialize(binary); end
50
50
  end
51
51
 
52
+ module StringifyParams
53
+ def self.new(object)
54
+ case(object)
55
+ when Hash
56
+ object.inject({}){ |h, (k, v)| h.merge(k.to_s => self.new(v)) }
57
+ when Array
58
+ object.map{ |item| self.new(item) }
59
+ else
60
+ object
61
+ end
62
+ end
63
+ end
64
+
52
65
  end
53
66
  end
@@ -6,85 +6,76 @@ class Sanford::Protocol::Request
6
6
  class UnitTests < Assert::Context
7
7
  desc "Sanford::Protocol::Request"
8
8
  setup do
9
- @request = Sanford::Protocol::Request.new('some_service', { 'key' => 'value' })
9
+ @name = Factory.string
10
+ @params = { Factory.string => Factory.string }
11
+ @request_class = Sanford::Protocol::Request
12
+ end
13
+ subject{ @request_class }
14
+
15
+ should have_imeths :parse
16
+
17
+ should "parse a request from a body hash" do
18
+ request = @request_class.parse({
19
+ 'name' => @name,
20
+ 'params' => @params
21
+ })
22
+
23
+ assert_instance_of @request_class, request
24
+ assert_equal @name, request.name
25
+ assert_equal @params, request.params
26
+ end
27
+
28
+ end
29
+
30
+ class InitTests < UnitTests
31
+ desc "when init"
32
+ setup do
33
+ @request = @request_class.new(@name, @params)
10
34
  end
11
35
  subject{ @request }
12
36
 
13
37
  should have_imeths :name, :params, :to_hash
14
- should have_cmeths :parse
15
38
 
16
39
  should "know its name and params" do
17
- assert_equal 'some_service', subject.name
18
- assert_equal({ 'key' => 'value' }, subject.params)
40
+ assert_equal @name, subject.name
41
+ assert_equal @params, subject.params
19
42
  end
20
43
 
21
44
  should "force string request names" do
22
- request = Sanford::Protocol::Request.new(:symbol_service_name, {})
23
- assert_equal 'symbol_service_name', request.name
45
+ request = @request_class.new(@name.to_sym, {})
46
+ assert_equal @name, request.name
24
47
  end
25
48
 
26
- should "return it's name with #to_s" do
49
+ should "return its name using `to_s`" do
27
50
  assert_equal subject.name, subject.to_s
28
51
  end
29
52
 
30
- should "parse requests given a body hash" do
31
- # using BSON messages are hashes
32
- hash = {
33
- 'name' => 'service_name',
34
- 'params' => { 'service_params' => 'yes' }
53
+ should "return the request as a hash using `to_hash`" do
54
+ @params[Factory.integer] = Factory.integer
55
+ @params[Factory.string.to_sym] = Factory.string.to_sym
56
+ request = @request_class.new(@name, @params)
57
+ exp = {
58
+ 'name' => @name,
59
+ 'params' => Sanford::Protocol::StringifyParams.new(@params)
35
60
  }
36
- request = Sanford::Protocol::Request.parse(hash)
37
-
38
- assert_instance_of Sanford::Protocol::Request, request
39
- assert_equal hash['name'], request.name
40
- assert_equal hash['params'], request.params
41
- end
42
-
43
- should "return the request as a hash with stringified params with #to_hash" do
44
- # using BSON, messages are hashes
45
- request = Sanford::Protocol::Request.new('service', {
46
- 1 => 1,
47
- :symbol => :symbol
48
- })
49
- expected = {
50
- 'name' => 'service',
51
- 'params' => { '1' => 1, 'symbol' => :symbol }
52
- }
53
- assert_equal expected, request.to_hash
61
+ assert_equal exp, request.to_hash
54
62
  end
55
63
 
56
64
  should "be comparable" do
57
- match_request = Sanford::Protocol::Request.new(
58
- subject.name.dup,
59
- subject.params.dup
60
- )
61
- assert_equal match_request, subject
62
-
63
- not_match_request = Sanford::Protocol::Request.new('other', {})
64
- assert_not_equal not_match_request, subject
65
- end
66
-
67
- end
65
+ matching = @request_class.new(@name, @params)
66
+ assert_equal matching, subject
68
67
 
69
- class ValidTests < UnitTests
70
-
71
- should "not raise an exception with valid request args" do
72
- assert_nothing_raised do
73
- Sanford::Protocol::Request.new('name', {})
74
- end
68
+ non_matching = @request_class.new(Factory.string, @params)
69
+ assert_not_equal non_matching, subject
70
+ non_matching = @request_class.new(@name, Factory.string => Factory.string)
71
+ assert_not_equal non_matching, subject
75
72
  end
76
73
 
77
- should "raise an exception when there isn't a name arg" do
78
- assert_raises(Sanford::Protocol::BadRequestError) do
79
- Sanford::Protocol::Request.new(nil, {})
80
- end
74
+ should "raise an error when given invalid attributes" do
75
+ assert_raises(InvalidError){ @request_class.new(nil, @params) }
76
+ assert_raises(InvalidError){ @request_class.new(@name, Factory.string) }
81
77
  end
82
78
 
83
- should "return false and a message when the params are not a Hash" do
84
- assert_raises(Sanford::Protocol::BadRequestError) do
85
- Sanford::Protocol::Request.new('name', true)
86
- end
87
- end
88
79
  end
89
80
 
90
81
  end
@@ -20,27 +20,27 @@ module Sanford::Protocol
20
20
  should "encode the protocol version to a 1B binary string" do
21
21
  assert_equal 1, subject.msg_version.bytesize
22
22
 
23
- expected = [ subject::VERSION ].pack('C')
24
- assert_equal expected, subject.msg_version
23
+ exp = [subject::VERSION].pack('C')
24
+ assert_equal exp, subject.msg_version
25
25
  end
26
26
 
27
27
  should "encode/decode the size to/from a 4B binary string" do
28
28
  assert_equal 4, subject.msg_size.bytes
29
29
 
30
30
  size = (2 ** 32) - 1 # the max number it supports
31
- binary = [ size ].pack('N')
31
+ binary = [size].pack('N')
32
32
  assert_equal binary, subject.msg_size.encode(size)
33
33
  assert_equal size, subject.msg_size.decode(binary)
34
34
  end
35
35
 
36
36
  should "encode the body to BSON" do
37
37
  data = {
38
- 'string' => 'test',
39
- 'int' => 1,
40
- 'float' => 2.1,
41
- 'boolean' => true,
42
- 'array' => [ 1, 2, 3 ],
43
- 'hash' => { 'something' => 'else' }
38
+ 'string' => Factory.string,
39
+ 'int' => Factory.integer,
40
+ 'float' => Factory.float,
41
+ 'boolean' => Factory.boolean,
42
+ 'array' => Factory.integer(3).times.map{ Factory.integer },
43
+ 'hash' => { Factory.string => Factory.string }
44
44
  }
45
45
  binary = ::BSON.serialize(data).to_s
46
46
 
@@ -49,4 +49,28 @@ module Sanford::Protocol
49
49
  end
50
50
 
51
51
  end
52
+
53
+ class StringifyParamsTests < UnitTests
54
+ desc "StringifyParams"
55
+ subject{ StringifyParams }
56
+
57
+ should have_imeths :new
58
+
59
+ should "convert all hash keys to strings" do
60
+ key, value = Factory.string.to_sym, Factory.string
61
+ result = subject.new({
62
+ key => value,
63
+ :hash => { key => [value] },
64
+ :array => [{ key => value }]
65
+ })
66
+ exp = {
67
+ key.to_s => value,
68
+ 'hash' => { key.to_s => [value] },
69
+ 'array' => [{ key.to_s => value }]
70
+ }
71
+ assert_equal exp, result
72
+ end
73
+
74
+ end
75
+
52
76
  end
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: 55
4
+ hash: 51
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 10
8
+ - 11
9
9
  - 0
10
- version: 0.10.0
10
+ version: 0.11.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: 2014-09-26 00:00:00 Z
19
+ date: 2015-06-19 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  requirement: &id001 !ruby/object:Gem::Requirement
@@ -37,9 +37,9 @@ dependencies:
37
37
  - 10
38
38
  - 0
39
39
  version: 1.10.0
40
- version_requirements: *id001
41
40
  type: :runtime
42
41
  name: bson
42
+ version_requirements: *id001
43
43
  prerelease: false
44
44
  - !ruby/object:Gem::Dependency
45
45
  requirement: &id002 !ruby/object:Gem::Requirement
@@ -52,9 +52,9 @@ dependencies:
52
52
  - 2
53
53
  - 11
54
54
  version: "2.11"
55
- version_requirements: *id002
56
55
  type: :development
57
56
  name: assert
57
+ version_requirements: *id002
58
58
  prerelease: false
59
59
  description: Ruby implementation of the Sanford TCP communication protocol.
60
60
  email:
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  requirements: []
124
124
 
125
125
  rubyforge_project:
126
- rubygems_version: 1.8.29
126
+ rubygems_version: 1.8.25
127
127
  signing_key:
128
128
  specification_version: 3
129
129
  summary: Ruby implementation of the Sanford TCP communication protocol.