repldb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +6 -6
  3. data/README.md +47 -46
  4. data/bin/repldb +49 -3
  5. data/lib/repldb.rb +95 -95
  6. data/test/test.rb +57 -58
  7. metadata +7 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 555046f8b9c45e67ec2710b34180215a5ee0ffe93ad54a32f6451073f53063e8
4
- data.tar.gz: 20c7791fc02f750cc276ecf163823c61948baf6d89b4acb7be57a30326c22909
3
+ metadata.gz: fca11f6e1224b47560db11dd0616a9957286bc652ef6bbc7920e759ed7ff144a
4
+ data.tar.gz: 272ec9182a7e7d527e5dc321310a1b3a37f7b3510f748e39ee93af76167f5c41
5
5
  SHA512:
6
- metadata.gz: 00a4bef890f53bc559ae892fc0451ee0e09b59b9d3c0c5c8c1912b633d920d49663250f153044f6e6739573c47593378c17629bf749b2b728834c8b885a4fe7e
7
- data.tar.gz: e6771ecaf474ceb727eb3cd17d7f3fabb6cdf5decc0d47653fa4b61730c1751171226673251cd6c9aaf2d1c483f9735759c7ae55eaefd565bc9489e33abca2e0
6
+ metadata.gz: c14295ccf368ae51c4ef37f4e56ded5dc15662f3f1c0e12967ada4ee61ab680c879eca0495649373e2824f460f346a2dddc1e67fc2978d1a5c0e3ef59ac86950
7
+ data.tar.gz: 6b5f072be85b2020dfa7b563d9c507709e966ac1de2b5a78c3a7cde5cbe408350670159a71a3f09e5f3277cb5b6646b23b45691ce8535ec5fabc8c36041b60df
data/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
- Copyright © 2021 Coder100
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
1
+ Copyright © 2021 Coder100
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
7
  THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,47 +1,48 @@
