Sha256: b5e5775fbca832ca2c384f8b308f7aa6caa356e7119e2b6635c82d5a98e0faec

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module Federails
  module Utils
    class Host
      class << self
        COMMON_PORTS = [80, 443].freeze

        ##
        # @return [String] Host and port of the current instance
        def localhost
          uri = URI.parse Federails.configuration.site_host
          host_and_port (uri.host || 'localhost'), Federails.configuration.site_port
        end

        ##
        # Checks if the given URL points somewhere on current instance
        #
        # @param url [String] URL to check
        #
        # @return [true, false]
        def local_url?(url)
          uri = URI.parse url
          host = host_and_port uri.host, uri.port
          localhost == host
        end

        ##
        # Gets the route on the current instance, or nil
        #
        # @param url [String] URL to check
        #
        # @return [ActionDispatch::Routing::RouteSet, nil] nil when URL do not match a route
        def local_route(url)
          return nil unless local_url? url

          Rails.application.routes.recognize_path(url)
        rescue ActionController::RoutingError
          nil
        end

        private

        def host_and_port(host, port)
          port_string = if port.present? && COMMON_PORTS.exclude?(port)
                          ":#{port}"
                        else
                          ''
                        end

          "#{host}#{port_string}"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
federails-0.4.0 lib/federails/utils/host.rb
federails-0.3.0 lib/federails/utils/host.rb
federails-0.2.0 lib/federails/utils/host.rb
federails-0.1.0 lib/federails/utils/host.rb