mirror of
https://github.com/discourse/discourse.git
synced 2024-11-28 02:53:45 +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>
57 lines
1.6 KiB
Ruby
57 lines
1.6 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Onebox
|
|
module Engine
|
|
class StackExchangeOnebox
|
|
include Engine
|
|
include LayoutSupport
|
|
include JSON
|
|
|
|
def self.domains
|
|
%w(stackexchange.com stackoverflow.com superuser.com serverfault.com askubuntu.com stackapps.com mathoverflow.net)
|
|
.map { |domain| Regexp.escape(domain) }
|
|
end
|
|
|
|
matches_regexp(/^https?:\/\/(?:(?:(?<subsubdomain>\w*)\.)?(?<subdomain>\w*)\.)?(?<domain>#{domains.join('|')})\/((?:questions|q)\/(?<question_id>\d*)(\/.*\/(?<answer_id1>\d*))?|(a\/(?<answer_id2>\d*)))/)
|
|
|
|
def always_https?
|
|
uri.host.split('.').length <= 3
|
|
end
|
|
|
|
private
|
|
|
|
def match
|
|
@match ||= @url.match(@@matcher)
|
|
end
|
|
|
|
def url
|
|
domain = uri.host
|
|
question_id = match[:question_id]
|
|
answer_id = match[:answer_id2] || match[:answer_id1]
|
|
|
|
if answer_id
|
|
"https://api.stackexchange.com/2.2/answers/#{answer_id}?site=#{domain}&filter=!.FjueITQdx6-Rq3Ue9PWG.QZ2WNdW"
|
|
else
|
|
"https://api.stackexchange.com/2.2/questions/#{question_id}?site=#{domain}&filter=!5-duuxrJa-iw9oVvOA(JNimB5VIisYwZgwcfNI"
|
|
end
|
|
end
|
|
|
|
def data
|
|
return @data if defined?(@data)
|
|
|
|
result = raw['items'][0]
|
|
if result
|
|
result['creation_date'] =
|
|
Time.at(result['creation_date'].to_i).strftime("%I:%M%p - %d %b %y %Z")
|
|
|
|
result['tags'] = result['tags'].take(4).join(', ')
|
|
result['is_answer'] = result.key?('answer_id')
|
|
result['is_question'] = result.key?('question_id')
|
|
end
|
|
|
|
@data = result
|
|
end
|
|
end
|
|
end
|
|
end
|