0
0
Fork 0

Enable to recognize most kinds of characters as URL paths (#4941)

This commit is contained in:
ふぁぼ原 2017-09-15 01:03:20 +09:00 committed by Eugen Rochko
parent b39d512ade
commit 3816943e6b
5 changed files with 96 additions and 5 deletions

View file

@ -1,9 +1,15 @@
# frozen_string_literal: true
class FetchLinkCardService < BaseService
include ActionView::Helpers::TagHelper
URL_PATTERN = %r{https?://\S+}
URL_PATTERN = %r{
( # $1 URL
(https?:\/\/)? # $2 Protocol (optional)
(#{Twitter::Regex[:valid_domain]}) # $3 Domain(s)
(?::(#{Twitter::Regex[:valid_port_number]}))? # $4 Port number (optional)
(/#{Twitter::Regex[:valid_url_path]}*)? # $5 URL Path and anchor
(\?#{Twitter::Regex[:valid_url_query_chars]}*#{Twitter::Regex[:valid_url_query_ending_chars]})? # $6 Query String
)
}iox
def call(status)
@status = status
@ -42,7 +48,7 @@ class FetchLinkCardService < BaseService
def parse_urls
if @status.local?
urls = @status.text.match(URL_PATTERN).to_a.map { |uri| Addressable::URI.parse(uri).normalize }
urls = @status.text.scan(URL_PATTERN).map { |array| Addressable::URI.parse(array[0]).normalize }
else
html = Nokogiri::HTML(@status.text)
links = html.css('a')