repldb 0.0.1

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 +7 -0
  2. data/LICENSE +7 -0
  3. data/README.md +47 -0
  4. data/bin/repldb +3 -0
  5. data/lib/repldb.rb +95 -0
  6. data/test/test.rb +58 -0
  7. metadata +49 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 555046f8b9c45e67ec2710b34180215a5ee0ffe93ad54a32f6451073f53063e8
4
+ data.tar.gz: 20c7791fc02f750cc276ecf163823c61948baf6d89b4acb7be57a30326c22909
5
+ SHA512:
6
+ metadata.gz: 00a4bef890f53bc559ae892fc0451ee0e09b59b9d3c0c5c8c1912b633d920d49663250f153044f6e6739573c47593378c17629bf749b2b728834c8b885a4fe7e
7
+ data.tar.gz: e6771ecaf474ceb727eb3cd17d7f3fabb6cdf5decc0d47653fa4b61730c1751171226673251cd6c9aaf2d1c483f9735759c7ae55eaefd565bc9489e33abca2e0
data/LICENSE ADDED
@@ -0,0 +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
+
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 ADDED
@@ -0,0 +1,47 @@
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)`
47
+ Check if `key` exists.
data/bin/repldb ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts 'hello!'
data/lib/repldb.rb ADDED
@@ -0,0 +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
data/test/test.rb ADDED
@@ -0,0 +1,58 @@
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!"
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: repldb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Coder100
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple client for replit db
14
+ email:
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - bin/repldb
22
+ - lib/repldb.rb
23
+ - test/test.rb
24
+ homepage: https://docs.replit.com/misc/database
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.7.6
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Ruby Repldb!
48
+ test_files:
49
+ - test/test.rb