oxblood 0.1.0.dev2 → 0.1.0.dev3

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: 4a00f0943e58421dc3712679260605e17607e709
4
- data.tar.gz: 8cf70de44aaa9dc92d6c58c614781548ee3db53b
3
+ metadata.gz: 2d7a859b01da306481014391452d354b78e733d9
4
+ data.tar.gz: b5b6a5ccc51f7b07c562c29b434a85f65b9bd4cd
5
5
  SHA512:
6
- metadata.gz: 90fd668f8be2a153061537d565c0470f9202121b88bf64c8ee82e03b4d057d5a34a1cf6a7a989ffa225afba91406e7342901b9c696f7c2988ac6c0b56c4d3881
7
- data.tar.gz: eab75bed05a363806827dffed9bcfe0f32b51d900fc4e31248eb1c42d44ebfc7549e61e3941e027854d345536b7d14fc9cfc84b5ea2ca91c0c7c64897c95684e
6
+ metadata.gz: c1dffe945b9bb71adec19fc0f33a6847ca90fa325d061f699e7b784ced2aedcc4ec0f7685a17764e94db4f2ca513e830f0233e7a379b89a75584409f08862ce0
7
+ data.tar.gz: 53ccf202c3821c309a5d7e4c998280f682f7197d54f9c2f2cb6e7295bd07acb7054a5b4df50086e7b604a91f2911301d79677f416dcd8dd046a2202ec561b42e
data/.codeclimate.yml CHANGED
@@ -9,6 +9,8 @@ engines:
9
9
  enabled: true
10
10
  rubocop:
11
11
  enabled: true
12
+ exclude_fingerprints:
13
+ - 5b0714ee48391458a7484df9bc2ac262
12
14
  ratings:
13
15
  paths:
14
16
  - "**.rb"
