trexrb 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a2e7466f0ae48623f28f020285ff34eb0f3ebce8
4
- data.tar.gz: 32aa5713cab8237066bcb13feb97436cd09c5f46
3
+ metadata.gz: 0275222130a56cc32a2d798d2a9cc07348645d57
4
+ data.tar.gz: ef3b4d92a01ecad82b6eb5c25f46ba27047c3bad
5
5
  SHA512:
6
- metadata.gz: 3cafaedd8efa8ea382e97c32a2f8f580b0c4577a4eaa827b9cbedd333de1640a76a1fe068071fa05884789094197ad8ee99c9442894633f91ff656b683cfd01a
7
- data.tar.gz: 0183cb43d24f51cff81236aaf10740e5c7a6ef6bc5bda8e5e8092dc26c5c6032c7f3b3f7b28f3585602e6a7face17ac4b635f71cf5cfe23d24efd91c76a24077
6
+ metadata.gz: 6cb4743922e20bda32ac49b8633da74061bf2797bd56aa0e842b27e9d126c2aa9acbc00efa27af2a41d33ac9d422d2a32e33a5896a7629c4ff8c8c74484ecfb0
7
+ data.tar.gz: 87eaabbe2d9f2a3e6cc49a509bcdbf3c355030a454af1c2b1a4c7e9e254eda8bb5d24a8cb2307002f3c848ea3e7425208d38b8214c59c40851217c9060ef3e06
@@ -1,9 +1,106 @@
1
- require "trexrb/version"
2
- require "trexrb/storage"
3
- require "trexrb/backend"
1
+ require 'trexrb/version'
2
+ require 'socket'
3
+ require 'yaml'
4
4
 
5
- module Trexrb
6
- def self.store
7
- @store ||= Storage.new
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
@@ -1,3 +1,3 @@
1
- module Trexrb
2
- VERSION = "0.1.0"
1
+ class Trexrb
2
+ VERSION = "0.1.2"
3
3
  end
@@ -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 "rails-i18n", '~> 4.0.0'
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.0
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-04-15 00:00:00.000000000 Z
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: rails-i18n
57
+ name: pry
58
58
  requirement: !ruby/object:Gem::Requirement
59
59
  requirements:
60
- - - "~>"
60
+ - - ">="
61
61
  - !ruby/object:Gem::Version
62
- version: 4.0.0
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: 4.0.0
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:
@@ -1,11 +0,0 @@
1
- require 'rails-i18n'
2
-
3
- module Trexrb
4
- class Backend < I18n::Backend::KeyValue
5
- include I18n::Backend::Memoize
6
-
7
- def initialize
8
- super(Trexrb.store)
9
- end
10
- end
11
- end
@@ -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