Created
February 13, 2009 22:18
-
-
Save tarvaina/64133 to your computer and use it in GitHub Desktop.
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 'rack/request' | |
require 'rack/response' | |
module Anttis | |
class Flipper | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
status, headers, body = @app.call(env) | |
body.extend Enumerable | |
body_string = body.inject { |a, b| a + b } | |
req = Rack::Request.new(env) | |
res = Rack::Response.new | |
res.write(flip_pres_lines(body_string, req.GET["flip"])) | |
res.write "<p><a href='?flip=#{flip_flip(req.GET["flip"])}'>flip!</a></p>" | |
res.finish | |
end | |
private | |
def flip_pres_lines(string, flip) | |
if flip == "left" | |
string.gsub(/<pre>(.*)<\/pre>/m) { "<pre>#{flip_lines($1)}</pre>" } | |
else | |
string | |
end | |
end | |
def flip_lines(string) | |
string.split("\n").map { |line| line.ljust(42).reverse }.join("\n") | |
end | |
def flip_flip(string) | |
if string == "left" | |
"right" | |
else | |
"left" | |
end | |
end | |
end | |
end |
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 'zlib' | |
require 'rack/request' | |
require 'rack/response' | |
module Anttis | |
class Lobster | |
LobsterString = Zlib::Inflate.inflate("eJx9kEEOwyAMBO99xd7MAcytUhPlJyj2 | |
P6jy9i4k9EQyGAnBarEXeCBqSkntNXsi/ZCvC48zGQoZKikGrFMZvgS5ZHd+aGWVuWwhVF0 | |
t1drVmiR42HcWNz5w3QanT+2gIvTVCiE1lm1Y0eU4JGmIIbaKwextKn8rvW+p5PIwFl8ZWJ | |
I8jyiTlhTcYXkekJAzTyYN6E08A+dk8voBkAVTJQ==".delete("\n ").unpack("m*")[0]) | |
def call(env) | |
res = Rack::Response.new | |
res.write "<title>Lobstericious!</title>" | |
res.write "<pre>" | |
res.write LobsterString | |
res.write "</pre>" | |
res.finish | |
end | |
end | |
end | |
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
# To run this: | |
# rackup -p 80 lobster.ru | |
require 'anttis_flipper' | |
require 'anttis_lobster' | |
use Anttis::Flipper | |
run Anttis::Lobster.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment