Class: DhanHQ::Utils::NetworkInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/DhanHQ/utils/network_inspector.rb

Overview

Collects network and environment metadata for order audit logging.

Results are memoized at class level so that repeated calls within a single process do not incur additional HTTP round-trips to the IP-lookup services.

Examples:

DhanHQ::Utils::NetworkInspector.public_ipv4  # => "122.171.22.40"
DhanHQ::Utils::NetworkInspector.hostname     # => "DESKTOP-SHUBHAM"
DhanHQ::Utils::NetworkInspector.environment  # => "production"

Constant Summary collapse

IPV4_URI =
URI("https://api.ipify.org")
IPV6_URI =
URI("https://api64.ipify.org")

Class Method Summary collapse

Class Method Details

.environment ⇒ String

Returns the current runtime environment name. Checks RAILS_ENV, RACK_ENV, APP_ENV in order; falls back to "unknown".

Returns:

  • (String)


49
50
51
# File 'lib/DhanHQ/utils/network_inspector.rb', line 49

def environment
  ENV["RAILS_ENV"] || ENV["RACK_ENV"] || ENV["APP_ENV"] || "unknown"
end

.hostname ⇒ String

Returns the system hostname.

Returns:

  • (String)


41
42
43
# File 'lib/DhanHQ/utils/network_inspector.rb', line 41

def hostname
  Socket.gethostname
end

.public_ipv4 ⇒ String

Returns the public IPv4 address of this machine. Cached after the first successful lookup. Returns "unknown" on failure.

Returns:

  • (String)


26
27
28
# File 'lib/DhanHQ/utils/network_inspector.rb', line 26

def public_ipv4
  @public_ipv4 ||= fetch_ip(IPV4_URI)
end

.public_ipv6 ⇒ String

Returns the public IPv6 address of this machine. Cached after the first successful lookup. Returns "unknown" on failure.

Returns:

  • (String)


34
35
36
# File 'lib/DhanHQ/utils/network_inspector.rb', line 34

def public_ipv6
  @public_ipv6 ||= fetch_ip(IPV6_URI)
end

.reset_cache! ⇒ void

This method returns an undefined value.

Clears the memoized IP cache (useful in tests or when the IP may change).



56
57
58
59
# File 'lib/DhanHQ/utils/network_inspector.rb', line 56

def reset_cache!
  @public_ipv4 = nil
  @public_ipv6 = nil
end