Skip to content

Instantly share code, notes, and snippets.

@KarlHerler
Created September 12, 2011 12:49
Show Gist options
  • Save KarlHerler/1211170 to your computer and use it in GitHub Desktop.
Save KarlHerler/1211170 to your computer and use it in GitHub Desktop.
Move along...
require 'sinatra'
require 'fileutils'
require 'date'
require 'json'
set :port, 1337
set :public, './data'
@id = 1337
def h_top
return "<html>
<head>
<title>RecPotato stach</title>
<style>
html { font-family: sans-serif; color: #404040; background: #FDF6DE; }
h1 {font-size: 5em; }
h3 {text-align: center; }
body { width: 80%; margin-left: 10%; }
label { display: block; font-size: .8em; margin-left: 10%;}
input, textarea { width: 70%; font-size: 16px; margin-bottom: 1em; margin-left: 10%; }
textarea { height: 100px; }
.heading { width: 92%; max-width: 1000px; margin: 0 auto; }
.view {width: 40%; float: left;}
.manage {width: 40%; float: right;}
</style>
</head>
<body>
<img class='heading' src='http://www.recpotato.com/wp-content/uploads/2011/07/recpotatologo3.png' width='1000' alt='RecPotato'>
<h2> Welcome to the RecPotato &beta; home, here's a listing of videos uploaded from the recpoato app</h2><br />"
end
def get_view
returner = "<div class='view'>"
returner = returner + "<h3>Recorded videos</h3>"
returner = returner + "<ul>"+`ls --sort=t data/videos`.split(/\n/).map{|x| "<li><a href='/videos/#{x}' target='_blank'>#{x}</a></li>" }.join()+"</ul>"
returner = returner + "</div>"
return returner
end
def form(name, type)
returner = "<label>#{name}</label>"
returner = returner + "<input type='text' name='#{name}' id='#{name}'></input>" if type==:text
returner = returner + "<textarea name='#{name}' id='#{name}'></textarea>" if type==:area
return returner
end
def get_add
returner = "<div class='manage'>"
returner = returner + "<h3>Add new task</h3>"
returner = returner + "<p>Here you can write a new research task and push it to our users. Be careful though, this <del>does work so a user gets it instantly<del>.</p>"
returner = returner + "<form method='post' action='/tasks/new'>"+form("Name", :text)+form("Value", :text)+form("Location", :text)
returner = returner + form("Intro description", :area)+form("Long Description", :area)
returner = returner + "<input type='submit' value='Submit'></input></form>"
returner = returner + "</div>"
return returner
end
def h_end
return "</body></html>"
end
def h_output(input)
return "#{h_top}#{input}#{h_end}"
end
get '/' do
#dir = `ls --sort=t uploads`
#return h_output("<ul>"+dir.split(/\n/).map{|x| "<li><a href='#{x}' target='_blank'>#{x}</a></li>" }.join()+"</ul>")
return h_output("#{get_view}#{get_add}")
end
post '/' do
#puts params
#puts params[:upload][:file][:filename]
d = DateTime.now
File.open('data/videos/'+"#{params[:upload][:file][:filename].split(' ').join('-')}_1_#{d.to_s.split('+')[0]}.mov", "w") do |f|
f.write(params[:upload][:file][:tempfile].read)
end
"cake"
end
post '/tasks/new' do
File.open('data/tasks.json', 'r+') do |f|
tasks = JSON.parse(f.read)
tasks.push(params)
f.rewind
f.write(tasks.to_json)
end
puts params
output = ""
params.each {|key, val| output = output + "<strong>#{key}</strong>: #{val}. <br />" }
return h_output("<h2>Creating the task for you</h2><br><p>#{output}</p>")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment