class Replit::Database::Client
Public Class Methods
Create a new ReplitDb::Client.
@param [String] custom_url optional custom URL
# 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
Public Instance Methods
Deletes a key.
@param [String] key Key.
# File lib/replit/database/client.rb, line 64 def delete(key) verify_connection_url Net::HTTP.delete(URI("#{@database_url}/#{key}")) end
Delete multiple entries by keys
@param [Array<String>] keys Keys.
# File lib/replit/database/client.rb, line 127 def delete_multiple(keys = []) verify_connection_url keys.each { |key| delete key } end
Clears the database.
# File lib/replit/database/client.rb, line 92 def empty verify_connection_url list.each { |key| delete key } end
Gets a key.
@param [String, Symbol] key Key. @param [Hash] options Options Hash. @option options [Boolean] :raw Makes it so that we return the raw string value. Default is false.
@return [String,Object] the stored value.
# File lib/replit/database/client.rb, line 33 def get(key, options = { raw: false }) verify_connection_url raw_value = Net::HTTP.get(URI("#{@database_url}/#{key}")) return nil if raw_value.empty? return raw_value if options[:raw] json_parse(raw_value, key) end
Get all key/value pairs and return as an object.
@return [Hash<String, Object>] Hash with all objects in database.
# 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 key starting with a prefix or list all.
@param [String] prefix Filter keys starting with prefix.
@return [Array<String>] keys in database.
# 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
Sets a key.
@param [String, Symbol] key Key. @param [Object] value Value. @param [Hash] options Options Hash. @option options [Boolean] :raw Makes it so that we store the raw string value. Default is false.
# File lib/replit/database/client.rb, line 51 def set(key, value, options = { raw: false }) verify_connection_url json_value = options[:raw] ? value : value.to_json payload = "#{CGI.escape(key.to_s)}=#{CGI.escape(json_value)}" Net::HTTP.post(URI(@database_url), payload) end
Sets the entire database through an object.
@param [Hash] obj The object.
# File lib/replit/database/client.rb, line 116 def set_all(obj = {}) verify_connection_url obj.each_key { |key| set(key, obj[key]) } end
Private Instance Methods
# File lib/replit/database/client.rb, line 141 def json_parse(string, key) JSON.parse(string, { symbolize_names: true }) rescue JSON::ParserError raise Replit::Database::SyntaxError, "Failed to parse value of #{ key }, try passing a raw option to get the raw value" end
# File lib/replit/database/client.rb, line 135 def verify_connection_url error = Replit::Database::ConfigurationError.new "Missing database connection url" raise error unless @database_url raise error if @database_url.empty? end