simple_socket 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +26 -0
- data/README.md +20 -0
- data/Rakefile +7 -0
- data/lib/simple_socket.rb +27 -0
- data/simple_socket.gemspec +17 -0
- data/spec/simple_socket_spec.rb +62 -0
- data/spec/spec_helper.rb +31 -0
- metadata +79 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
simple_socket (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.3)
|
10
|
+
rake (0.9.2.2)
|
11
|
+
rspec (2.8.0)
|
12
|
+
rspec-core (~> 2.8.0)
|
13
|
+
rspec-expectations (~> 2.8.0)
|
14
|
+
rspec-mocks (~> 2.8.0)
|
15
|
+
rspec-core (2.8.0)
|
16
|
+
rspec-expectations (2.8.0)
|
17
|
+
diff-lcs (~> 1.1.2)
|
18
|
+
rspec-mocks (2.8.0)
|
19
|
+
|
20
|
+
PLATFORMS
|
21
|
+
ruby
|
22
|
+
|
23
|
+
DEPENDENCIES
|
24
|
+
rake
|
25
|
+
rspec
|
26
|
+
simple_socket!
|
data/README.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
|
2
|
+
# Simple Socket
|
3
|
+
|
4
|
+
Simple Socket is a gem to allow you to more easily make use of TCP sockets.
|
5
|
+
|
6
|
+
### Installation
|
7
|
+
|
8
|
+
gem install simple_socket
|
9
|
+
|
10
|
+
### Usage
|
11
|
+
|
12
|
+
=> SimpleSocket.new('localhost', 12345).write("my_query")
|
13
|
+
=> "response_value_from_socket"
|
14
|
+
|
15
|
+
|
16
|
+
SimpleSocket also supports specifying a timeout parameter when creating a new socket:
|
17
|
+
|
18
|
+
=> SimpleSocket.new('localhost', 12345, 12)
|
19
|
+
|
20
|
+
This will set the timeout to 12 seconds. The default timeout is 10 seconds.
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'timeout'
|
3
|
+
|
4
|
+
class SimpleSocket
|
5
|
+
attr_reader :host, :port, :timeout
|
6
|
+
DEFAULT_TIMEOUT = 10
|
7
|
+
|
8
|
+
def initialize(host, port, timeout = DEFAULT_TIMEOUT)
|
9
|
+
@host = host
|
10
|
+
@port = port
|
11
|
+
@timeout = timeout
|
12
|
+
end
|
13
|
+
|
14
|
+
def write(message)
|
15
|
+
begin
|
16
|
+
socket = TCPSocket.new(host, port)
|
17
|
+
socket.puts(message)
|
18
|
+
response = nil
|
19
|
+
Timeout::timeout(timeout) do
|
20
|
+
response = socket.read
|
21
|
+
end
|
22
|
+
ensure
|
23
|
+
socket.close if socket
|
24
|
+
end
|
25
|
+
response
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "simple_socket"
|
3
|
+
s.version = "0.1.1"
|
4
|
+
s.authors = ["Cyrus Innovation LLC"]
|
5
|
+
s.email = ["pzimbelman@cyrusinnovation.com"]
|
6
|
+
s.homepage = "https://github.com/cyrusinnovation/simple_socket"
|
7
|
+
s.summary = %q{TCP socket wrapper in a Ruby gem}
|
8
|
+
s.description = %q{A gem designed to allow easier use of a TCP socket by providing proper handling of the socket, including timeout/error handling, closing the connection, etc.}
|
9
|
+
|
10
|
+
s.files = `git ls-files`.split("\n")
|
11
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
12
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
13
|
+
s.require_paths = ["lib"]
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec"
|
16
|
+
s.add_development_dependency "rake"
|
17
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require_relative 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleSocket do
|
4
|
+
let(:host) { "myhost" }
|
5
|
+
let(:port) { 1151 }
|
6
|
+
|
7
|
+
it "should create a simple socket with a default timeout" do
|
8
|
+
socket = SimpleSocket.new(host, port)
|
9
|
+
socket.host.should == host
|
10
|
+
socket.port.should == port
|
11
|
+
socket.timeout.should == SimpleSocket::DEFAULT_TIMEOUT
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should allow specifying a timeout" do
|
15
|
+
socket = SimpleSocket.new(host, port, 3)
|
16
|
+
socket.timeout.should == 3
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should create a TCP socket with the correct parameters" do
|
20
|
+
TCPSocket.should_receive(:new).with(host, port).and_return(a_stubbed_socket)
|
21
|
+
SimpleSocket.new(host, port).write("foobar")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return the sockets response" do
|
25
|
+
with(a_stubbed_socket.that_returns("A response")) do
|
26
|
+
socket = SimpleSocket.new(host, port)
|
27
|
+
socket.write("foo").should == "A response"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should send the message passed in to the socket" do
|
32
|
+
with(a_stubbed_socket.that_expects_message_of("my message")) do
|
33
|
+
SimpleSocket.new(host, port).write("my message")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should close the socket" do
|
38
|
+
with(a_stubbed_socket.that_requires_closing) do
|
39
|
+
SimpleSocket.new(host, port).write("foo")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should wrap the socket read with a timeout" do
|
44
|
+
with(a_stubbed_socket) do
|
45
|
+
Timeout.should_receive(:timeout).with(SimpleSocket::DEFAULT_TIMEOUT)
|
46
|
+
SimpleSocket.new(host, port).write("foo")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should close the socket even if an exceptions is raised" do
|
51
|
+
with(a_stubbed_socket.that_requires_closing) do
|
52
|
+
Timeout.stub(:timeout).and_raise(Timeout::Error)
|
53
|
+
lambda {SimpleSocket.new(host, port).write("foo")}.should raise_error
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should return correct error if no connection is made" do
|
58
|
+
expect {
|
59
|
+
SimpleSocket.new("foobar", 123).write("foo")
|
60
|
+
}.to raise_error(SocketError)
|
61
|
+
end
|
62
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative '../lib/simple_socket'
|
2
|
+
|
3
|
+
module MockHelpers
|
4
|
+
def that_returns(response)
|
5
|
+
self.should_receive(:read).and_return(response)
|
6
|
+
self
|
7
|
+
end
|
8
|
+
|
9
|
+
def that_expects_message_of(message)
|
10
|
+
self.should_receive(:puts).with(message)
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
def that_requires_closing
|
15
|
+
self.should_receive(:close)
|
16
|
+
self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def a_stubbed_socket
|
21
|
+
double("tcpsocket").tap do |s|
|
22
|
+
s.stub(:puts)
|
23
|
+
s.stub(:read)
|
24
|
+
s.stub(:close)
|
25
|
+
end.extend(MockHelpers)
|
26
|
+
end
|
27
|
+
|
28
|
+
def with(socket)
|
29
|
+
TCPSocket.stub(:new => socket)
|
30
|
+
yield
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_socket
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cyrus Innovation LLC
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70319314834420 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70319314834420
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70319314833980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70319314833980
|
36
|
+
description: A gem designed to allow easier use of a TCP socket by providing proper
|
37
|
+
handling of the socket, including timeout/error handling, closing the connection,
|
38
|
+
etc.
|
39
|
+
email:
|
40
|
+
- pzimbelman@cyrusinnovation.com
|
41
|
+
executables: []
|
42
|
+
extensions: []
|
43
|
+
extra_rdoc_files: []
|
44
|
+
files:
|
45
|
+
- Gemfile
|
46
|
+
- Gemfile.lock
|
47
|
+
- README.md
|
48
|
+
- Rakefile
|
49
|
+
- lib/simple_socket.rb
|
50
|
+
- simple_socket.gemspec
|
51
|
+
- spec/simple_socket_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: https://github.com/cyrusinnovation/simple_socket
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.17
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: TCP socket wrapper in a Ruby gem
|
77
|
+
test_files:
|
78
|
+
- spec/simple_socket_spec.rb
|
79
|
+
- spec/spec_helper.rb
|