Sha256: 9c69154ab6c527608a930d2a9b2aba80c24d1cdf4372a1db9f6dbd5d2ef60b9b

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

require 'micropub'

Micropub.configure do |c|
  c.token_endpoint = "https://tokens.indieauth.com/token"
  c.me             = "http://bookisworthy.com"
  c.allowed_scopes = [:post]
end

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))
        puts token.inspect
        if token.valid?
          response
        else
          error_response
        end
      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, ["I am micropub."]]
    end

    def error_response
      [401, {}, [""]]
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
micropub-server-rails-0.1.4 lib/micropub/server/rails/middleware.rb