Class: Runbox::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, api_key: nil, timeout: nil, open_timeout: nil) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
# File 'lib/runbox/client.rb', line 10

def initialize(url: nil, api_key: nil, timeout: nil, open_timeout: nil)
  config = Runbox.configuration
  @url = url || config.url
  @api_key = api_key || config.api_key
  @timeout = timeout || config.timeout
  @open_timeout = open_timeout || config.open_timeout

  validate_configuration!
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/runbox/client.rb', line 8

def api_key
  @api_key
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/runbox/client.rb', line 8

def url
  @url
end

Instance Method Details

#delete_containers(identifier) ⇒ Object



68
69
70
71
# File 'lib/runbox/client.rb', line 68

def delete_containers(identifier)
  response = delete("/v1/containers/#{identifier}")
  response["deleted"] || []
end

#healthObject



73
74
75
76
77
78
79
# File 'lib/runbox/client.rb', line 73

def health
  response = get("/v1/health", auth: false)
  {
    status: response["status"],
    version: response["version"]
  }
end

#run(container_id:, files:, run_command:, env: {}, timeout: nil, new_dependencies: nil) ⇒ Result

Run code in a container that was set up via setup()

Parameters:

  • container_id (String)

    Container ID from setup response

  • files (Array<Hash>)

    Files to write before running

  • run_command (String)

    Command to execute

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

    Runtime environment variables

  • timeout (Integer) (defaults to: nil)

    Timeout in seconds

  • new_dependencies (Array<String>) (defaults to: nil)

    New dependencies to install before running

Returns:

  • (Result)

    Run result with stdout, stderr, exit_code, and optional packages



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/runbox/client.rb', line 53

def run(container_id:, files:, run_command:, env: {}, timeout: nil, new_dependencies: nil)
  payload = {
    container_id: container_id,
    files: normalize_files(files),
    run_command: run_command,
    env: env
  }

  payload[:timeout] = timeout if timeout
  payload[:new_dependencies] = new_dependencies if new_dependencies

  response = post("/v1/run", payload)
  Result.new(response)
end

#setup(identifier:, language:, env: {}, timeout: nil, memory: nil, network_allow: nil) ⇒ SetupResult

Set up a container and get environment information

Parameters:

  • identifier (String)

    Unique identifier for container reuse

  • language (String)

    Programming language (python, ruby, shell)

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

    Environment variables to set

  • timeout (Integer) (defaults to: nil)

    Default timeout in seconds

  • memory (String) (defaults to: nil)

    Memory limit (e.g., "256m")

  • network_allow (Array<String>) (defaults to: nil)

    Allowed network destinations

Returns:

  • (SetupResult)

    Setup result with container_id and environment_snapshot



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/runbox/client.rb', line 29

def setup(identifier:, language:, env: {}, timeout: nil, memory: nil, network_allow: nil)
  payload = {
    identifier: identifier,
    language: language,
    env: env
  }

  payload[:timeout] = timeout if timeout
  payload[:memory] = memory if memory
  payload[:network_allow] = network_allow if network_allow

  response = post("/v1/setup", payload)
  SetupResult.new(response)
end