1
- # Ruby Repldb
2
- The ease of using repldb now meets the ease of developing in Ruby!
3
-
4
- # Usage
5
- ```rb
6
- require "repldb"
7
- db = Client.new
8
-
9
- db.set("key", "value")
10
- puts db.get("key") # => value
11
- db.del_all()
12
- ```
13
-
14
- # Docs
15
- ## `class Client`
16
- ### `Client#initialize(url)`
17
- Change `url` to point to a different path.
18
-
19
- ### `Client#set(key, value)`
20
- Sets `key` to `val`.
21
-
22
- ### `Client#get(key, default)`
23
- Gets `key`.
24
-
25
- If it doesn't exist, `default` will be used.
26
-
27
- ### `Client#del(key)`
28
- Deletes `key`.
29
-
30
- ### `Client#keys(prefix)`
31
- Get all the keys as an array.
32
-
33
- `prefix` can be used to get all keys with a prefix.
34
-
35
- ### `Client#del_all()`
36
- Deletes all keys, ultimately emptying the database.
37
-
38
- ### `Client#set_hash(hash)`
39
- This will append `hash`.
40
-
41
- **This will NOT replace existing keys!**
42
-
43
- ### `Client#get_hash()`
44
- Gets all the keys as a hash.
45
-
46
- ### `Client#exists?(key)`
1
+ # Ruby Repldb
2
+ The ease of using repldb now meets the ease of developing in Ruby!
3
+ You can learn more about repldb [here](https://docs.replit.com/misc/database)
4
+
5
+ # Usage
6
+ ```rb
7
+ require "repldb"
8
+ db = Client.new
9
+
10
+ db.set("key", "value")
11
+ puts db.get("key") # => value
12
+ db.del_all()
13
+ ```
14
+
15
+ # Docs
16
+ ## `class Client`
17
+ ### `Client#initialize(url)`
18
+ Change `url` to point to a different path.
19
+
20
+ ### `Client#set(key, value)`
21
+ Sets `key` to `val`.
22
+
23
+ ### `Client#get(key, default)`
24
+ Gets `key`.
25
+
26
+ If it doesn't exist, `default` will be used.
27
+
28
+ ### `Client#del(key)`
29
+ Deletes `key`.
30
+
31
+ ### `Client#keys(prefix)`
32
+ Get all the keys as an array.
33
+
34
+ `prefix` can be used to get all keys with a prefix.
35
+
36
+ ### `Client#del_all()`
37
+ Deletes all keys, ultimately emptying the database.
38
+
39
+ ### `Client#set_hash(hash)`
40
+ This will append `hash`.
41
+
42
+ **This will NOT replace existing keys!**
43
+
44
+ ### `Client#get_hash()`
45
+ Gets all the keys as a hash.
46
+
47
+ ### `Client#exists?(key)`
47
48
  Check if `key` exists.
data/bin/repldb CHANGED
@@ -1,3 +1,49 @@
1
- #!/usr/bin/env ruby
2
-
3
- puts 'hello!'
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'repldb'
4
+
5
+ db = Client.new
6
+
7
+ def colorize(inp)
8
+ inp.gsub(/<(.+?)>/, "<\x1b[93m\\1\x1b[0m>")
9
+ end
10
+
11
+ def help_message
12
+ puts colorize "\x1b[1m% -30sDescription\x1b[0m
13
+ % -30sGets <key>
14
+ % -30sSets <key> to <val>
15
+ % -30sDeletes <key>
16
+ % -30sGets all keys" % ["Name", "repldb get <key>", "repldb set <key> <val>", "repldb del <key>", "repldb all"]
17
+ end
18
+
19
+ a0 = ARGV[0]
20
+ a1 = ARGV[1]
21
+ a2 = ARGV[2]
22
+ len = ARGV.length - 1
23
+
24
+ if !a0
25
+ help_message
26
+ exit 0
27
+ end
28
+
29
+ case a0.downcase
30
+ when 'get'
31
+ if len == 1; pp(db.get(a1))
32
+ else puts colorize "Expected <key>" end
33
+ when 'set'
34
+ if len == 2
35
+ db.set(a1, a2)
36
+ puts "Set '\x1b[1m#{a1}\x1b[0m' to '\x1b[1m#{a2}\x1b[0m'
37
+ (output may have been json)"
38
+ else puts colorize "Expected <key> and <val>" end
39
+ when 'del'
40
+ if len == 1
41
+ db.del(a1)
42
+ puts "Deleted '\x1b[1m#{a1}\x1b[0m'"
43
+ else puts colorize "Expected <key>" end
44
+ when 'all'
45
+ if len == 0; pp db.keys(a1 || '')
46
+ else puts "Usage: \x1b[1mrepldb all\x1b[0m" end
47
+ else
48
+ help_message
49
+ end
data/lib/repldb.rb CHANGED
@@ -1,95 +1,95 @@
1
- require 'cgi'
2
- require 'uri'
3
- require 'json'
4
- require 'net/http'
5
-
6
- class Client
7
- # Change `url` to point to a different path.
8
- def initialize(url = ENV['REPLIT_DB_URL'])
9
- @url = url
10
- end
11
-
12
- # Sets `key` to `val`.
13
- def set(key, val)
14
- post_url(@url.to_s, serialize(key) => JSON.generate(val))
15
- end
16
-
17
- # Gets `key`.
18
- #
19
- # If it doesn't exist, `default` will be used.
20
- def get(key, default = nil)
21
- val = get_url("#{@url}/#{serialize(key)}")
22
- if val.body == ''
23
- default
24
- else
25
- begin
26
- JSON.parse(val.body)
27
- rescue StandardError
28
- default
29
- end
30
- end
31
- end
32
-
33
- # Get all the keys as an array.
34
- #
35
- # `prefix` can be used to get all keys with a prefix.
36
- def keys(prefix = '')
37
- get_url("#{@url}?prefix=#{prefix}").body.split("\n").map { |k| deserialize(k) }
38
- end
39
-
40
- # Deletes `key`.
41
- def del(key)
42
- uri = URI.parse("#{@url}/#{serialize(key)}")
43
- req = Net::HTTP::Delete.new(uri.path)
44
-
45
- http = Net::HTTP.new(uri.host, uri.port)
46
- http.use_ssl = true
47
- res = http.request(req)
48
- end
49
-
50
- # Deletes all keys, ultimately emptying the database.
51
- def del_all
52
- keys.each { |k| del(k) }
53
- end
54
-
55
- # This will append `hash`.
56
- #
57
- # **This will NOT replace existing keys!**
58
- def set_hash(hash)
59
- if hash.is_a? Hash
60
- hash.each { |k, v| set(k, v) }
61
- else
62
- raise '[GemDb] Error: Argument must be a hash'
63
- end
64
- end
65
-
66
- # Gets all the keys as a hash.
67
- def get_hash
68
- o = {}
69
- ks = keys
70
- ks.each { |k| o[k] = get(k) }
71
- o
72
- end
73
-
74
- # Check if `key` exists.
75
- def exists?(key)
76
- not get(key).nil?
77
- end
78
-
79
- # internals
80
- private def post_url(url, data)
81
- Net::HTTP.post_form(URI(url), data)
82
- end
83
-
84
- private def get_url(url)
85
- Net::HTTP.get_response(URI(url))
86
- end
87
-
88
- private def serialize(key)
89
- CGI.escape key.to_s
90
- end
91
-
92
- private def deserialize(key)
93
- CGI.unescape key.to_s
94
- end
95
- end
1
+ require 'cgi'
2
+ require 'uri'
3
+ require 'json'
4
+ require 'net/http'
5
+
6
+ class Client
7
+ # Change `url` to point to a different path.
8
+ def initialize(url = ENV['REPLIT_DB_URL'])
9
+ @url = url
10
+ end
11
+
12
+ # Sets `key` to `val`.
13
+ def set(key, val)
14
+ post_url(@url.to_s, serialize(key) => JSON.generate(val))
15
+ end
16
+
17
+ # Gets `key`.
18
+ #
19
+ # If it doesn't exist, `default` will be used.
20
+ def get(key, default = nil)
21
+ val = get_url("#{@url}/#{serialize(key)}")
22
+ if val.body == ''
23
+ default
24
+ else
25
+ begin
26
+ JSON.parse(val.body)
27
+ rescue StandardError
28
+ default
29
+ end
30
+ end
31
+ end
32
+
33
+ # Get all the keys as an array.
34
+ #
35
+ # `prefix` can be used to get all keys with a prefix.
36
+ def keys(prefix = '')
37
+ get_url("#{@url}?prefix=#{prefix}").body.split("\n").map { |k| deserialize(k) }
38
+ end
39
+
40
+ # Deletes `key`.
41
+ def del(key)
42
+ uri = URI.parse("#{@url}/#{serialize(key)}")
43
+ req = Net::HTTP::Delete.new(uri.path)
44
+
45
+ http = Net::HTTP.new(uri.host, uri.port)
46
+ http.use_ssl = true
47
+ http.request(req)
48
+ end
49
+
50
+ # Deletes all keys, ultimately emptying the database.
51
+ def del_all
52
+ keys.each { |k| del(k) }
53
+ end
54
+
55
+ # This will append `hash`.
56
+ #
57
+ # **This will NOT replace existing keys!**
58
+ def set_hash(hash)
59
+ if hash.is_a? Hash
60
+ hash.each { |k, v| set(k, v) }
61
+ else
62
+ raise '[GemDb] Error: Argument must be a hash'
63
+ end
64
+ end
65
+
66
+ # Gets all the keys as a hash.
67
+ def get_hash
68
+ o = {}
69
+ ks = keys
70
+ ks.each { |k| o[k] = get(k) }
71
+ o
72
+ end
73
+
74
+ # Check if `key` exists.
75
+ def exists?(key)
76
+ not get(key).nil?
77
+ end
78
+
79
+ # internals
80
+ private def post_url(url, data)
81
+ Net::HTTP.post_form(URI(url), data)
82
+ end
83
+
84
+ private def get_url(url)
85
+ Net::HTTP.get_response(URI(url))
86
+ end
87
+
88
+ private def serialize(key)
89
+ CGI.escape key.to_s
90
+ end
91
+
92
+ private def deserialize(key)
93
+ CGI.unescape key.to_s
94
+ end
95
+ end
data/test/test.rb CHANGED
@@ -1,58 +1,57 @@
1
- require 'lib/repldb'
2
-
3
- class AssertionError < RuntimeError
4
- end
5
-
6
-
7
- def assert
8
- raise AssertionError unless yield
9
- end
10
-
11
- class TestDb
12
- def initialize; end
13
-
14
- def set_bool
15
- db = Client.new
16
- db.set('bool_val', true)
17
- assert { db.get('bool_val') == true }
18
- db.del_all
19
- end
20
-
21
- def set_str
22
- db = Client.new
23
- db.set('str_val', 's')
24
- assert { db.get('str_val') == 's' }
25
- db.del_all
26
- end
27
-
28
- def set_int
29
- db = Client.new
30
- db.set('int_val', 5)
31
- assert { db.get('int_val') == 5 }
32
- db.del_all
33
- end
34
-
35
- def del
36
- db = Client.new
37
- db.set('del_val', 3)
38
- db.del('del_val')
39
- assert { db.exists?('del_val') == false }
40
- end
41
-
42
- def hash
43
- db = Client.new
44
- db.set('key', 3)
45
- db.set('test', 4)
46
- assert { db.get_hash == { 'key' => 3, 'test' => 4 } }
47
- db.del_all
48
- end
49
- end
50
-
51
- a = TestDb.new
52
- a.set_bool
53
- a.set_int
54
- a.set_str
55
- a.del
56
- a.hash
57
-
58
- puts "Tests successful!"
1
+ require 'lib/repldb'
2
+
3
+ class AssertionError < RuntimeError
4
+ end
5
+
6
+ def assert
7
+ raise AssertionError unless yield
8
+ end
9
+
10
+ class TestDb
11
+ def initialize; end
12
+
13
+ def set_bool
14
+ db = Client.new
15
+ db.set('bool_val', true)
16
+ assert { db.get('bool_val') == true }
17
+ db.del_all
18
+ end
19
+
20
+ def set_str
21
+ db = Client.new
22
+ db.set('str_val', 's')
23
+ assert { db.get('str_val') == 's' }
24
+ db.del_all
25
+ end
26
+
27
+ def set_int
28
+ db = Client.new
29
+ db.set('int_val', 5)
30
+ assert { db.get('int_val') == 5 }
31
+ db.del_all
32
+ end
33
+
34
+ def del
35
+ db = Client.new
36
+ db.set('del_val', 3)
37
+ db.del('del_val')
38
+ assert { db.exists?('del_val') == false }
39
+ end
40
+
41
+ def hash
42
+ db = Client.new
43
+ db.set('key', 3)
44
+ db.set('test', 4)
45
+ assert { db.get_hash == { 'key' => 3, 'test' => 4 } }
46
+ db.del_all
47
+ end
48
+ end
49
+
50
+ a = TestDb.new
51
+ a.set_bool
52
+ a.set_int
53
+ a.set_str
54
+ a.del
55
+ a.hash
56
+
57
+ puts "Tests successful!"
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repldb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Coder100
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2021-03-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A simple client for replit db
14
- email:
14
+ email:
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
@@ -21,11 +21,11 @@ files:
21
21
  - bin/repldb
22
22
  - lib/repldb.rb
23
23
  - test/test.rb
24
- homepage: https://docs.replit.com/misc/database
24
+ homepage: https://github.com/cursorweb/rubygem-repldb
25
25
  licenses:
26
26
  - MIT
27
27
  metadata: {}
28
- post_install_message:
28
+ post_install_message:
29
29
  rdoc_options: []
30
30
  require_paths:
31
31
  - lib
@@ -40,9 +40,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
40
  - !ruby/object:Gem::Version
41
41
  version: '0'
42
42
  requirements: []
43
- rubyforge_project:
44
- rubygems_version: 2.7.6
45
- signing_key:
43
+ rubygems_version: 3.1.4
44
+ signing_key:
46
45
  specification_version: 3
47
46
  summary: Ruby Repldb!
48
47
  test_files: