lib/micropub/server/rails/middleware.rb in micropub-server-rails-0.1.7 vs lib/micropub/server/rails/middleware.rb in micropub-server-rails-0.1.8
- old
+ new
@@ -1,10 +1,11 @@
require 'micropub'
module Micropub::Server::Rails
class Middleware
class HTTPError < StandardError
+ attr_reader :status
def initialize(env, status, body="")
@env = env
@status = status
@body = body
end
@@ -15,19 +16,18 @@
end
def call(env)
begin
@params = request(env).params
- @headers = {"Content-Type" => "text/plain; charset=utf-8"}
+ @headers = {"Content-Type" => "application/x-www-form-urlencoded"}
if env["PATH_INFO"] == "/micropub"
- check_token(env)
micropub_call env
else
@app.call env
end
- rescue
- error_response(401)
+ rescue HTTPError => env
+ error_response(env.status)
end
end
def check_token(env)
token = Micropub::Token.new(auth_token(env))
@@ -38,21 +38,32 @@
input = env["HTTP_AUTHORIZATION"] || @params["access_token"] || ""
input.split("Bearer ").last
end
def micropub_call(env)
+ case env["REQUEST_METHOD"]
+ when "GET" then syndicate_to env
+ when "POST" then post env
+ else
+ raise HTTPError.new env, 405
+ end
+ end
+
+ def syndicate_to(env)
+ # TODO: Remove hardcoded syndicate link
+ [200, @headers, ["syndicate-to[]=twitter.com%2fbookis"]]
+ end
+
+ def post(env)
+ check_token env
router = Micropub::Homesteading::Router.new(@params)
client = Micropub::Client.new(router.as, token: auth_token(env))
response = client.post(@params.except("access_token"))
raise HTTPError.new env, response.status if !response.successful?
@headers["Location"] = response.location
- success_response
- end
-
- def success_response
[201, @headers, [""]]
end
def error_response(status)
[status, {}, [""]]