data/.gitignore CHANGED
@@ -7,5 +7,6 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /spec/examples.txt
10
- /tmp/
10
+ /tmp/*
11
+ !/tmp/.keep
11
12
  /benchmarks/Gemfile.lock
@@ -61,7 +61,7 @@ module Oxblood
61
61
  serialize([:HVALS, key])
62
62
  end
63
63
 
64
- def hscan(key, cursor)
64
+ def hscan(_key, _cursor)
65
65
  raise 'Not implemented!'
66
66
  end
67
67
 
@@ -69,6 +69,10 @@ module Oxblood
69
69
 
70
70
  # ------------------ Connection ---------------------
71
71
 
72
+ def auth(password)
73
+ serialize([:AUTH, password])
74
+ end
75
+
72
76
  def ping(message = nil)
73
77
  command = [:PING]
74
78
  command << message if message
@@ -76,6 +80,10 @@ module Oxblood
76
80
  serialize(command)
77
81
  end
78
82
 
83
+ def select(index)
84
+ serialize([:SELECT, index])
85
+ end
86
+
79
87
  # ------------------ Server ---------------------
80
88
 
81
89
  def info(section = nil)
@@ -1,5 +1,6 @@
1
1
  require 'oxblood/protocol'
2
2
  require 'oxblood/buffered_io'
3
+ require 'oxblood/session'
3
4
 
4
5
  module Oxblood
5
6
  # Class responsible for connection maintenance
@@ -7,54 +8,51 @@ module Oxblood
7
8
  TimeoutError = Class.new(RuntimeError)
8
9
 
9
10
  class << self
10
- # Open Redis connection
11
+ # Open connection to Redis server
11
12
  #
12
- # @param [Hash] options Connection options
13
+ # @param [Hash] opts Connection options
13
14
  #
14
- # @option (see .connect_tcp)
15
- # @option (see .connect_unix)
16
- def open(options = {})
17
- options.key?(:path) ? connect_unix(options) : connect_tcp(options)
18
- end
19
-
20
- # Connect to Redis server through TCP
15
+ # @option opts [Float] :timeout (1.0) socket read timeout
16
+ # @option opts [Integer] :db database number
17
+ # @option opts [String] :password
21
18
  #
22
- # @param [Hash] options Connection options
19
+ # @option opts [String] :host ('localhost') Hostname or IP address to connect to
20
+ # @option opts [Integer] :port (6379) Port Redis server listens on
21
+ # @option opts [Float] :connect_timeout (1.0) socket connect timeout
23
22
  #
24
- # @option options [String] :host ('localhost') Hostname or IP address to connect to
25
- # @option options [Integer] :port (5672) Port Redis server listens on
26
- # @option options [Float] :timeout (1.0) socket read timeout
27
- # @option options [Float] :connect_timeout (1.0) socket connect timeout
23
+ # @option opts [String] :path UNIX socket path
28
24
  #
29
25
  # @return [Oxblood::Connection] connection instance
30
- def connect_tcp(options = {})
31
- host = options.fetch(:host, 'localhost')
32
- port = options.fetch(:port, 6379)
33
- timeout = options.fetch(:timeout, 1.0)
34
- connect_timeout = options.fetch(:connect_timeout, 1.0)
26
+ def open(opts = {})
27
+ socket = if opts.key?(:path)
28
+ unix_socket(opts.fetch(:path))
29
+ else
30
+ host = opts.fetch(:host, 'localhost')
31
+ port = opts.fetch(:port, 6379)
32
+ connect_timeout = opts.fetch(:connect_timeout, 1.0)
33
+
34
+ tcp_socket(host, port, connect_timeout)
35
+ end
36
+
37
+ timeout = opts.fetch(:timeout, 1.0)
38
+
39
+ new(socket, timeout).tap do |conn|
40
+ session = Session.new(conn)
41
+ session.auth!(opts[:password]) if opts[:password]
42
+ session.select(opts[:db]) if opts[:db]
43
+ end
44
+ end
35
45
 
36
- socket = Socket.tcp(host, port, connect_timeout: connect_timeout)
37
- socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
46
+ private
38
47
 
39
- new(socket, timeout)
48
+ def unix_socket(path)
49
+ UNIXSocket.new(path)
40
50
  end
41
51
 
42
- # Connect to Redis server through UNIX socket
43
- #
44
- # @param [Hash] options Connection options
45
- #
46
- # @option options [String] :path UNIX socket path
47
- # @option options [Float] :timeout (1.0) socket read timeout
48
- #
49
- # @raise [KeyError] if :path was not passed
50
- #
51
- # @return [Oxblood::Connection] connection instance
52
- def connect_unix(options = {})
53
- path = options.fetch(:path)
54
- timeout = options.fetch(:timeout, 1.0)
55
-
56
- socket = ::Socket.unix(path)
57
- new(socket, timeout)
52
+ def tcp_socket(host, port, connect_timeout)
53
+ Socket.tcp(host, port, connect_timeout: connect_timeout).tap do |sock|
54
+ sock.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
55
+ end
58
56
  end
59
57
  end
60
58
 
@@ -72,15 +70,16 @@ module Oxblood
72
70
  write(Protocol.build_command(command))
73
71
  end
74
72
 
75
- # FIXME: docs
76
- def write(command)
77
- @socket.write(command)
73
+ # Write data to socket
74
+ # @param [#to_s] data given
75
+ # @return [Integer] the number of bytes written
76
+ def write(data)
77
+ @socket.write(data)
78
78
  end
79
79
 
80
80
  # Send command to Redis server and read response from it
81
81
  # @example run_command(['PING']) => PONG
82
82
  # @param [Array] command Array of command name with it's args
83
- # @return #FIXME
84
83
  def run_command(command)
85
84
  send_command(command)
86
85
  read_response
@@ -129,7 +128,8 @@ module Oxblood
129
128
  Protocol.parse(self)
130
129
  end
131
130
 
132
- # FIXME: docs
131
+ # Read several responses from server
132
+ # (see #read_response)
133
133
  def read_responses(n)
134
134
  Array.new(n) { read_response }
135
135
  end
@@ -1,4 +1,3 @@
1
- require 'oxblood/connection'
2
1
  require 'oxblood/command'
3
2
 
4
3
  module Oxblood
@@ -185,6 +184,28 @@ module Oxblood
185
184
 
186
185
  # ------------------ Connection ---------------------
187
186
 
187
+ # Authenticate to the server
188
+ # @see http://redis.io/commands/auth
189
+ #
190
+ # @param [String] password
191
+ #
192
+ # @return [String] 'OK'
193
+ def auth(password)
194
+ run(cmd.auth(password))
195
+ end
196
+
197
+ # Like {#auth}, except that if error returned, raises it.
198
+ #
199
+ # @param [String] password
200
+ #
201
+ # @raise [Protocol::RError] if error returned
202
+ #
203
+ # @return [String] 'OK'
204
+ def auth!(password)
205
+ response = auth(password)
206
+ error?(response) ? (raise response) : response
207
+ end
208
+
188
209
  # Returns PONG if no argument is provided, otherwise return a copy of
189
210
  # the argument as a bulk
190
211
  # @see http://redis.io/commands/ping
@@ -196,6 +217,16 @@ module Oxblood
196
217
  run(cmd.ping(message))
197
218
  end
198
219
 
220
+ # Change the selected database for the current connection
221
+ # @see http://redis.io/commands/select
222
+ #
223
+ # @param [Integer] index database to switch
224
+ #
225
+ # @return [String] 'OK'
226
+ def select(index)
227
+ run(cmd.select(index))
228
+ end
229
+
199
230
  # ------------------ Server ---------------------
200
231
 
201
232
  # Returns information and statistics about the server in a format that is
@@ -204,10 +235,7 @@ module Oxblood
204
235
  #
205
236
  # @param [String] section used to select a specific section of information
206
237
  def info(section = nil)
207
- command = [:INFO]
208
- command << section if section
209
-
210
- response = run(cmd.info(section))
238
+ run(cmd.info(section))
211
239
  # FIXME: Parse response
212
240
  end
213
241
 
@@ -304,5 +332,11 @@ module Oxblood
304
332
  @connection.write(command)
305
333
  @connection.read_response
306
334
  end
335
+
336
+ private
337
+
338
+ def error?(response)
339
+ Protocol::RError === response
340
+ end
307
341
  end
308
342
  end
@@ -1,3 +1,3 @@
1
1
  module Oxblood
2
- VERSION = '0.1.0.dev2'
2
+ VERSION = '0.1.0.dev3'
3
3
  end
data/tmp/.keep ADDED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxblood
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.dev2
4
+ version: 0.1.0.dev3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Konstantin Shabanov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-10 00:00:00.000000000 Z
11
+ date: 2016-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool
@@ -123,6 +123,7 @@ files:
123
123
  - lib/oxblood/version.rb
124
124
  - lib/redis/connection/oxblood.rb
125
125
  - oxblood.gemspec
126
+ - tmp/.keep
126
127
  homepage: https://github.com/etehtsea/oxblood
127
128
  licenses:
128
129
  - MIT