Skip to content

Instantly share code, notes, and snippets.

@Koronen
Created October 6, 2013 20:54
Show Gist options
  • Save Koronen/6858977 to your computer and use it in GitHub Desktop.
Save Koronen/6858977 to your computer and use it in GitHub Desktop.
A simple TextAid server written in Ruby using Sinatra.
require './textaid-server'
run TextAid::Server
source 'https://rubygems.org'
gem 'rake'
gem 'sinatra'
GEM
remote: https://rubygems.org/
specs:
rack (1.5.2)
rack-protection (1.5.0)
rack
rake (10.1.0)
sinatra (1.4.3)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (1.4.1)
PLATFORMS
ruby
DEPENDENCIES
rake
sinatra
require 'sinatra'
require 'tempfile'
module TextAid
class Server < Sinatra::Base
post '/' do
text = request.env['rack.input'].read
editor = ENV['EDITOR']
file = Tempfile.new([params[:id], '.txt'])
begin
file.write(text)
file.close
command = "#{editor} #{file.path}"
pid = spawn(command, in: STDIN, out: STDOUT, err: STDERR)
Process.waitpid(pid)
text = IO.read(file.path).strip
ensure
file.unlink
end
text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment