Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
gem 'twitter', '=5.0.0'
require 'twitter'
require 'sinatra'
require 'json'
set :server, 'webrick'
client = Twitter::REST::Client.new do |config|
config.consumer_key = 'XXX'
config.consumer_secret = 'XXX'
config.access_token = 'XXX'
config.access_token_secret = 'XXX'
end
MAX_ATTEMPTS = 3
get '/:id' do |id|
num_attempts = 0
begin
num_attempts += 1
status = client.status(id, {include_entities: "1"})
rescue Twitter::Error::TooManyRequests => error
if num_attempts <= MAX_ATTEMPTS
sleep error.rate_limit.reset_in
retry
else
raise
end
end
output =<<EOF
<head>
<style>
body {
margin: 0;
padding: 0;
background-color: white; }
body .content {
-webkit-font-smoothing: none;
width: 384px;
overflow: hidden;
margin: 0 auto;
background-color: white; }
/*
From CCS speech bubbles: http://nicolasgallagher.com/pure-css-speech-bubbles/
*/
.triangle-border {
position: relative;
padding: 15px;
margin: 1em 0 3em;
border: 5px solid #000000;
color: #000000;
background: #fff;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px; }
.triangle-border:before {
content: "";
position: absolute;
bottom: -20px;
left: 40px;
border-width: 20px 20px 0;
border-style: solid;
border-color: #000000 transparent;
display: block;
width: 0; }
.triangle-border:after {
content: "";
position: absolute;
bottom: -13px;
left: 47px;
border-width: 13px 13px 0;
border-style: solid;
border-color: #fff transparent;
display: block;
width: 0; }
</style>
</head><body>
<div class="content">
<div style="margin-bottom: 1em;" class="triangle-border">
<p>
<span style="font-family: 'Verdana'; font-size: 18pt">#{status.text}</span>
</p><p>
<span style="font-family: 'Helvetica Neue'; font-size: 10pt">#{Time.parse(status.attrs[:created_at]).strftime("%H:%M %p - %-d %b %Y")}</span>
</p>
</div>
<img style="float: left; margin: 0px 15px 15px 0px;" src="#{status.attrs[:user][:profile_image_url]}"/>
<p>
<span style="font-family: 'Helvetica Neue'; font-size: 16pt; font-weight: bold;">#{status.attrs[:user][:name]}</span><br/>
<span style="font-family: 'Helvetica Neue'; font-size: 14pt; font-weight: bold;">@#{status.attrs[:user][:screen_name]}</span><br/>
</p>
EOF
if status.entities? && !status.attrs[:entities][:media].nil?
status.attrs[:entities][:media].each do |entity|
if entity[:media_url]
output += "<div style=\"text-align: center;\"><img src=\"#{entity[:media_url]}:small\"/></div>"
end
end
end
output +=<<EOF
</div>
</body>
</html>
EOF
output
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.