Module: Seam::Http::Options

Defined in:
lib/seam/options.rb,
lib/seam/parse_options.rb

Defined Under Namespace

Classes: SeamInvalidOptionsError

Class Method Summary collapse

Class Method Details

.get_api_key_from_env(personal_access_token) ⇒ Object

A personal access token passed as an option takes precedence over the environment, so the environment is not consulted when one is given.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/seam/parse_options.rb', line 43

def self.get_api_key_from_env(personal_access_token)
  return nil unless personal_access_token.nil?

  api_key = ENV["SEAM_API_KEY"]

  if api_key && ENV["SEAM_PERSONAL_ACCESS_TOKEN"]
    raise SeamInvalidOptionsError.new(
      "Both SEAM_API_KEY and SEAM_PERSONAL_ACCESS_TOKEN environment variables are defined. " \
      "Please use only one authentication method."
    )
  end

  api_key
end

.get_endpoint(endpoint = nil) ⇒ Object



8
9
10
# File 'lib/seam/options.rb', line 8

def self.get_endpoint(endpoint = nil)
  endpoint || get_endpoint_from_env || Seam::DEFAULT_ENDPOINT
end

.get_endpoint_from_envObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/seam/options.rb', line 12

def self.get_endpoint_from_env
  seam_api_url = ENV["SEAM_API_URL"]
  seam_endpoint = ENV["SEAM_ENDPOINT"]

  if seam_api_url
    warn "\033[93mUsing the SEAM_API_URL environment variable is deprecated. Support will be removed in a later major version. Use SEAM_ENDPOINT instead.\033[0m"
  end

  if seam_api_url && seam_endpoint
    warn "\033[93mDetected both the SEAM_API_URL and SEAM_ENDPOINT environment variables. Using SEAM_ENDPOINT.\033[0m"
  end

  seam_endpoint || seam_api_url
end

.get_personal_access_token_from_env(api_key) ⇒ Object

An api_key, whether passed as an option or read from the environment, takes precedence, so the environment is not consulted when one is set.



60
61
62
63
64
# File 'lib/seam/parse_options.rb', line 60

def self.get_personal_access_token_from_env(api_key)
  return nil unless api_key.nil?

  ENV["SEAM_PERSONAL_ACCESS_TOKEN"]
end

.parse_options(api_key: nil, personal_access_token: nil, workspace_id: nil, endpoint: nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/seam/parse_options.rb', line 9

def self.parse_options(api_key: nil, personal_access_token: nil, workspace_id: nil, endpoint: nil)
  api_key ||= get_api_key_from_env(personal_access_token)
  personal_access_token ||= get_personal_access_token_from_env(api_key)
  workspace_id ||= ENV["SEAM_WORKSPACE_ID"]

  auth_headers = Http::Auth.get_auth_headers(
    api_key: api_key,
    personal_access_token: personal_access_token,
    workspace_id: workspace_id
  )
  endpoint = Http::Options.get_endpoint(endpoint)

  {auth_headers: auth_headers, endpoint: endpoint}
end

.parse_without_workspace_options(personal_access_token: nil, endpoint: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/seam/parse_options.rb', line 24

def self.parse_without_workspace_options(personal_access_token: nil, endpoint: nil)
  personal_access_token ||= ENV["SEAM_PERSONAL_ACCESS_TOKEN"]

  if personal_access_token.nil?
    raise SeamInvalidOptionsError.new(
      "Must specify a personal_access_token. " \
      "Attempted reading configuration from the environment, " \
      "but the environment variable SEAM_PERSONAL_ACCESS_TOKEN is not set."
    )
  end

  auth_headers = Http::Auth.get_auth_headers_for_without_workspace_personal_access_token(personal_access_token)
  endpoint = Http::Options.get_endpoint(endpoint)

  {auth_headers: auth_headers, endpoint: endpoint}
end

.seam_http_options_with_api_key?(api_key: nil, personal_access_token: nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.seam_http_options_with_api_key?(api_key: nil, personal_access_token: nil)
  return false if api_key.nil?

  if personal_access_token
    raise SeamInvalidOptionsError.new(
      "The personal_access_token option cannot be used with the api_key option"
    )
  end

  true
end

.seam_http_options_with_personal_access_token?(personal_access_token: nil, api_key: nil, workspace_id: nil) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/seam/options.rb', line 45

def self.seam_http_options_with_personal_access_token?(personal_access_token: nil, api_key: nil, workspace_id: nil)
  return false if personal_access_token.nil?

  if api_key
    raise SeamInvalidOptionsError.new(
      "The api_key option cannot be used with the personal_access_token option"
    )
  end

  if workspace_id.nil?
    raise SeamInvalidOptionsError.new(
      "Must pass a workspace_id when using a personal_access_token"
    )
  end

  true
end