Created
October 6, 2013 20:54
-
-
Save Koronen/6858977 to your computer and use it in GitHub Desktop.
A simple TextAid server written in Ruby using Sinatra.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './textaid-server' | |
run TextAid::Server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'rake' | |
gem 'sinatra' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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