Class: RhetButler::Stasis::RackLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/rhet-butler/stasis/rack-loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(url, app) ⇒ RackLoader

Returns a new instance of RackLoader.



7
8
9
10
11
# File 'lib/rhet-butler/stasis/rack-loader.rb', line 7

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

Instance Method Details

#load(source_uri) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rhet-butler/stasis/rack-loader.rb', line 23

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

#root_uriObject



13
14
15
16
17
# File 'lib/rhet-butler/stasis/rack-loader.rb', line 13

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

#server_nameObject



19
20
21
# File 'lib/rhet-butler/stasis/rack-loader.rb', line 19

def server_name
  root_uri.host
end