Class: Requestkit::Storage

Inherits:
Object
  • Object
show all
Defined in:
lib/requestkit/storage.rb

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Storage

Returns a new instance of Storage.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/requestkit/storage.rb', line 8

def initialize(config)
  database_path = (config.storage == "file") ? config.database_path : ":memory:"

  if config.storage == "file"
    FileUtils.mkdir_p(File.dirname(config.database_path))
  end

  @db = SQLite3::Database.new(database_path)
  @db.results_as_hash = true

  setup!
end

Instance Method Details

#allObject



28
# File 'lib/requestkit/storage.rb', line 28

def all = @db.execute("SELECT * FROM requests ORDER BY id DESC")

#by_namespace(namespace) ⇒ Object



32
# File 'lib/requestkit/storage.rb', line 32

def by_namespace(namespace) = @db.execute("SELECT * FROM requests WHERE namespace = ? ORDER BY id DESC", [namespace])

#clearObject



34
# File 'lib/requestkit/storage.rb', line 34

def clear = @db.execute("DELETE FROM requests")

#namespacesObject



30
# File 'lib/requestkit/storage.rb', line 30

def namespaces = @db.execute("SELECT DISTINCT namespace FROM requests ORDER BY namespace").map { |row| row["namespace"] }

#store(namespace:, method:, path:, request:, timestamp:, direction: "inbound", response: nil, status: nil, parent_id: nil) ⇒ Object



21
22
23
24
25
26
# File 'lib/requestkit/storage.rb', line 21

def store(namespace:, method:, path:, request:, timestamp:, direction: "inbound", response: nil, status: nil, parent_id: nil)
  @db.execute(
    "INSERT INTO requests (namespace, direction, method, path, request, response, status, parent_id, timestamp) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
    [namespace, direction, method, path, request, response, status, parent_id, timestamp]
  )
end