Created
October 25, 2012 10:00
-
-
Save voidtuxic/3951759 to your computer and use it in GitHub Desktop.
ruby rack
This file contains hidden or 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 'json' | |
class IngeServer | |
def call(env) | |
@path = env['PATH_INFO'] | |
@request = Rack::Request.new(env) | |
self.router | |
end | |
def router | |
if @path == '/' | |
return self.home | |
elsif @path.split('/')[1] == 'Send' | |
return self.send | |
end | |
self.not_found | |
end | |
def home | |
ip = @request.path | |
content = "<h1>Hello</h1>" | |
content += "<p><form method=POST action='/Send'>" | |
content += "<input type='text' name='value'/>" | |
content += "<input type='submit' name='send' value='send'/>" | |
content += "</form></p>" | |
[200,{"Content-Type" => "text/html"},[content]] | |
end | |
def not_found | |
content = Hash.new | |
content['error'] = "GET IN THE CAR, IT'S A LION" | |
[404,{"Content-Type" => "application/json"},[content.to_json]] | |
end | |
def send | |
if @request.request_method != "POST" | |
return self.forbidden | |
end | |
[201,{"Content-Type" => "application/json"},[@request.params.to_json]] | |
end | |
def forbidden | |
content = Hash.new | |
content['error'] = "NOPE" | |
[403,{"Content-Type" => "application/json"},[content.to_json]] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment