mirror of
https://github.com/discourse/discourse.git
synced 2025-05-29 23:08:25 +08:00
DEV: Apply syntax_tree formatting to lib/*
This commit is contained in:
@ -5,13 +5,16 @@ class TwitterApi
|
||||
class << self
|
||||
include ActionView::Helpers::NumberHelper
|
||||
|
||||
BASE_URL = 'https://api.twitter.com'
|
||||
BASE_URL = "https://api.twitter.com"
|
||||
|
||||
def prettify_tweet(tweet)
|
||||
text = tweet["full_text"].dup
|
||||
if (entities = tweet["entities"]) && (urls = entities["urls"])
|
||||
urls.each do |url|
|
||||
text.gsub!(url["url"], "<a target='_blank' href='#{url["expanded_url"]}'>#{url["display_url"]}</a>")
|
||||
text.gsub!(
|
||||
url["url"],
|
||||
"<a target='_blank' href='#{url["expanded_url"]}'>#{url["display_url"]}</a>",
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
@ -19,37 +22,29 @@ class TwitterApi
|
||||
|
||||
result = Rinku.auto_link(text, :all, 'target="_blank"').to_s
|
||||
|
||||
if tweet['extended_entities'] && media = tweet['extended_entities']['media']
|
||||
if tweet["extended_entities"] && media = tweet["extended_entities"]["media"]
|
||||
media.each do |m|
|
||||
if m['type'] == 'photo'
|
||||
if large = m['sizes']['large']
|
||||
result << "<div class='tweet-images'><img class='tweet-image' src='#{m['media_url_https']}' width='#{large['w']}' height='#{large['h']}'></div>"
|
||||
if m["type"] == "photo"
|
||||
if large = m["sizes"]["large"]
|
||||
result << "<div class='tweet-images'><img class='tweet-image' src='#{m["media_url_https"]}' width='#{large["w"]}' height='#{large["h"]}'></div>"
|
||||
end
|
||||
elsif m['type'] == 'video' || m['type'] == 'animated_gif'
|
||||
video_to_display = m['video_info']['variants']
|
||||
.select { |v| v['content_type'] == 'video/mp4' }
|
||||
.sort { |v| v['bitrate'] }.last # choose highest bitrate
|
||||
elsif m["type"] == "video" || m["type"] == "animated_gif"
|
||||
video_to_display =
|
||||
m["video_info"]["variants"]
|
||||
.select { |v| v["content_type"] == "video/mp4" }
|
||||
.sort { |v| v["bitrate"] }
|
||||
.last # choose highest bitrate
|
||||
|
||||
if video_to_display && url = video_to_display['url']
|
||||
width = m['sizes']['large']['w']
|
||||
height = m['sizes']['large']['h']
|
||||
if video_to_display && url = video_to_display["url"]
|
||||
width = m["sizes"]["large"]["w"]
|
||||
height = m["sizes"]["large"]["h"]
|
||||
|
||||
attributes =
|
||||
if m['type'] == 'animated_gif'
|
||||
%w{
|
||||
playsinline
|
||||
loop
|
||||
muted
|
||||
autoplay
|
||||
disableRemotePlayback
|
||||
disablePictureInPicture
|
||||
}
|
||||
if m["type"] == "animated_gif"
|
||||
%w[playsinline loop muted autoplay disableRemotePlayback disablePictureInPicture]
|
||||
else
|
||||
%w{
|
||||
controls
|
||||
playsinline
|
||||
}
|
||||
end.join(' ')
|
||||
%w[controls playsinline]
|
||||
end.join(" ")
|
||||
|
||||
result << <<~HTML
|
||||
<div class='tweet-images'>
|
||||
@ -57,7 +52,7 @@ class TwitterApi
|
||||
<video class='tweet-video' #{attributes}
|
||||
width='#{width}'
|
||||
height='#{height}'
|
||||
poster='#{m['media_url_https']}'>
|
||||
poster='#{m["media_url_https"]}'>
|
||||
<source src='#{url}' type="video/mp4">
|
||||
</video>
|
||||
</div>
|
||||
@ -72,7 +67,16 @@ class TwitterApi
|
||||
end
|
||||
|
||||
def prettify_number(count)
|
||||
number_to_human(count, format: '%n%u', precision: 2, units: { thousand: 'K', million: 'M', billion: 'B' })
|
||||
number_to_human(
|
||||
count,
|
||||
format: "%n%u",
|
||||
precision: 2,
|
||||
units: {
|
||||
thousand: "K",
|
||||
million: "M",
|
||||
billion: "B",
|
||||
},
|
||||
)
|
||||
end
|
||||
|
||||
def tweet_for(id)
|
||||
@ -87,19 +91,23 @@ class TwitterApi
|
||||
protected
|
||||
|
||||
def link_handles_in(text)
|
||||
text.gsub(/(?:^|\s)@\w+/) do |match|
|
||||
whitespace = match[0] == " " ? " " : ""
|
||||
handle = match.strip[1..]
|
||||
"#{whitespace}<a href='https://twitter.com/#{handle}' target='_blank'>@#{handle}</a>"
|
||||
end.strip
|
||||
text
|
||||
.gsub(/(?:^|\s)@\w+/) do |match|
|
||||
whitespace = match[0] == " " ? " " : ""
|
||||
handle = match.strip[1..]
|
||||
"#{whitespace}<a href='https://twitter.com/#{handle}' target='_blank'>@#{handle}</a>"
|
||||
end
|
||||
.strip
|
||||
end
|
||||
|
||||
def link_hashtags_in(text)
|
||||
text.gsub(/(?:^|\s)#\w+/) do |match|
|
||||
whitespace = match[0] == " " ? " " : ""
|
||||
hashtag = match.strip[1..]
|
||||
"#{whitespace}<a href='https://twitter.com/search?q=%23#{hashtag}' target='_blank'>##{hashtag}</a>"
|
||||
end.strip
|
||||
text
|
||||
.gsub(/(?:^|\s)#\w+/) do |match|
|
||||
whitespace = match[0] == " " ? " " : ""
|
||||
hashtag = match.strip[1..]
|
||||
"#{whitespace}<a href='https://twitter.com/search?q=%23#{hashtag}' target='_blank'>##{hashtag}</a>"
|
||||
end
|
||||
.strip
|
||||
end
|
||||
|
||||
def tweet_uri_for(id)
|
||||
@ -108,7 +116,7 @@ class TwitterApi
|
||||
|
||||
def twitter_get(uri)
|
||||
request = Net::HTTP::Get.new(uri)
|
||||
request.add_field 'Authorization', "Bearer #{bearer_token}"
|
||||
request.add_field "Authorization", "Bearer #{bearer_token}"
|
||||
response = http(uri).request(request)
|
||||
|
||||
if response.kind_of?(Net::HTTPTooManyRequests)
|
||||
@ -121,23 +129,21 @@ class TwitterApi
|
||||
def authorization
|
||||
request = Net::HTTP::Post.new(auth_uri)
|
||||
|
||||
request.add_field 'Authorization',
|
||||
"Basic #{bearer_token_credentials}"
|
||||
request.add_field 'Content-Type',
|
||||
'application/x-www-form-urlencoded;charset=UTF-8'
|
||||
request.add_field "Authorization", "Basic #{bearer_token_credentials}"
|
||||
request.add_field "Content-Type", "application/x-www-form-urlencoded;charset=UTF-8"
|
||||
|
||||
request.set_form_data 'grant_type' => 'client_credentials'
|
||||
request.set_form_data "grant_type" => "client_credentials"
|
||||
|
||||
http(auth_uri).request(request).body
|
||||
end
|
||||
|
||||
def bearer_token
|
||||
@access_token ||= JSON.parse(authorization).fetch('access_token')
|
||||
@access_token ||= JSON.parse(authorization).fetch("access_token")
|
||||
end
|
||||
|
||||
def bearer_token_credentials
|
||||
Base64.strict_encode64(
|
||||
"#{UrlHelper.encode_component(consumer_key)}:#{UrlHelper.encode_component(consumer_secret)}"
|
||||
"#{UrlHelper.encode_component(consumer_key)}:#{UrlHelper.encode_component(consumer_secret)}",
|
||||
)
|
||||
end
|
||||
|
||||
|
Reference in New Issue
Block a user