2021-05-26 17:41:35 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Onebox
|
|
|
|
module Engine
|
|
|
|
class GoogleDocsOnebox
|
|
|
|
include Engine
|
|
|
|
include StandardEmbed
|
|
|
|
include LayoutSupport
|
|
|
|
|
|
|
|
SUPPORTED_ENDPOINTS = %w[spreadsheets document forms presentation]
|
|
|
|
SHORT_TYPES = { spreadsheets: :sheets, document: :docs, presentation: :slides, forms: :forms }
|
2023-01-09 20:10:19 +08:00
|
|
|
|
2021-05-26 17:41:35 +08:00
|
|
|
matches_regexp(
|
|
|
|
%r{^(https?:)?//(docs\.google\.com)/(?<endpoint>(#{SUPPORTED_ENDPOINTS.join("|")}))/d/((?<key>[\w-]*)).+$},
|
|
|
|
)
|
|
|
|
always_https
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def data
|
|
|
|
og_data = get_opengraph
|
|
|
|
short_type = SHORT_TYPES[match[:endpoint].to_sym]
|
|
|
|
|
|
|
|
description =
|
2023-10-08 01:54:26 +08:00
|
|
|
if og_data.description.blank?
|
2021-05-26 17:41:35 +08:00
|
|
|
"This #{short_type.to_s.chop.capitalize} is private"
|
|
|
|
else
|
|
|
|
Onebox::Helpers.truncate(og_data.description, 250)
|
|
|
|
end
|
|
|
|
|
|
|
|
{
|
|
|
|
link: link,
|
|
|
|
title: og_data.title || "Google #{short_type.to_s.capitalize}",
|
|
|
|
description: description,
|
|
|
|
type: short_type,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def match
|
|
|
|
@match ||= @url.match(@@matcher)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|