require 'micropub' module Micropub::Server::Rails class Middleware def initialize(app) @app = app end def call(env) if env["PATH_INFO"] == "/micropub" token = Micropub::Token.new(auth_token(env)) token.valid? ? response : error_response else @app.call env end end def auth_token(env) input = env["HTTP_AUTHORIZATION"] || request(env).params["access_token"] || "" input.split("Bearer ").last end def response [201, headers, body] end def error_response [401, {}, body] end def body [""] end def request(env) Rack::Request.new(env) end def headers { "Location" => "http://bookisworthy.com/posts/1", "Content-Type" => "text/plain; charset=utf-8" } end end end