Class: Replit::Database::Client
- Inherits:
-
Object
- Object
- Replit::Database::Client
- Defined in:
- lib/replit/database/client.rb
Overview
The Replit Database Client.
Instance Method Summary collapse
-
#delete(key) ⇒ Object
Deletes a key.
-
#delete_multiple(keys = []) ⇒ Object
Delete multiple entries by keys.
-
#empty ⇒ Object
Clears the database.
-
#get(key, options = { raw: false }) ⇒ String, Object
Gets a key.
-
#get_all ⇒ Hash<String, Object>
Get all key/value pairs and return as an object.
-
#initialize(custom_url = nil) ⇒ Client
constructor
Create a new ReplitDb::Client.
-
#list(prefix = "") ⇒ Array<String>
List key starting with a prefix or list all.
-
#set(key, value, options = { raw: false }) ⇒ Object
Sets a key.
-
#set_all(obj = {}) ⇒ Object
Sets the entire database through an object.
Constructor Details
#initialize(custom_url = nil) ⇒ Client
Create a new ReplitDb::Client.
19 20 21 22 |
# File 'lib/replit/database/client.rb', line 19 def initialize(custom_url = nil) @database_url = ENV["REPLIT_DB_URL"] if ENV["REPLIT_DB_URL"] @database_url = custom_url if custom_url end |
Instance Method Details
#delete(key) ⇒ Object
Deletes a key.
64 65 66 67 68 |
# File 'lib/replit/database/client.rb', line 64 def delete(key) verify_connection_url Net::HTTP.delete(URI("#{@database_url}/#{key}")) end |
#delete_multiple(keys = []) ⇒ Object
Delete multiple entries by keys
127 128 129 130 131 |
# File 'lib/replit/database/client.rb', line 127 def delete_multiple(keys = []) verify_connection_url keys.each { |key| delete key } end |
#empty ⇒ Object
Clears the database.
92 93 94 95 96 |
# File 'lib/replit/database/client.rb', line 92 def empty verify_connection_url list.each { |key| delete key } end |
#get(key, options = { raw: false }) ⇒ String, Object
Gets a key.
33 34 35 36 37 38 39 40 41 |
# File 'lib/replit/database/client.rb', line 33 def get(key, = { raw: false }) verify_connection_url raw_value = Net::HTTP.get(URI("#{@database_url}/#{key}")) return nil if raw_value.empty? return raw_value if [:raw] json_parse(raw_value, key) end |
#get_all ⇒ Hash<String, Object>
Get all key/value pairs and return as an object.
103 104 105 106 107 108 109 |
# File 'lib/replit/database/client.rb', line 103 def get_all verify_connection_url output = {} list.each { |key| output[key.to_sym] = get key } output end |
#list(prefix = "") ⇒ Array<String>
List key starting with a prefix or list all.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/replit/database/client.rb', line 77 def list(prefix = "") verify_connection_url response = Net::HTTP.get( URI("#{@database_url}?encode=true&prefix=#{CGI.escape(prefix)}") ) return [] if response.empty? response.split("\n").map { |l| CGI.unescape(l) } end |
#set(key, value, options = { raw: false }) ⇒ Object
Sets a key.
51 52 53 54 55 56 57 |
# File 'lib/replit/database/client.rb', line 51 def set(key, value, = { raw: false }) verify_connection_url json_value = [:raw] ? value : value.to_json payload = "#{CGI.escape(key.to_s)}=#{CGI.escape(json_value)}" Net::HTTP.post(URI(@database_url), payload) end |
#set_all(obj = {}) ⇒ Object
Sets the entire database through an object.
116 117 118 119 120 |
# File 'lib/replit/database/client.rb', line 116 def set_all(obj = {}) verify_connection_url obj.each_key { |key| set(key, obj[key]) } end |