Created
April 4, 2013 02:12
-
-
Save codepants/5307165 to your computer and use it in GitHub Desktop.
ruby-hash cheapo 'CouchDb'
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 'webrick' | |
require 'json' | |
DB = {} | |
class Simple < WEBrick::HTTPServlet::AbstractServlet | |
def do_GET request, response | |
p = request.path.split "/" | |
response.status, response['Content-Type'], response.body = 200, 'text/plain', p.length < 4 ? DB[p[2].to_sym].to_json : DB[p[2].to_sym][p[3].to_sym].to_json | |
end | |
end | |
class SimpleP < WEBrick::HTTPServlet::AbstractServlet | |
def do_POST request, response | |
p = request.path.split "/" | |
x = {p[3].to_sym => p[4]} | |
(DB[p[2].to_sym]||={}).merge! x | |
response.status, response['Content-Type'], response.body = 200, 'text/plain', "loaded #{p.join "-->"}" | |
end | |
end | |
class Tester < WEBrick::HTTPServlet::AbstractServlet | |
def do_GET request, response | |
response.status, response['Content-Type'], response.body = 200, 'text/html', DATA | |
end | |
end | |
server = WEBrick::HTTPServer.new(:Port => 8699) | |
trap 'INT' do server.shutdown end | |
server.mount '/fetch', Simple | |
server.mount '/load', SimpleP | |
server.mount '/', Tester | |
server.start | |
__END__ | |
<html><body> | |
<form id='fetching'><label for='param'>params:</label><input id='liveinput' type='text' name='param' placeholder='e.g. abc/def'></form> | |
<hr></hr> | |
<form id='loading'><label for='param'>params:</label><input type='text' name='param' placeholder='e.g. abc/def'><label for='param'>params:</label><input type='text' name='val'> | |
<input type='submit' value='load'></input> | |
</form> | |
<hr></hr> | |
<div id='response'></div> | |
<style> | |
div.butt {background: red; width: 50px} | |
</style> | |
<script type='text/javascript'> | |
function reqListener () { | |
document.getElementById('response').innerHTML = '...' + this.responseText | |
}; | |
var fetch = function() { | |
var oReq = new XMLHttpRequest(); | |
oReq.onload = reqListener; | |
oReq.open('get', '/fetch/'+document.forms.fetching.param.value, true); | |
oReq.send(); | |
} | |
var load = function() { | |
var oReq = new XMLHttpRequest(); | |
oReq.onload = reqListener; | |
oReq.open('post', '/load/'+document.forms.loading.param.value +'/'+document.forms.loading.val.value, true); | |
oReq.send(); | |
return false; | |
} | |
window.load = load | |
document.getElementById('liveinput').oninput = fetch | |
document.getElementById('loading').onsubmit = load | |
</script> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment