Class: RelayGrid::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/relaygrid/configuration.rb

Overview

Settings a Client reads. RelayGrid.configure sets the process-wide default; Client.new(api_key: ...) takes per-client overrides so a multi-account host app can talk to several accounts at once.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://relaygrid.dev"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, base_url: nil, timeout: 30, open_timeout: 10, logger: nil, ca_file: nil, ws_url: nil, max_retries: 3) ⇒ Configuration

Returns a new instance of Configuration.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/relaygrid/configuration.rb', line 33

def initialize(api_key: nil, base_url: nil, timeout: 30, open_timeout: 10, logger: nil,
               ca_file: nil, ws_url: nil, max_retries: 3)
  @api_key = api_key
  @base_url = base_url || DEFAULT_BASE_URL
  @timeout = timeout
  @open_timeout = open_timeout
  @logger = logger
  @ca_file = ca_file
  @ws_url = ws_url
  @max_retries = max_retries
end

Instance Attribute Details

#api_keyString

Returns account API key ("sk_...").

Returns:

  • (String)

    account API key ("sk_...")



13
14
15
# File 'lib/relaygrid/configuration.rb', line 13

def api_key
  @api_key
end

#base_urlString

Returns API origin, e.g. "https://relaygrid.dev".

Returns:



15
16
17
# File 'lib/relaygrid/configuration.rb', line 15

def base_url
  @base_url
end

#ca_fileString?

Returns PEM bundle for a private CA. TLS certificates are always verified; this only adds trust anchors (self-signed local dev, internal deployments). There is deliberately no switch to turn verification off -- this client carries an API key and relays push channel tokens, so an unverified connection is never the right default.

Returns:

  • (String, nil)

    PEM bundle for a private CA. TLS certificates are always verified; this only adds trust anchors (self-signed local dev, internal deployments). There is deliberately no switch to turn verification off -- this client carries an API key and relays push channel tokens, so an unverified connection is never the right default.



27
28
29
# File 'lib/relaygrid/configuration.rb', line 27

def ca_file
  @ca_file
end

#loggerLogger?

Returns when set, requests are logged (headers redacted).

Returns:

  • (Logger, nil)

    when set, requests are logged (headers redacted)



21
22
23
# File 'lib/relaygrid/configuration.rb', line 21

def logger
  @logger
end

#max_retriesInteger

Returns retry attempts for idempotent (GET) requests.

Returns:

  • (Integer)

    retry attempts for idempotent (GET) requests



31
32
33
# File 'lib/relaygrid/configuration.rb', line 31

def max_retries
  @max_retries
end

#open_timeoutInteger

Returns seconds to wait for the connection to open.

Returns:

  • (Integer)

    seconds to wait for the connection to open



19
20
21
# File 'lib/relaygrid/configuration.rb', line 19

def open_timeout
  @open_timeout
end

#timeoutInteger

Returns seconds to wait for a response.

Returns:

  • (Integer)

    seconds to wait for a response



17
18
19
# File 'lib/relaygrid/configuration.rb', line 17

def timeout
  @timeout
end

#ws_urlString?

Returns overrides the Action Cable URL derived from base_url.

Returns:

  • (String, nil)

    overrides the Action Cable URL derived from base_url



29
30
31
# File 'lib/relaygrid/configuration.rb', line 29

def ws_url
  @ws_url
end

Instance Method Details

#cable_urlObject

Action Cable endpoint, derived from base_url unless one was set explicitly ("https://relaygrid.dev" -> "wss://relaygrid.dev/cable").



80
81
82
83
84
85
86
87
88
# File 'lib/relaygrid/configuration.rb', line 80

def cable_url
  return ws_url if ws_url

  uri = URI.parse(base_url.to_s)
  uri.scheme = uri.scheme == "https" ? "wss" : "ws"
  uri.path = "/cable"
  uri.query = nil
  uri.to_s
end

#merge(overrides = {}) ⇒ Object

A copy with the given attributes replaced. Client.new layers per-client overrides over the global default this way, without mutating it.



47
48
49
50
51
52
53
# File 'lib/relaygrid/configuration.rb', line 47

def merge(overrides = {})
  dup.tap do |copy|
    overrides.each do |attribute, value|
      copy.public_send(:"#{attribute}=", value) unless value.nil?
    end
  end
end

#validate!Object

Fail at construction rather than on the first request, so a misconfigured deploy surfaces at boot.

Raises:



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/relaygrid/configuration.rb', line 57

def validate!
  raise ConfigurationError, "api_key is required" if blank?(api_key)
  raise ConfigurationError, "base_url is required" if blank?(base_url)

  uri = begin
    URI.parse(base_url.to_s)
  rescue URI::InvalidURIError
    nil
  end

  unless uri.is_a?(URI::HTTP) && !uri.host.to_s.empty?
    raise ConfigurationError, "base_url must be an http(s) URL, got #{base_url.inspect}"
  end

  if ca_file && !File.readable?(ca_file.to_s)
    raise ConfigurationError, "ca_file is not readable: #{ca_file.inspect}"
  end

  self
end