-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet_block_macro.rb
79 lines (65 loc) · 2.48 KB
/
tweet_block_macro.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'uri'
require 'net/http'
require 'json'
require 'date'
# Define the extension
class TweetBlockMacro < Asciidoctor::Extensions::BlockMacroProcessor
# Implement the process method
def process parent, target, attrs
tweet_id = target
# Validate that the ID is an integer value
unless tweet_id =~ /\A\d+\z/
raise "Invalid tweet ID: #{tweet_id}"
end
# Check if the 'TWITTER_BEARER_TOKEN' environment variable is set
unless ENV.key?('TWITTER_BEARER_TOKEN')
raise "Twitter bearer token is not set"
end
# Generate the HTML code for the tweet
html = generate_tweet_html(tweet_id)
# Create a new block containing the tweet HTML and add it to the document
create_block parent, :pass, html, attrs
end
def generate_tweet_html tweet_id
# Use the Twitter API to fetch the tweet by its ID
tweet = fetch_tweet(tweet_id)
tweet_data = tweet['data']
tweet_author = tweet['includes']['users'][0]
created_at = DateTime.parse(tweet_data['created_at'])
# Generate the HTML code for the tweet
html = <<~HTML
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">#{tweet_data['text']}</p>— #{tweet_author['name']} (@#{tweet_author['username']})
<a href="https://twitter.com/#{tweet_author['username']}/status/#{tweet_id}">#{created_at.strftime('%b %d, %Y')}</a>
</blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
HTML
html
end
def fetch_tweet tweet_id
# construct the URL for the Twitter API request
tweet_url = "https://api.twitter.com/2/tweets/#{tweet_id}?user.fields=id,name,username&tweet.fields=id,text,created_at&expansions=author_id"
uri = URI.parse(tweet_url)
# create the HTTP request and set the necessary headers
request = Net::HTTP::Get.new(uri)
request['Authorization'] = "Bearer " + ENV["TWITTER_BEARER_TOKEN"]
request['Accept'] = 'application/json'
# make the HTTP request and retrieve the response
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
http.request(request)
end
if response.code != '200'
if response.code == '404'
raise "Error: Tweet not found with ID #{tweet_id}"
else
raise "Error: #{response.code} #{response.message}"
end
end
tweet = JSON.parse(response.body)
tweet
end
end
# Register the extension
Asciidoctor::Extensions.register do
block_macro TweetBlockMacro, 'tweet'
end