repldb 0.0.2 → 0.0.3
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/README.md +1 -1
- data/lib/repldb.rb +18 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e725a97d9fad8f4ecc9a6d369f3928876061ba2df9cbf50c0ed40a57687592a9
|
4
|
+
data.tar.gz: bc2729273d394819373134cd044bbb836ee770020f01bccf8b0fa6b979cbefad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a18e7feb555265bed09d7b26fced181fa42e43d2b113276dd2c42b1b0984d55fedbffef7317b0c9ba0cca28a0723eb43431a5a1901f3199f8cb43e867f0e562
|
7
|
+
data.tar.gz: 98be4f7bc5d88f78751c53bc33255be4b9c0956261977232492d3d999edb3ec2ecc88c0e2060131a7549bfa6f46903e65c49e482095ac44f0041b30a960b00e4
|
data/README.md
CHANGED
data/lib/repldb.rb
CHANGED
@@ -4,19 +4,23 @@ require 'json'
|
|
4
4
|
require 'net/http'
|
5
5
|
|
6
6
|
class Client
|
7
|
-
# Change
|
7
|
+
# Change <tt>url</tt> to point to a different path.
|
8
|
+
# @param url [String, nil]
|
8
9
|
def initialize(url = ENV['REPLIT_DB_URL'])
|
9
10
|
@url = url
|
10
11
|
end
|
11
12
|
|
12
|
-
# Sets
|
13
|
+
# Sets <tt>key</tt> to <tt>val</tt>.
|
14
|
+
# @param key [String] the key to set.
|
15
|
+
# @param val [Any] the value.
|
13
16
|
def set(key, val)
|
14
17
|
post_url(@url.to_s, serialize(key) => JSON.generate(val))
|
15
18
|
end
|
16
19
|
|
17
|
-
# Gets
|
18
|
-
#
|
19
|
-
#
|
20
|
+
# Gets <tt>key</tt>.
|
21
|
+
# If it doesn't exist, <tt>default<tt> will be used.
|
22
|
+
# @param key [String] the key to get
|
23
|
+
# @param default [Any] the default value if the key doesn't exist.
|
20
24
|
def get(key, default = nil)
|
21
25
|
val = get_url("#{@url}/#{serialize(key)}")
|
22
26
|
if val.body == ''
|
@@ -31,13 +35,14 @@ class Client
|
|
31
35
|
end
|
32
36
|
|
33
37
|
# Get all the keys as an array.
|
34
|
-
#
|
35
|
-
#
|
38
|
+
# <tt>prefix</tt> can be used to get all keys with a prefix.
|
39
|
+
# @param prefix [String, nil] Get all keys with <tt>prefix</tt> at the start.
|
36
40
|
def keys(prefix = '')
|
37
41
|
get_url("#{@url}?prefix=#{prefix}").body.split("\n").map { |k| deserialize(k) }
|
38
42
|
end
|
39
43
|
|
40
|
-
# Deletes
|
44
|
+
# Deletes <tt>key</tt>.
|
45
|
+
# @param key [String]
|
41
46
|
def del(key)
|
42
47
|
uri = URI.parse("#{@url}/#{serialize(key)}")
|
43
48
|
req = Net::HTTP::Delete.new(uri.path)
|
@@ -52,9 +57,9 @@ class Client
|
|
52
57
|
keys.each { |k| del(k) }
|
53
58
|
end
|
54
59
|
|
55
|
-
# This will append
|
56
|
-
#
|
60
|
+
# This will append <tt>hash</tt>.
|
57
61
|
# **This will NOT replace existing keys!**
|
62
|
+
# @param hash [Hash]
|
58
63
|
def set_hash(hash)
|
59
64
|
if hash.is_a? Hash
|
60
65
|
hash.each { |k, v| set(k, v) }
|
@@ -71,9 +76,10 @@ class Client
|
|
71
76
|
o
|
72
77
|
end
|
73
78
|
|
74
|
-
# Check if
|
79
|
+
# Check if <tt>key</tt> exists.
|
80
|
+
# @param key [String]
|
75
81
|
def exists?(key)
|
76
|
-
|
82
|
+
!get(key).nil?
|
77
83
|
end
|
78
84
|
|
79
85
|
# internals
|