Sha256: d472b0104f473459f2806c817f537497c731d83c52c2f734063f71a70a929137

Contents?: true

Size: 1.51 KB

Versions: 4

Compression:

Stored size: 1.51 KB

Contents

require 'rhet-butler/stasis'
module RhetButler
  module Stasis
    class LoadFailed < StandardError; end

    class RackLoader
      def initialize(url, app)
        require 'stringio'
        @url = url
        @app = app
      end

      def root_uri
        @root_uri ||= Addressable::URI.parse(@url).tap do |root_url|
          root_url.path = ""
        end
      end

      def server_name
        root_uri.host
      end

      def load(source_uri)
        doc = Document.new
        doc.source_uri = source_uri

        source_uri = root_uri.join(source_uri).route_from(root_uri).to_s
        source_uri = "/" if source_uri == "#"

        env = {
          "REQUEST_METHOD" => "GET",
          "SCRIPT_NAME" => "",
          "PATH_INFO" => source_uri,
          "QUERY_STRING" => "",
          "SERVER_NAME" => server_name,
          "rack.errors" => StringIO.new,
          "rack.input" => StringIO.new,
          "rack.url_scheme" => "http",
          "rack.version" => Rack::VERSION,
          "rack.multithread" => false,
          "rack.multiprocess" => false,
          "rack.runonce" => false,
          "rack.hijack?" => false,
        }

        response = @app.call(env)

        code, headers, body = *response

        unless code == 200
          raise LoadFailed, "Bad response from local server for #{source_uri}:\n#{response[0..1].inspect} body: #{response[2].to_a.join.length}"
        end
        doc.type = headers["Content-Type"]
        doc.body = body.to_a.join("")
        return doc
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rhet-butler-0.14.1 lib/rhet-butler/stasis/rack-loader.rb
rhet-butler-0.14.0 lib/rhet-butler/stasis/rack-loader.rb
rhet-butler-0.13.0 lib/rhet-butler/stasis/rack-loader.rb
rhet-butler-0.12.1 lib/rhet-butler/stasis/rack-loader.rb