Skip to content

Commit

Permalink
Add location link to openmap
Browse files Browse the repository at this point in the history
  • Loading branch information
tschaefer committed Jan 23, 2025
1 parent 53250d7 commit 26a8c5c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/helpers/host_info_helper.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
module HostInfoHelper
OSM_MARKER_URL = 'https://www.openstreetmap.org/?mlat=%s&mlon=%s#map=17/%s/%s'.freeze

def host_info
server_name = request.server_name

info = Rails.cache.fetch(server_name, expires_in: 1.day) do
Supervisor::HostInfo.new(server_name).to_h
end

location = format_location(info)

# rubocop:disable Rails/OutputSafety
"Hostname: #{info[:hostname]} | IP: #{info[:ip]} | Location: #{location}".html_safe
# rubocop:enable Rails/OutputSafety
end

private

def format_location(info)
location = %i[city region country org].filter_map do |key|
next if info[key].blank?

info[key]
end.join(', ')
location = '-' if location.blank?

"Hostname: #{info[:hostname]} | IP: #{info[:ip]} | Location: #{location}"
return '-' if location.blank?

lat, lon = info[:loc].split(',')
link_to(location, format(OSM_MARKER_URL, lat, lon, lat, lon), class: 'link-secondary')
end
end

0 comments on commit 26a8c5c

Please sign in to comment.