Module: NurseAndrea::PlatformDetector

Defined in:
lib/nurse_andrea/platform_detector.rb

Constant Summary collapse

PLATFORMS =
{
  railway:      -> { ENV.key?("RAILWAY_ENVIRONMENT") },
  render:       -> { ENV.key?("RENDER") },
  fly:          -> { ENV.key?("FLY_APP_NAME") },
  heroku:       -> { ENV.key?("DYNO") },
  digitalocean: -> { ENV.key?("DIGITALOCEAN_APP_PLATFORM_COMPONENT_NAME") },
  vercel:       -> { ENV.key?("VERCEL") }
}.freeze

Class Method Summary collapse

Class Method Details

.contextObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nurse_andrea/platform_detector.rb', line 31

def self.context
  platform = detect
  ctx = { platform: platform }

  case platform
  when "railway"
    ctx[:region] = ENV["RAILWAY_REGION"] if ENV.key?("RAILWAY_REGION")
    ctx[:environment] = ENV["RAILWAY_ENVIRONMENT"] if ENV.key?("RAILWAY_ENVIRONMENT")
  when "render"
    ctx[:region] = ENV["RENDER_REGION"] if ENV.key?("RENDER_REGION")
  when "fly"
    ctx[:region] = ENV["FLY_REGION"] if ENV.key?("FLY_REGION")
  when "heroku"
    ctx[:dyno_type] = ENV["DYNO"]&.gsub(/\.\d+$/, "") if ENV.key?("DYNO")
  end

  ctx
end

.detectObject



16
17
18
19
# File 'lib/nurse_andrea/platform_detector.rb', line 16

def self.detect
  PLATFORMS.each { |name, check| return name.to_s if check.call }
  "unknown"
end

.environment_variable_nameObject

The env-var NAME (never its value) that supplies the platform’s environment tier, when the detected platform exposes one. Used by EnvironmentDetector to tag the resolution source. nil when the platform has no environment-name var.



25
26
27
28
29
# File 'lib/nurse_andrea/platform_detector.rb', line 25

def self.environment_variable_name
  case detect
  when "railway" then "RAILWAY_ENVIRONMENT"
  end
end