port = 2000 host = "127.0.0.1" tweet_text = "Grüße vom #eh17" consumer_key = "" consumer_secret = "" access_token = "" access_token_secret = "" require 'twitter' require 'socket' require 'open-uri' module Twitter::Image # The Twitter gem is particular about the type of IO object it # recieves when tweeting an image. If an image is < 10kb, Ruby opens it as a # StringIO object. Which is not supported by the Twitter gem/api. # # This method ensures we always have a valid IO object for Twitter. def self.open_from_url(input) image_file = open(input) return image_file unless image_file.is_a?(StringIO) file_name = File.basename(input) temp_file = Tempfile.new(file_name) temp_file.binmode temp_file.write(image_file.read) temp_file.close open(temp_file.path) end end twitter = Twitter::REST::Client.new do |config| config.consumer_key = consumer_key config.consumer_secret = consumer_secret config.access_token = access_token config.access_token_secret = access_token_secret end server = TCPServer.open(host, port) loop { client = server.accept client.puts(Time.now.ctime) client.puts("Twitter Gateway ready, waiting for input!") while input = client.gets image = Twitter::Image.open_from_url(input) puts input puts image twitter.update_with_media(tweet_text, image) end }