Created
August 23, 2012 22:48
-
-
Save grantr/3442964 to your computer and use it in GitHub Desktop.
Reel-powered http endpoints
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 'octarine' | |
require 'reel' | |
module Reel | |
module App | |
def self.included(base) | |
base.class_eval do | |
include Octarine::App | |
attr_accessor :server | |
end | |
end | |
def initialize(host, port) | |
super() | |
@server = Reel::Server.supervise host, port do |connection| | |
if connection.request # why is this nil sometimes? | |
request = connection.request | |
status, headers, body = call Rack::MockRequest.env_for(request.url, method: request.method, input: request.body) | |
connection.respond status_symbol(status), headers, body.to_s | |
end | |
end | |
end | |
def status_symbol(status) | |
status.is_a?(Fixnum) ? Http::Response::STATUS_CODES[status].downcase.gsub(/\s|-/, '_').to_sym : status.to_sym | |
end | |
def terminate | |
@server.terminate | |
end | |
end | |
end | |
class MyApp | |
include Reel::App | |
get "/foo" do |request| | |
[200, {}, "hello foo"] | |
end | |
end | |
MyApp.new("0.0.0.0", 3010) | |
# curl 127.0.0.1:3010/foo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment