Class: Replit::Database::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/replit/database/client.rb

Overview

The Replit Database Client.

Instance Method Summary collapse

Constructor Details

#initialize(custom_url = nil) ⇒ Client

Create a new ReplitDb::Client.

Parameters:

  • custom_url (String) (defaults to: nil)

    optional custom URL



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.

Parameters:

  • key (String)

    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

Parameters:

  • keys (Array<String>) (defaults to: [])

    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

#emptyObject

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.

Parameters:

  • key (String, Symbol)

    Key.

  • options (Hash) (defaults to: { raw: false })

    Options Hash.

Options Hash (options):

  • :raw (Boolean)

    Makes it so that we return the raw string value. Default is false.

Returns:

  • (String, Object)

    the stored value.



33
34
35
36
37
38
39
40
41
# 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_allHash<String, Object>

Get all key/value pairs and return as an object.

Returns:

  • (Hash<String, Object>)

    Hash with all objects in database.



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.

Parameters:

  • prefix (String) (defaults to: "")

    Filter keys starting with prefix.

Returns:

  • (Array<String>)

    keys in database.



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.

Parameters:

  • key (String, Symbol)

    Key.

  • value (Object)

    Value.

  • options (Hash) (defaults to: { raw: false })

    Options Hash.

Options Hash (options):

  • :raw (Boolean)

    Makes it so that we store the raw string value. Default is false.



51
52
53
54
55
56
57
# 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

#set_all(obj = {}) ⇒ Object

Sets the entire database through an object.

Parameters:

  • obj (Hash) (defaults to: {})

    The 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