Skip to content

Instantly share code, notes, and snippets.

@dannguyen
Forked from ashaw/temper.rb
Created February 13, 2012 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannguyen/1820932 to your computer and use it in GitHub Desktop.
Save dannguyen/1820932 to your computer and use it in GitHub Desktop.
# server
require 'rubygems'
require 'sinatra'
get '/temp' do
content_type :json
temp = `./temper`
bits = temp.split(' ')
"{ \"time\" : #{bits[0]}, \"fahrenheit\" : #{bits[2].gsub(/F/,'')}, \"celsius\" : #{bits[3].gsub(/C/,'')} }"
end
# autotweeter
require 'rubygems'
require 'twitter_oauth'
require 'json'
require 'rest_client'
client = TwitterOAuth::Client.new(
:consumer_key => '',
:consumer_secret => '',
:token => "",
:secret => ""
)
p client.authorized?
temp = JSON.parse(RestClient.get("http://SERVER:4567/temp"))['fahrenheit'].to_f
last_tweet = client.show('propubtemp')['status']['text']
existing_temp = last_tweet.match(/^[\d\.]+/) ? last_tweet.match(/^[\d\.]+/)[0].to_f : nil
if existing_temp && temp > existing_temp
tweet = "#{temp}°F ↑"
end
if existing_temp && temp < existing_temp
tweet = "#{temp}°F ↓"
end
if existing_temp.nil?
tweet = "#{temp}°F"
end
if tweet
p "tweeting: #{tweet}"
client.update(tweet)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment