net-snarl 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Isolate +5 -0
- data/Rakefile +41 -0
- data/lib/net/snarl.rb +75 -0
- data/spec/net/snarl_spec.rb +21 -0
- data/specs.watchr +76 -0
- metadata +74 -0
data/Isolate
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
begin
|
2
|
+
require 'isolate/now'
|
3
|
+
rescue LoadError
|
4
|
+
abort "This project requires Isolate to work, please 'gem install isolate' first."
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rubygems/package_task'
|
8
|
+
|
9
|
+
# continious testing
|
10
|
+
desc "Invoke watchr in Isolation mode"
|
11
|
+
task :watchr, [:options] do |t, args|
|
12
|
+
cmd = "#{Gem.ruby} -S watchr #{args.options}"
|
13
|
+
puts cmd
|
14
|
+
exec cmd
|
15
|
+
end
|
16
|
+
|
17
|
+
# gemspec
|
18
|
+
spec = Gem::Specification.new do |s|
|
19
|
+
s.name = 'net-snarl'
|
20
|
+
s.summary = 'Simple implementation of Snarl SNP protocol'
|
21
|
+
s.version = '0.0.1'
|
22
|
+
s.author = 'Luis Lavena'
|
23
|
+
s.email = 'luislavena@gmail.com'
|
24
|
+
s.description = <<-EOT
|
25
|
+
Net::Snarl is a trivial implementation of Snarl SNP protocol. It is aimed to
|
26
|
+
verbatin return Snarl answers for further processing.
|
27
|
+
EOT
|
28
|
+
s.homepage = 'http://github.com/luislavena/net-snarl'
|
29
|
+
|
30
|
+
s.require_path = 'lib'
|
31
|
+
s.files = FileList[
|
32
|
+
'lib/**/*.rb', 'spec/**/*.rb', 'Rakefile',
|
33
|
+
'Isolate', 'specs.watchr'
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
# packaging
|
38
|
+
Gem::PackageTask.new(spec) do |pkg|
|
39
|
+
pkg.need_zip = false
|
40
|
+
pkg.need_tar = false
|
41
|
+
end
|
data/lib/net/snarl.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'socket'
|
2
|
+
|
3
|
+
module Net
|
4
|
+
class Snarl
|
5
|
+
HEADER = "type=SNP".freeze
|
6
|
+
PROTOCOL_VERSION = "version=1.0".freeze
|
7
|
+
TERMINATOR = "\r\n".freeze
|
8
|
+
DEFAULT_TIMEOUT = 10
|
9
|
+
|
10
|
+
attr_reader :host, :port
|
11
|
+
|
12
|
+
def initialize(host = nil, port = nil)
|
13
|
+
@host = host || '127.0.0.1'
|
14
|
+
@port = (port || 9887).to_i
|
15
|
+
end
|
16
|
+
|
17
|
+
def notify(params = {})
|
18
|
+
add_defaults(params)
|
19
|
+
|
20
|
+
connected { |c| c.write command('notification', params) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def register(app)
|
24
|
+
connected { |c| c.write command('register', :app => app) }
|
25
|
+
end
|
26
|
+
|
27
|
+
def unregister(app)
|
28
|
+
connected { |c| c.write command('unregister', :app => app) }
|
29
|
+
end
|
30
|
+
|
31
|
+
def add_class(app, klass, params = {})
|
32
|
+
params[:app] = app
|
33
|
+
params[:class] = klass
|
34
|
+
|
35
|
+
connected { |c| c.write command('add_class', params) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def hello
|
39
|
+
connected { |c| c.write command('hello') }
|
40
|
+
end
|
41
|
+
|
42
|
+
def version
|
43
|
+
connected { |c| c.write command('version') }
|
44
|
+
end
|
45
|
+
|
46
|
+
def inspect
|
47
|
+
version = hello.split('/').last
|
48
|
+
"#<#{self.class} host=#{@host}, port=#{@port}, version=#{version.inspect}>"
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def command(action, params = {})
|
54
|
+
cmd = [HEADER, PROTOCOL_VERSION, "action=#{action}"]
|
55
|
+
|
56
|
+
params.each do |k, v|
|
57
|
+
cmd << "#{k}=#{v}"
|
58
|
+
end
|
59
|
+
|
60
|
+
cmd.join('#?') + TERMINATOR
|
61
|
+
end
|
62
|
+
|
63
|
+
def add_defaults(params)
|
64
|
+
params[:timeout] ||= DEFAULT_TIMEOUT
|
65
|
+
params
|
66
|
+
end
|
67
|
+
|
68
|
+
def connected
|
69
|
+
TCPSocket.open(@host, @port) do |s|
|
70
|
+
yield s
|
71
|
+
s.gets.chomp
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'net/snarl'
|
2
|
+
|
3
|
+
describe Net::Snarl do
|
4
|
+
context 'when first created' do
|
5
|
+
it 'defaults connection to local server' do
|
6
|
+
snarl = Net::Snarl.new
|
7
|
+
snarl.host.should == '127.0.0.1'
|
8
|
+
snarl.port.should == 9887
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'allows connection to remote server' do
|
12
|
+
snarl = Net::Snarl.new('remote', 1234)
|
13
|
+
snarl.host.should == 'remote'
|
14
|
+
snarl.port.should == 1234
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when connected' do
|
19
|
+
it 'should have some decent specs'
|
20
|
+
end
|
21
|
+
end
|
data/specs.watchr
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Run me with:
|
2
|
+
# $ watchr specs.watchr
|
3
|
+
|
4
|
+
# --------------------------------------------------
|
5
|
+
# Rules
|
6
|
+
# --------------------------------------------------
|
7
|
+
watch('^spec/(.*)_spec\.rb') { |m| run_spec_matching(m[1]) }
|
8
|
+
watch('^lib/(.*)\.rb') { |m| run_spec_matching(m[1]) }
|
9
|
+
watch('^spec/spec_helper\.rb') { run_all_specs }
|
10
|
+
watch('^spec/support/.*\.rb') { run_all_specs }
|
11
|
+
|
12
|
+
# --------------------------------------------------
|
13
|
+
# RSpec specifics
|
14
|
+
# --------------------------------------------------
|
15
|
+
def all_spec_files
|
16
|
+
Dir.glob('spec/**/*_spec.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
def run_all_specs
|
20
|
+
rspec all_spec_files
|
21
|
+
end
|
22
|
+
|
23
|
+
def run_spec_matching(thing_to_match)
|
24
|
+
matches = all_spec_files.grep(/#{thing_to_match}/i)
|
25
|
+
if matches.empty?
|
26
|
+
puts "Sorry, thanks for playing, but there were no matches for #{thing_to_match}"
|
27
|
+
else
|
28
|
+
rspec matches
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# --------------------------------------------------
|
33
|
+
# Signal Handling
|
34
|
+
# --------------------------------------------------
|
35
|
+
def no_interruption
|
36
|
+
@interrupted = false
|
37
|
+
end
|
38
|
+
|
39
|
+
# Ctrl-C
|
40
|
+
Signal.trap 'INT' do
|
41
|
+
if @interrupted then
|
42
|
+
@wants_to_quit = true
|
43
|
+
exit
|
44
|
+
else
|
45
|
+
puts "Interrupt a second time to quit"
|
46
|
+
@interrupted = true
|
47
|
+
Kernel.sleep 1.5
|
48
|
+
run_all_specs
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# --------------------------------------------------
|
53
|
+
# Helpers
|
54
|
+
# --------------------------------------------------
|
55
|
+
def run(command)
|
56
|
+
system clearscreen
|
57
|
+
puts command
|
58
|
+
system command
|
59
|
+
no_interruption
|
60
|
+
end
|
61
|
+
|
62
|
+
def rspec(*paths)
|
63
|
+
run "rspec #{gem_opt} -I#{include_dirs} #{paths.flatten.join(' ')}"
|
64
|
+
end
|
65
|
+
|
66
|
+
def include_dirs
|
67
|
+
%w(. lib spec).join(File::PATH_SEPARATOR)
|
68
|
+
end
|
69
|
+
|
70
|
+
def gem_opt
|
71
|
+
defined?(Gem) ? "-rubygems" : ""
|
72
|
+
end
|
73
|
+
|
74
|
+
def clearscreen
|
75
|
+
RbConfig::CONFIG['host_os'] =~ /mingw|mswin/ ? 'cls' : 'clear'
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: net-snarl
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Luis Lavena
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-25 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: |
|
23
|
+
Net::Snarl is a trivial implementation of Snarl SNP protocol. It is aimed to
|
24
|
+
verbatin return Snarl answers for further processing.
|
25
|
+
|
26
|
+
email: luislavena@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files: []
|
32
|
+
|
33
|
+
files:
|
34
|
+
- lib/net/snarl.rb
|
35
|
+
- spec/net/snarl_spec.rb
|
36
|
+
- Rakefile
|
37
|
+
- Isolate
|
38
|
+
- specs.watchr
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/luislavena/net-snarl
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
hash: 3
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.7
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Simple implementation of Snarl SNP protocol
|
73
|
+
test_files: []
|
74
|
+
|