sanford-protocol 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ data.tar.gz: afef8536266febd04c1c5bc749538e769482724f
4
+ metadata.gz: d29e43e064234aaae4527b789973a954214b87c5
5
+ SHA512:
6
+ data.tar.gz: 1e27916f51b006431fae39664ce62e40fccf99ca320573b365d78b717b943370be5b326ba947f74bc6044092284ef6b63171f1371805f4a6dcad691091b33551
7
+ metadata.gz: dbc5915a634bd1313d5ed58334fce8ce38f1c409dc46e5ce043ed3f091a90fc415ca7cd3f32cf6e073b2ec9f2db548c4f8bd4195bec8b207eb51ed86e3174c27
data/Gemfile CHANGED
@@ -2,7 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake'
6
5
  gem 'pry', "~> 0.9.0"
7
-
8
6
  gem 'bson_ext', '~>1.7'
File without changes
data/README.md CHANGED
@@ -76,11 +76,12 @@ response.data #=> true
76
76
 
77
77
  This is the list of defined status codes.
78
78
 
79
- * `200` - `ok` - The request was successful.
80
- * `400` - `bad_request` - The request couldn't be read. This is usually because it was not formed correctly.
81
- * `404` - `not_found` - The server couldn't find something requested.
82
- * `408` - `timeout` - A client connected but didn't write a request before the server timeod out waiting for one.
83
- * `500` - `error` - The server errored responding to the request.
79
+ * `200` - `OK`- The request was successful.
80
+ * `400` - `BAD REQUEST` - The request couldn't be read. This is usually because it was not formed correctly.
81
+ * `404` - `NOT FOUND` - The server couldn't find something requested.
82
+ * `408` - `TIMEOUT` - A client connected but didn't write a request before the server timeod out waiting for one.
83
+ * `422` - `INVALID` - The request was sent with invalid params.
84
+ * `500` - `ERROR` - The server errored responding to the request.
84
85
 
85
86
  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.
86
87
 
@@ -21,7 +21,7 @@ module Sanford::Protocol
21
21
  # | msg version | msg body size | msg body |
22
22
  # |-----------------|-----------------|----------------------|
23
23
 
24
- def read(timeout=nil)
24
+ def read(timeout = nil)
25
25
  wait_for_data(timeout) if timeout
26
26
  MsgVersion.new{ @socket.read msg_version.bytesize }.validate!
27
27
  size = MsgSize.new{ @socket.decode msg_size, msg_size.bytes }.validate!.value
@@ -35,7 +35,7 @@ module Sanford::Protocol
35
35
  @socket.write(msg_version, size, body)
36
36
  end
37
37
 
38
- def peek(timeout=nil)
38
+ def peek(timeout = nil)
39
39
  wait_for_data(timeout) if timeout
40
40
  @socket.peek
41
41
  end
@@ -15,13 +15,13 @@ module Sanford::Protocol
15
15
  self.with_msg_body(request.to_hash)
16
16
  end
17
17
 
18
- def self.with_msg_body(body, size=nil, encoded_version=nil)
18
+ def self.with_msg_body(body, size = nil, encoded_version = nil)
19
19
  encoded_body = Sanford::Protocol.msg_body.encode(body)
20
20
  self.with_encoded_msg_body(encoded_body, size, encoded_version)
21
21
  end
22
22
 
23
- def self.with_encoded_msg_body(encoded_body, size=nil, encoded_version=nil)
24
- encoded_size = Sanford::Protocol.msg_size.encode(size || encoded_body.bytesize)
23
+ def self.with_encoded_msg_body(encoded_body, size = nil, encoded_version = nil)
24
+ encoded_size = Sanford::Protocol.msg_size.encode(size || encoded_body.bytesize)
25
25
  encoded_version ||= Sanford::Protocol.msg_version
26
26
  self.new(encoded_version, encoded_size, encoded_body)
27
27
  end
@@ -2,7 +2,7 @@ module Sanford; end
2
2
  module Sanford::Protocol
3
3
 
4
4
  class BadMessageError < RuntimeError
5
- def initialize(message, bt=nil)
5
+ def initialize(message, bt = nil)
6
6
  super(message)
7
7
  set_backtrace(bt || caller)
8
8
  end
@@ -11,7 +11,7 @@ module Sanford::Protocol
11
11
  class MsgData
12
12
  attr_reader :value
13
13
 
14
- def initialize(called_from=nil, &get_value)
14
+ def initialize(called_from = nil, &get_value)
15
15
 
