fix dependency
This commit is contained in:
parent
109dcd205c
commit
aad67af0d1
1 changed files with 9 additions and 7 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'net/http'
|
||||||
|
|
||||||
class Link < ApplicationRecord
|
class Link < ApplicationRecord
|
||||||
belongs_to :link_category
|
belongs_to :link_category
|
||||||
has_rich_text :description_html
|
has_rich_text :description_html
|
||||||
|
|
@ -5,7 +7,7 @@ class Link < ApplicationRecord
|
||||||
validates :url, :text, presence: true
|
validates :url, :text, presence: true
|
||||||
validate :check_url
|
validate :check_url
|
||||||
validate :valid_url
|
validate :valid_url
|
||||||
|
|
||||||
before_validation :ensure_absolute_url
|
before_validation :ensure_absolute_url
|
||||||
|
|
||||||
def ok?
|
def ok?
|
||||||
|
|
@ -14,21 +16,21 @@ class Link < ApplicationRecord
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0"
|
USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/115.0'
|
||||||
|
|
||||||
def check_url(input = url, add_error = true)
|
def check_url(input = url, add_error = true)
|
||||||
response = begin
|
response = begin
|
||||||
Net::HTTP.get_response(URI.parse(input), "User-Agent": USER_AGENT)
|
Net::HTTP.get_response(URI.parse(input), 'User-Agent': USER_AGENT)
|
||||||
rescue Socket::ResolutionError
|
rescue Socket::ResolutionError
|
||||||
errors.add(:url, :invalid) if add_error
|
errors.add(:url, :invalid) if add_error
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
case response
|
case response
|
||||||
when Net::HTTPSuccess
|
when Net::HTTPSuccess
|
||||||
self.last_check_at = Time.zone.now
|
self.last_check_at = Time.zone.now
|
||||||
self.fail_count = 0
|
self.fail_count = 0
|
||||||
|
|
||||||
true
|
true
|
||||||
when Net::HTTPRedirection
|
when Net::HTTPRedirection
|
||||||
check_url(response[:location])
|
check_url(response[:location])
|
||||||
|
|
@ -42,10 +44,10 @@ class Link < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_absolute_url
|
def ensure_absolute_url
|
||||||
self.url = "https://#{self.url}" unless self.url.match?(%r{https{0,1}://.*})
|
self.url = "https://#{url}" unless url.match?(%r{https{0,1}://.*})
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_url
|
def valid_url
|
||||||
errors.add(:url, :invalid) unless url.match /\A#{URI::regexp(['http', 'https'])}\z/
|
errors.add(:url, :invalid) unless url.match(/\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue