Since you can't get a hold of a request object in an initializer, you'll need to use socket
from the standard library. Socket is not gem, so no gem installation is necessary!
Et viola:
require 'socket'
Socket.gethostname
This assumes though that host name of your device matches the address you seek (run the command hostname
in the terminal to see what I mean). If that's not the case, you won't be able to determine the host name until a request is made, since any number of addresses can point to the same application. It might be wiser to set this through configuration variables.
If you really want something dynamic, the closest you can get is by performing the initializer within a controller. Here's an example that will dynamically set action_mailer.default_url_options
based on the first request made to the server.
class ApplicationController < ActionController::Base
before_action :set_mailer_host
private
def set_mailer_host
if !ProMetrics::Application.config.action_mailer.default_url_options
ProMetrics::Application.config.action_mailer.default_url_options = { host: request.host, port: request.port }
end
end
end