Module: Heatwave::AppType

Defined in:
lib/heatwave/app_type.rb

Overview

Per-request "what subdomain are we serving?" lookup that drives
subdomain-specific routing, layouts, and middleware. Replaces the
historical boot-time WWW=true / CRM=true env-var split with a
single code path that infers the active app from the incoming
request. See file header for full rationale.

Constant Summary collapse

TYPES =

Types.

%i[www crm api mcp media unknown].freeze

Class Method Summary collapse

Class Method Details

.api?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/heatwave/app_type.rb', line 72

def self.api?
  current == :api
end

.crm?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/heatwave/app_type.rb', line 76

def self.crm?
  current == :crm
end

.currentObject

Thread-local storage for current request's app type
Set by middleware early in the stack, read by controllers/services



30
31
32
# File 'lib/heatwave/app_type.rb', line 30

def self.current
  RequestStore.store[:heatwave_app_type] || :unknown
end

.current=(type) ⇒ Object



34
35
36
# File 'lib/heatwave/app_type.rb', line 34

def self.current=(type)
  RequestStore.store[:heatwave_app_type] = type
end

.detect_from_host(host) ⇒ Object

Detect app type from host string
Extracted for testability



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/heatwave/app_type.rb', line 47

def self.detect_from_host(host)
  return :unknown if host.blank?

  subdomain = host.split('.').first&.downcase

  case subdomain
  when 'mcp'
    :mcp
  when 'api'
    :api
  when 'crm'
    :crm
  when 'media'
    :media
  else
    # Handle www subdomain, no subdomain (warmlyyours.com), or unknown subdomains
    :www
  end
end

.from_request(env) ⇒ Object

Detect app type from Rack env (request)
This is the primary detection method used by middleware



40
41
42
43
# File 'lib/heatwave/app_type.rb', line 40

def self.from_request(env)
  host = extract_host(env)
  detect_from_host(host)
end

.mcp?Boolean

Check if current request is for a specific app type

Returns:

  • (Boolean)


68
69
70
# File 'lib/heatwave/app_type.rb', line 68

def self.mcp?
  current == :mcp
end

.media?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/heatwave/app_type.rb', line 84

def self.media?
  current == :media
end

.run_for?(env, *included_types) ⇒ Boolean

For middleware that should only run on certain app types
Returns true if the middleware should run

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/heatwave/app_type.rb', line 97

def self.run_for?(env, *included_types)
  app_type = from_request(env)
  included_types.include?(app_type)
end

.skip_for?(env, *excluded_types) ⇒ Boolean

For middleware that should only run on certain app types
Returns true if the middleware should be skipped

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/heatwave/app_type.rb', line 90

def self.skip_for?(env, *excluded_types)
  app_type = from_request(env)
  excluded_types.include?(app_type)
end

.www?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/heatwave/app_type.rb', line 80

def self.www?
  current == :www
end