2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-08-03 21:35:55 -05:00
|
|
|
class IncomingDomain < ActiveRecord::Base
|
2014-08-04 01:43:57 -05:00
|
|
|
def self.add!(uri)
|
|
|
|
name = uri.host
|
2017-05-04 12:26:37 -05:00
|
|
|
return unless name.present?
|
|
|
|
|
2014-08-04 01:43:57 -05:00
|
|
|
https = uri.scheme == "https"
|
|
|
|
port = uri.port
|
|
|
|
|
|
|
|
current = find_by(name: name, https: https, port: port)
|
|
|
|
return current if current
|
|
|
|
|
|
|
|
# concurrency ...
|
|
|
|
|
|
|
|
begin
|
|
|
|
current = create!(name: name, https: https, port: port)
|
2014-08-05 19:00:43 -05:00
|
|
|
rescue ActiveRecord::RecordNotUnique
|
2014-08-04 01:43:57 -05:00
|
|
|
# duplicate key is just ignored
|
|
|
|
end
|
|
|
|
|
|
|
|
current || find_by(name: name, https: https, port: port)
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_url
|
2019-05-14 09:52:56 -05:00
|
|
|
url = +"http#{https ? "s" : ""}://#{name}"
|
2014-08-04 01:43:57 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
url << ":#{port}" if https && port != 443 || !https && port != 80
|
2014-08-04 01:43:57 -05:00
|
|
|
|
|
|
|
url
|
|
|
|
end
|
2014-08-03 21:35:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: incoming_domains
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(100) not null
|
|
|
|
# https :boolean default(FALSE), not null
|
|
|
|
# port :integer not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_incoming_domains_on_name_and_https_and_port (name,https,port) UNIQUE
|
|
|
|
#
|