16
16
  # By default, any exceptions from getting the value are "hidden" behind a
17
17
  # more general `BadMessageError`. In non-debug scenarios this is ideal and
@@ -13,18 +13,18 @@ module Sanford::Protocol
13
13
  self.new(hash['status'], hash['data'])
14
14
  end
15
15
 
16
- def initialize(status, data=nil)
16
+ def initialize(status, data = nil)
17
17
  super(build_status(status), data)
18
18
  end
19
19
 
20
- def code; status.code; end
21
- def code=(new_code); status.code = new_code; end
22
- def message; status.message; end
23
- def message=(new_message); status.message = new_message; end
24
- def to_s; status.to_s; end
20
+ def code; status.code; end
21
+ def code=(value); status.code = value; end
22
+ def message; status.message; end
23
+ def message=(value); status.message = value; end
24
+ def to_s; status.to_s; end
25
25
 
26
26
  def to_hash
27
- { 'status' => [ status.code, status.message ],
27
+ { 'status' => [status.code, status.message],
28
28
  'data' => data
29
29
  }
30
30
  end
@@ -26,25 +26,21 @@ module Sanford::Protocol
26
26
  end
27
27
 
28
28
  class Code < Struct.new(:number, :name)
29
- NUMBERS = {
30
- 'ok' => 200,
31
- 'bad_request' => 400,
32
- 'not_found' => 404,
33
- 'timeout' => 408,
34
- 'error' => 500
29
+ NAMES = {
30
+ 200 => 'OK',
31
+ 400 => 'BAD REQUEST',
32
+ 404 => 'NOT FOUND',
33
+ 408 => 'TIMEOUT',
34
+ 422 => 'INVALID',
35
+ 500 => 'ERROR'
35
36
  }.freeze
36
37
 
37
- def initialize(key)
38
- num = NUMBERS[key.to_s] || key.to_i
39
- name = NUMBERS.index(num) || NoName
40
- super(num, name.upcase)
38
+ def initialize(number)
39
+ n = number.to_i
40
+ super(n, NAMES[n])
41
41
  end
42
42
 
43
43
  def to_s; "[#{[number, name].compact.join(', ')}]"; end
44
-
45
- class NoName
46
- def self.upcase; nil; end
47
- end
48
44
  end
49
45
 
50
46
  end
@@ -1,5 +1,5 @@
1
1
  module Sanford
2
2
  module Protocol
3
- GEM_VERSION = "0.11.0"
3
+ GEM_VERSION = "0.12.0"
4
4
  end
5
5
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Sanford::Protocol::GEM_VERSION
9
9
  gem.authors = ["Collin Redding", "Kelly Redding"]
10
10
  gem.email = ["collin.redding@me.com", "kelly@kellyredding.com"]
11
- gem.description = "Ruby implementation of the Sanford TCP communication protocol."
12
11
  gem.summary = "Ruby implementation of the Sanford TCP communication protocol."
12
+ gem.description = "Ruby implementation of the Sanford TCP communication protocol."
13
13
  gem.homepage = "https://github.com/redding/sanford-protocol"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
@@ -17,7 +17,8 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
+ gem.add_development_dependency("assert", ["~> 2.16.1"])
21
+
20
22
  gem.add_dependency("bson", ["~> 1.7", "< 1.10.0"])
21
23
 
22
- gem.add_development_dependency("assert", ["~> 2.11"])
23
24
  end
@@ -14,6 +14,15 @@ FakeSocket = Sanford::Protocol::FakeSocket
14
14
 
15
15
  require 'test/support/factory'
16
16
 
17
+ # 1.8.7 backfills
18
+
19
+ # Array#sample
20
+ if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
21
+ class Array
22
+ alias_method :sample, :choice
23
+ end
24
+ end
25
+
17
26
  class Assert::Context
18
27
 
19
28
  def setup_some_msg_data(data = nil)
@@ -44,7 +44,7 @@ class Sanford::Protocol::Connection
44
44
  def start_server(options, &block)
45
45
  begin
46
46
  # this `fork` is a separate process, so it runs parallel to the code
47
- # after it's block
47
+ # after its block
48
48
  pid = fork do
49
49
  tcp_server = TCPServer.open 'localhost', 12000
50
50
  trap("TERM"){ tcp_server.close }
@@ -6,46 +6,55 @@ class Sanford::Protocol::ResponseStatus
6
6
  class UnitTests < Assert::Context
7
7
  desc "Sanford::Protocol::ResponseStatus"
8
8
  setup do
9
- @status = Sanford::Protocol::ResponseStatus.new(200, "OK")
9
+ @status_class = Sanford::Protocol::ResponseStatus
10
+ @code = Factory.integer
11
+ @msg = Factory.string
12
+ @status = @status_class.new(@code, @msg)
10
13
  end
11
14
  subject{ @status }
12
15
 
13
16
  should have_readers :code_obj, :message
14
17
  should have_imeths :code, :code=, :name, :to_i
15
18
 
16
- should "know it's code name" do
17
- named = Sanford::Protocol::ResponseStatus.new(200)
18
- unamed = Sanford::Protocol::ResponseStatus.new(999)
19
-
20
- assert_equal "OK", named.name
21
- assert_equal nil, unamed.name
19
+ should "know its code obj and message" do
20
+ assert_kind_of Code, subject.code_obj
21
+ assert_equal @code, subject.code_obj.number
22
+ assert_equal @msg, subject.message
22
23
  end
23
24
 
24
- should "know it's code number" do
25
- Code::NUMBERS.each do |name, value|
26
- status = Sanford::Protocol::ResponseStatus.new(name)
27
- assert_equal value, status.code
28
- end
25
+ should "know its code numbers" do
26
+ assert_equal subject.code_obj.number, subject.code
27
+ assert_equal subject.code, subject.to_i
29
28
 
30
- unamed = Sanford::Protocol::ResponseStatus.new('unamed')
31
- assert_equal 0, unamed.code
29
+ assert_equal 0, @status_class.new(Factory.string).code
32
30
  end
33
31
 
34
- should "return it's code number with #to_i" do
35
- assert_equal subject.code, subject.to_i
32
+ should "know its code names" do
33
+ assert_equal subject.code_obj.name, subject.name
34
+
35
+ assert_equal 'OK', @status_class.new(200).name
36
+ assert_equal 'BAD REQUEST', @status_class.new(400).name
37
+ assert_equal 'NOT FOUND', @status_class.new(404).name
38
+ assert_equal 'TIMEOUT', @status_class.new(408).name
39
+ assert_equal 'INVALID', @status_class.new(422).name
40
+ assert_equal 'ERROR', @status_class.new(500).name
41
+ assert_equal nil, @status_class.new(Factory.integer+500).name
36
42
  end
37
43
 
38
44
  should "allow setting its code" do
39
- number = Factory.integer
45
+ number = [200, 400, 404, 408, 422, 500].sample
40
46
  subject.code = number
41
- assert_equal number, subject.code
42
- end
43
47
 
44
- should "return it's code number and code name with #to_s" do
45
- named = Sanford::Protocol::ResponseStatus.new(200)
46
- unamed = Sanford::Protocol::ResponseStatus.new(999)
48
+ exp_status = @status_class.new(number)
49
+ assert_equal exp_status.code, subject.code
50
+ assert_equal exp_status.name, subject.name
51
+ end
47
52
 
53
+ should "return its code number and code name with #to_s" do
54
+ named = @status_class.new([200, 400, 404, 408, 422, 500].sample)
48
55
  assert_equal "[#{named.code}, #{named.name}]", named.to_s
56
+
57
+ unamed = @status_class.new(Factory.integer+500)
49
58
  assert_equal "[#{unamed.code}]", unamed.to_s
50
59
  end
51
60
 
@@ -6,95 +6,97 @@ class Sanford::Protocol::Response
6
6
  class UnitTests < Assert::Context
7
7
  desc "Sanford::Protocol::Response"
8
8
  setup do
9
- @response = Sanford::Protocol::Response.new([ 672, 'YAR!' ], { 'something' => true })
9
+ @num = Factory.integer+500
10
+ @msg = Factory.string
11
+ @data = { Factory.string => Factory.string }
12
+
13
+ @response_class = Sanford::Protocol::Response
14
+ @response = @response_class.new([@num, @msg], @data)
10
15
  end
11
16
  subject{ @response }
12
17
 
13
- should have_imeths :status, :data, :to_hash
14
- should have_imeths :code, :code=, :message, :message=, :to_s
15
18
  should have_cmeths :parse
19
+ should have_imeths :status, :data
20
+ should have_imeths :code, :code=, :message, :message=
21
+ should have_imeths :to_s, :to_hash
22
+
23
+ should "know its status and data" do
24
+ assert_equal @num, subject.status.code
25
+ assert_equal @msg, subject.status.message
26
+ assert_equal @data, subject.data
27
+ end
16
28
 
17
29
  should "demeter its status" do
18
- assert_equal subject.status.code, subject.code
30
+ assert_equal subject.status.code, subject.code
19
31
  assert_equal subject.status.message, subject.message
20
- assert_equal subject.status.to_s, subject.to_s
32
+ assert_equal subject.status.to_s, subject.to_s
21
33
 
22
- new_code = Factory.integer
23
- new_message = Factory.string
24
- subject.code = new_code
25
- subject.message = new_message
34
+ subject.code = new_code = Factory.integer
35
+ subject.message = new_msg = Factory.string
26
36
  assert_equal new_code, subject.code
27
- assert_equal new_message, subject.message
37
+ assert_equal new_msg, subject.message
28
38
  end
29
39
 
30
- should "return an instance of a Sanford::Protocol::Response given a hash using #parse" do
31
- # using BSON messages are hashes
32
- hash = {
33
- 'status' => [ 200, 'OK' ],
34
- 'data' => 'yes'
40
+ should "know its hash representation" do
41
+ # BSON messages are hashes
42
+ exp = {
43
+ 'status' => [@num, @msg],
44
+ 'data' => @data
35
45
  }
36
- request = Sanford::Protocol::Response.parse(hash)
37
-
38
- assert_instance_of Sanford::Protocol::Response, request
39
- assert_equal hash['status'].first, request.status.code
40
- assert_equal hash['status'].last, request.status.message
41
- assert_equal hash['data'], request.data
46
+ assert_equal exp, subject.to_hash
42
47
  end
43
48
 
44
- should "return the request as a hash with #to_hash" do
45
- # using BSON messages are hashes
46
- expected = {
47
- 'status' => [ 672, 'YAR!' ],
48
- 'data' => { 'something' => true }
49
+ should "should parse hash representations into objects" do
50
+ # BSON messages are hashes
51
+ hash = {
52
+ 'status' => [Factory.integer, Factory.string],
53
+ 'data' => Factory.string
49
54
  }
55
+ response = @response_class.parse(hash)
50
56
 
51
- assert_equal expected, subject.to_hash
57
+ assert_instance_of @response_class, response
58
+ assert_equal hash['status'].first, response.status.code
59
+ assert_equal hash['status'].last, response.status.message
60
+ assert_equal hash['data'], response.data
52
61
  end
53
62
 
54
- should "be comparable" do
55
- match_response = Sanford::Protocol::Response.new(
56
- subject.status.dup,
57
- subject.data.dup
58
- )
59
- assert_equal match_response, subject
63
+ should "know if it is equal to another response" do
64
+ equal = @response_class.new(subject.status.dup, subject.data.dup)
65
+ assert_equal equal, subject
66
+
67
+ not_equal = @response_class.new(Factory.integer, subject.data.dup)
68
+ assert_not_equal not_equal, subject
60
69
 
61
- not_match_response = Sanford::Protocol::Response.new(123, {})
62
- assert_not_equal not_match_response, subject
70
+ not_equal = @response_class.new(subject.status.dup, {})
71
+ assert_not_equal not_equal, subject
63
72
  end
64
73
 
65
74
  end
66
75
 
67
76
  # Somewhat of a system test, want to make sure if Response is passed some
68
- # "fuzzy" args that it will build it's status object as expected
77
+ # "fuzzy" args that it will build its status object as expected
69
78
  class StatusBuildingTests < UnitTests
70
79
 
71
- should "build a status with it's code set, given an integer" do
72
- response = Sanford::Protocol::Response.new(574)
80
+ should "build a status with its code set if given an integer" do
81
+ response = Sanford::Protocol::Response.new(@num)
73
82
 
74
- assert_equal 574, response.status.code
75
- assert_equal nil, response.status.message
76
- end
77
-
78
- should "build a status with it's code set, given a name" do
79
- response = Sanford::Protocol::Response.new('ok')
80
-
81
- assert_equal 200, response.status.code
82
- assert_equal nil, response.status.message
83
+ assert_equal @num, response.status.code
84
+ assert_equal nil, response.status.message
83
85
  end
84
86
 
85
87
  should "use a status object, if given one" do
86
- status = Sanford::Protocol::ResponseStatus.new(200, "OK")
88
+ status = Sanford::Protocol::ResponseStatus.new(@num, @msg)
87
89
  response = Sanford::Protocol::Response.new(status)
88
-
89
90
  assert_same status, response.status
90
91
  end
91
92
 
92
93
  should "build a status with a code and message set, when given both" do
93
- response = Sanford::Protocol::Response.new([ 348, "my message" ])
94
+ response = Sanford::Protocol::Response.new([@num, @msg])
94
95
 
95
- assert_equal 348, response.status.code
96
- assert_equal "my message", response.status.message
96
+ assert_equal @num, response.status.code
97
+ assert_equal @msg, response.status.message
97
98
  end
99
+
98
100
  end
99
101
 
100
102
  end
metadata CHANGED
@@ -1,13 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanford-protocol
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
5
- prerelease:
6
- segments:
7
- - 0
8
- - 11
9
- - 0
10
- version: 0.11.0
4
+ version: 0.12.0
11
5
  platform: ruby
12
6
  authors:
13
7
  - Collin Redding
@@ -16,46 +10,31 @@ autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
12
 
19
- date: 2015-06-19 00:00:00 Z
13
+ date: 2016-06-09 00:00:00 Z
20
14
  dependencies:
21
15
  - !ruby/object:Gem::Dependency
16
+ name: assert
17
+ prerelease: false
22
18
  requirement: &id001 !ruby/object:Gem::Requirement
23
- none: false
24
19
  requirements:
25
20
  - - ~>
26
21
  - !ruby/object:Gem::Version
27
- hash: 1
28
- segments:
29
- - 1
30
- - 7
31
- version: "1.7"
32
- - - <
33
- - !ruby/object:Gem::Version
34
- hash: 63
35
- segments:
36
- - 1
37
- - 10
38
- - 0
39
- version: 1.10.0
40
- type: :runtime
41
- name: bson
22
+ version: 2.16.1
23
+ type: :development
42
24
  version_requirements: *id001
43
- prerelease: false
44
25
  - !ruby/object:Gem::Dependency
26
+ name: bson
27
+ prerelease: false
45
28
  requirement: &id002 !ruby/object:Gem::Requirement
46
- none: false
47
29
  requirements:
48
30
  - - ~>
49
31
  - !ruby/object:Gem::Version
50
- hash: 21
51
- segments:
52
- - 2
53
- - 11
54
- version: "2.11"
55
- type: :development
56
- name: assert
32
+ version: "1.7"
33
+ - - <
34
+ - !ruby/object:Gem::Version
35
+ version: 1.10.0
36
+ type: :runtime
57
37
  version_requirements: *id002
58
- prerelease: false
59
38
  description: Ruby implementation of the Sanford TCP communication protocol.
60
39
  email:
61
40
  - collin.redding@me.com
@@ -69,9 +48,8 @@ extra_rdoc_files: []
69
48
  files:
70
49
  - .gitignore
71
50
  - Gemfile
72
- - LICENSE.txt
51
+ - LICENSE
73
52
  - README.md
74
- - Rakefile
75
53
  - lib/sanford-protocol.rb
76
54
  - lib/sanford-protocol/connection.rb
77
55
  - lib/sanford-protocol/fake_connection.rb
@@ -97,35 +75,28 @@ files:
97
75
  homepage: https://github.com/redding/sanford-protocol
98
76
  licenses: []
99
77
 
78
+ metadata: {}
79
+
100
80
  post_install_message:
101
81
  rdoc_options: []
102
82
 
103
83
  require_paths:
104
84
  - lib
105
85
  required_ruby_version: !ruby/object:Gem::Requirement
106
- none: false
107
86
  requirements:
108
- - - ">="
87
+ - &id003
88
+ - ">="
109
89
  - !ruby/object:Gem::Version
110
- hash: 3
111
- segments:
112
- - 0
113
90
  version: "0"
114
91
  required_rubygems_version: !ruby/object:Gem::Requirement
115
- none: false
116
92
  requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- hash: 3
120
- segments:
121
- - 0
122
- version: "0"
93
+ - *id003
123
94
  requirements: []
124
95
 
125
96
  rubyforge_project:
126
- rubygems_version: 1.8.25
97
+ rubygems_version: 2.6.4
127
98
  signing_key:
128
- specification_version: 3
99
+ specification_version: 4
129
100
  summary: Ruby implementation of the Sanford TCP communication protocol.
130
101
  test_files:
131
102
  - test/helper.rb
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require "bundler/gem_tasks"