Class: Terminalwire::Server::Context

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/terminalwire/server/context.rb

Overview

Contains all of the resources that are accessible to the server on the client-side. It’s the primary interface for the server to interact with the client and is integrated into other libraries like Thor, etc.

Defined Under Namespace

Classes: Env

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter:, entitlement:) ⇒ Context

Returns a new instance of Context.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/terminalwire/server/context.rb', line 23

def initialize(adapter:, entitlement:)
  @adapter = adapter
  @entitlement = entitlement

  # Initialize resources
  @stdout = Resource::STDOUT.new("stdout", @adapter)
  @stdin = Resource::STDIN.new("stdin", @adapter)
  @stderr = Resource::STDERR.new("stderr", @adapter)
  @browser = Resource::Browser.new("browser", @adapter)
  @file = Resource::File.new("file", @adapter)
  @directory = Resource::Directory.new("directory", @adapter)
  @environment_variable = Resource::EnvironmentVariable.new("environment_variable", @adapter)

  # Authority is provided by the client.
  @authority = @entitlement.fetch(:authority)
  # The Terminalwire home path is provided by the client and set
  # as an environment variable.
  @root_path = Pathname.new(
    @environment_variable.read("TERMINALWIRE_HOME")
  )
  # Now derive the rest of the paths from the Terminalwire home path.
  @authority_path = @root_path.join("authorities", @authority)
  @storage_path = @authority_path.join("storage")

  if block_given?
    begin
      yield self
    ensure
      exit
    end
  end
end

Instance Attribute Details

#authorityObject (readonly)

Returns the value of attribute authority.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def authority
  @authority
end

#authority_pathObject (readonly)

Returns the value of attribute authority_path.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def authority_path
  @authority_path
end

#browserObject (readonly)

Returns the value of attribute browser.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def browser
  @browser
end

#directoryObject (readonly)

Returns the value of attribute directory.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def directory
  @directory
end

#environment_variableObject (readonly)

Returns the value of attribute environment_variable.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def environment_variable
  @environment_variable
end

#fileObject (readonly)

Returns the value of attribute file.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def file
  @file
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def root_path
  @root_path
end

#stderrObject (readonly)

Returns the value of attribute stderr.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def stderr
  @stderr
end

#stdinObject (readonly)

Returns the value of attribute stdin.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def stdout
  @stdout
end

#storage_pathObject (readonly)

Returns the value of attribute storage_path.



10
11
12
# File 'lib/terminalwire/server/context.rb', line 10

def storage_path
  @storage_path
end

Instance Method Details

#closeObject



76
77
78
# File 'lib/terminalwire/server/context.rb', line 76

def close
  @adapter.close
end

#ENVObject



68
69
70
# File 'lib/terminalwire/server/context.rb', line 68

def ENV
  @ENV ||= Env.new(context: self)
end

#exit(status = 0) ⇒ Object



72
73
74
# File 'lib/terminalwire/server/context.rb', line 72

def exit(status = 0)
  @adapter.write(event: "exit", status: status)
end