trexrb 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/trexrb.rb +103 -6
- data/lib/trexrb/version.rb +2 -2
- data/trexrb.gemspec +1 -1
- metadata +8 -9
- data/lib/trexrb/backend.rb +0 -11
- data/lib/trexrb/storage.rb +0 -83
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0275222130a56cc32a2d798d2a9cc07348645d57
|
4
|
+
data.tar.gz: ef3b4d92a01ecad82b6eb5c25f46ba27047c3bad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6cb4743922e20bda32ac49b8633da74061bf2797bd56aa0e842b27e9d126c2aa9acbc00efa27af2a41d33ac9d422d2a32e33a5896a7629c4ff8c8c74484ecfb0
|
7
|
+
data.tar.gz: 87eaabbe2d9f2a3e6cc49a509bcdbf3c355030a454af1c2b1a4c7e9e254eda8bb5d24a8cb2307002f3c848ea3e7425208d38b8214c59c40851217c9060ef3e06
|
data/lib/trexrb.rb
CHANGED
@@ -1,9 +1,106 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'trexrb/version'
|
2
|
+
require 'socket'
|
3
|
+
require 'yaml'
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
class Trexrb
|
6
|
+
DEFAULT_HOST = 'localhost'
|
7
|
+
DEFAULT_PORT = 4040
|
8
|
+
|
9
|
+
def initialize(host = nil, port = nil)
|
10
|
+
@host = host || DEFAULT_HOST
|
11
|
+
@port = port || DEFAULT_PORT
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(key)
|
15
|
+
with_connection do |conn|
|
16
|
+
conn.print Request.new.get(key)
|
17
|
+
|
18
|
+
Response.new(conn.read).body
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def set(key, value)
|
23
|
+
with_connection do |conn|
|
24
|
+
conn.print Request.new.set(key, value)
|
25
|
+
|
26
|
+
Response.new(conn.read).body
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def keys
|
31
|
+
with_connection do |conn|
|
32
|
+
conn.print Request.new.list
|
33
|
+
result = Response.new(conn.read).body
|
34
|
+
|
35
|
+
result || []
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
alias_method :[], :get
|
40
|
+
alias_method :[]=, :set
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
attr_reader :host, :port
|
45
|
+
|
46
|
+
def with_connection
|
47
|
+
socket = Socket.tcp(host, port)
|
48
|
+
yield socket
|
49
|
+
rescue => ex
|
50
|
+
puts "Connection error: #{ex.inspect}"
|
51
|
+
ensure
|
52
|
+
socket.close_write
|
53
|
+
end
|
54
|
+
|
55
|
+
Response = Struct.new(:data) do
|
56
|
+
def body
|
57
|
+
return if no_data?
|
58
|
+
|
59
|
+
YAML.load(cleaned_data)
|
60
|
+
rescue => ex
|
61
|
+
puts ex.inspect
|
62
|
+
cleaned_data
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def no_data?
|
68
|
+
data.to_s.strip.length == 0
|
69
|
+
end
|
70
|
+
|
71
|
+
def cleaned_data
|
72
|
+
remove_message_type_marker(data).rstrip
|
73
|
+
end
|
74
|
+
|
75
|
+
# RESP http://redis.io/topics/protocol uses first byte of the
|
76
|
+
# response to mark type of the message
|
77
|
+
def remove_message_type_marker(data)
|
78
|
+
data[1..-1]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
private_constant :Response
|
82
|
+
|
83
|
+
Request = Class.new do
|
84
|
+
FIELD_SEPARATOR = "\t"
|
85
|
+
TERMINATOR = "\r\n"
|
86
|
+
|
87
|
+
def get(key)
|
88
|
+
build("GET", key)
|
89
|
+
end
|
90
|
+
|
91
|
+
def set(key, value)
|
92
|
+
build("SET", key, YAML.dump(value))
|
93
|
+
end
|
94
|
+
|
95
|
+
def list
|
96
|
+
build("LIST")
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def build(command_name, *params)
|
102
|
+
([command_name] + params).join(FIELD_SEPARATOR) + TERMINATOR
|
103
|
+
end
|
8
104
|
end
|
105
|
+
private_constant :Request
|
9
106
|
end
|
data/lib/trexrb/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "0.1.
|
1
|
+
class Trexrb
|
2
|
+
VERSION = "0.1.2"
|
3
3
|
end
|
data/trexrb.gemspec
CHANGED
@@ -21,5 +21,5 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.11"
|
22
22
|
spec.add_development_dependency "rake", "~> 10.0"
|
23
23
|
spec.add_development_dependency "rspec", "~> 3.0"
|
24
|
-
spec.add_development_dependency "
|
24
|
+
spec.add_development_dependency "pry"
|
25
25
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trexrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ignacy Moryc
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-07-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -54,19 +54,19 @@ dependencies:
|
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '3.0'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
|
-
name:
|
57
|
+
name: pry
|
58
58
|
requirement: !ruby/object:Gem::Requirement
|
59
59
|
requirements:
|
60
|
-
- - "
|
60
|
+
- - ">="
|
61
61
|
- !ruby/object:Gem::Version
|
62
|
-
version:
|
62
|
+
version: '0'
|
63
63
|
type: :development
|
64
64
|
prerelease: false
|
65
65
|
version_requirements: !ruby/object:Gem::Requirement
|
66
66
|
requirements:
|
67
|
-
- - "
|
67
|
+
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version:
|
69
|
+
version: '0'
|
70
70
|
description:
|
71
71
|
email:
|
72
72
|
- imoryc@gmail.com
|
@@ -84,8 +84,6 @@ files:
|
|
84
84
|
- bin/console
|
85
85
|
- bin/setup
|
86
86
|
- lib/trexrb.rb
|
87
|
-
- lib/trexrb/backend.rb
|
88
|
-
- lib/trexrb/storage.rb
|
89
87
|
- lib/trexrb/version.rb
|
90
88
|
- trexrb.gemspec
|
91
89
|
homepage: https://github.com/ignacy/trexrb
|
@@ -113,3 +111,4 @@ signing_key:
|
|
113
111
|
specification_version: 4
|
114
112
|
summary: Client library for trex I18n store
|
115
113
|
test_files: []
|
114
|
+
has_rdoc:
|
data/lib/trexrb/backend.rb
DELETED
data/lib/trexrb/storage.rb
DELETED
@@ -1,83 +0,0 @@
|
|
1
|
-
module Trexrb
|
2
|
-
class Storage
|
3
|
-
DEFAULT_HOST = 'localhost'
|
4
|
-
DEFAULT_PORT = 4040
|
5
|
-
|
6
|
-
def initialize(host = nil, port = nil)
|
7
|
-
@host = host || DEFAULT_HOST
|
8
|
-
@port = port || DEFAULT_PORT
|
9
|
-
end
|
10
|
-
|
11
|
-
def get(key)
|
12
|
-
with_connection { |conn| conn.print Request.new.get(key) }
|
13
|
-
end
|
14
|
-
|
15
|
-
def set(key, value)
|
16
|
-
with_connection { |conn| conn.print Request.new.set(key, value) }
|
17
|
-
end
|
18
|
-
|
19
|
-
def keys
|
20
|
-
with_connection { |conn| conn.print Request.new.list }
|
21
|
-
end
|
22
|
-
|
23
|
-
alias_method :[], :get
|
24
|
-
alias_method :[]=, :set
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
attr_reader :host, :port
|
29
|
-
|
30
|
-
def with_connection
|
31
|
-
socket = Socket.tcp(host, port)
|
32
|
-
yield socket
|
33
|
-
rescue => ex
|
34
|
-
puts "Connection error: #{ex.inspect}"
|
35
|
-
ensure
|
36
|
-
socket.close_write
|
37
|
-
return Response.new(socket.read).body
|
38
|
-
end
|
39
|
-
|
40
|
-
Response = Struct.new(:data) do
|
41
|
-
def body
|
42
|
-
return "{}" if no_data?
|
43
|
-
YAML.load(cleaned_data)
|
44
|
-
rescue => ex
|
45
|
-
puts ex.inspect
|
46
|
-
cleaned_data
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def no_data?
|
52
|
-
data.to_s.strip.length == 0
|
53
|
-
end
|
54
|
-
|
55
|
-
def cleaned_data
|
56
|
-
data.rstrip
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
Request = Class.new do
|
61
|
-
FIELD_SEPARATOR = "\t"
|
62
|
-
TERMINATOR = "\r\n"
|
63
|
-
|
64
|
-
def get(key)
|
65
|
-
build("GET", key)
|
66
|
-
end
|
67
|
-
|
68
|
-
def set(key, value)
|
69
|
-
build("SET", key, YAML.dump(value))
|
70
|
-
end
|
71
|
-
|
72
|
-
def list
|
73
|
-
build("LIST")
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
|
78
|
-
def build(command_name, *params)
|
79
|
-
([command_name] + params).join(FIELD_SEPARATOR) + TERMINATOR
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|