mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 06:13:38 +08:00
283b08d45f
* Move onebox gem in core library * Update template file path * Remove warning for onebox gem caching * Remove onebox version file * Remove onebox gem * Add sanitize gem * Require onebox library in lazy-yt plugin * Remove onebox web specific code This code was used in standalone onebox Sinatra application * Merge Discourse specific AllowlistedGenericOnebox engine in core * Fix onebox engine filenames to match class name casing * Move onebox specs from gem into core * DEV: Rename `response` helper to `onebox_response` Fixes a naming collision. * Require rails_helper * Don't use `before/after(:all)` * Whitespace * Remove fakeweb * Remove poor unit tests * DEV: Re-add fakeweb, plugins are using it * Move onebox helpers * Stub Instagram API * FIX: Follow additional redirect status codes (#476) Don’t throw errors if we encounter 303, 307 or 308 HTTP status codes in responses * Remove an empty file * DEV: Update the license file Using the copy from https://choosealicense.com/licenses/gpl-2.0/# Hopefully this will enable GitHub to show the license UI? * DEV: Update embedded copyrights * DEV: Add Onebox copyright notice * DEV: Add MIT license, convert COPYRIGHT.txt to md * DEV: Remove an incorrect copyright claim Co-authored-by: Jarek Radosz <jradosz@gmail.com> Co-authored-by: jbrw <jamie@goatforce5.org>
94 lines
2.1 KiB
Ruby
94 lines
2.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Onebox
|
|
class OpenGraph
|
|
|
|
attr_reader :data
|
|
|
|
def initialize(doc)
|
|
@data = extract(doc)
|
|
end
|
|
|
|
def title
|
|
get(:title, 80)
|
|
end
|
|
|
|
def title_attr
|
|
!title.nil? ? "title='#{title}'" : ""
|
|
end
|
|
|
|
def secure_image_url
|
|
secure_url = URI(get(:image))
|
|
secure_url.scheme = 'https'
|
|
secure_url.to_s
|
|
end
|
|
|
|
def method_missing(attr, *args, &block)
|
|
value = get(attr, *args)
|
|
|
|
return nil if Onebox::Helpers::blank?(value)
|
|
|
|
method_name = attr.to_s
|
|
if method_name.end_with?(*integer_suffixes)
|
|
value.to_i
|
|
elsif method_name.end_with?(*url_suffixes)
|
|
result = Onebox::Helpers.normalize_url_for_output(value)
|
|
result unless Onebox::Helpers::blank?(result)
|
|
else
|
|
value
|
|
end
|
|
end
|
|
|
|
def get(attr, length = nil, sanitize = true)
|
|
return nil if Onebox::Helpers::blank?(data)
|
|
|
|
value = data[attr]
|
|
|
|
return nil if Onebox::Helpers::blank?(value)
|
|
|
|
value = html_entities.decode(value)
|
|
value = Sanitize.fragment(value) if sanitize
|
|
value.strip!
|
|
value = Onebox::Helpers.truncate(value, length) unless length.nil?
|
|
|
|
value
|
|
end
|
|
|
|
private
|
|
|
|
def integer_suffixes
|
|
['width', 'height']
|
|
end
|
|
|
|
def url_suffixes
|
|
['url', 'image', 'video']
|
|
end
|
|
|
|
def html_entities
|
|
@html_entities ||= HTMLEntities.new
|
|
end
|
|
|
|
def extract(doc)
|
|
return {} if Onebox::Helpers::blank?(doc)
|
|
|
|
data = {}
|
|
|
|
doc.css('meta').each do |m|
|
|
if (m["property"] && m["property"][/^(?:og|article|product):(.+)$/i]) || (m["name"] && m["name"][/^(?:og|article|product):(.+)$/i])
|
|
value = (m["content"] || m["value"]).to_s
|
|
data[$1.tr('-:', '_').to_sym] ||= value unless Onebox::Helpers::blank?(value)
|
|
end
|
|
end
|
|
|
|
# Attempt to retrieve the title from the meta tag
|
|
title_element = doc.at_css('title')
|
|
if title_element && title_element.text
|
|
data[:title] ||= title_element.text unless Onebox::Helpers.blank?(title_element.text)
|
|
end
|
|
|
|
data
|
|
end
|
|
|
|
end
|
|
end
|