>>9511197
def enter_urls
urls.each do |url|
3.times do
enter_url_and_click_preview("#{url}?#{rand(10_000)}=#{rand(10_000)}")
end
enter_url_and_click_preview(url)
end
end>>9511197
Dynamically generates social images.
There is more to the process than simply generating the image. We also need a cache-busting bot in order to update the card periodically. If we want to show a growing number of comments over time, we cannot wait for Twitter to bust the cache once per week. It still won't update instantly, but if a tweet has been floating around for a while it will show an accurate comment count even if it is more than when the tweet went out originally.
Twitter does not expose their cache-busting feature via API, so we had to ping it via browser automation.
It's a Ruby file which makes use of Capybara and Selenium to automate the process of pinging the Twitter card cache validation. It makes use of basically these three methods:
def visit_twitter_and_log_in
visit 'https://cards-dev.twitter.com/validator'
find('input.js-username-field').set(ENV['TWITTER_USERNAME'])
find('input.js-password-field').set(ENV['TWITTER_PASSWORD'])
click_on('Log in')
end
def enter_url_and_click_preview(url)
find('input.FormControl').set(url)
click_on('Preview card')
result = has_content?('Page fetched successfully')
visit 'https://cards-dev.twitter.com/validator'
end
end
That second method needs to be pinged a few times with some junk cache-busting. This is the method that calls it:
def enter_urls
urls.each do |url|
3.times do
enter_url_and_click_preview("#{url}?#{rand(10_000)}=#{rand(10_000)}")
end
enter_url_and_click_preview(url)
end
end