Class: EsiForRack::Lookup::PassThrough

Inherits:
Object
  • Object
show all
Defined in:
lib/esi_for_rack/lookup.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ PassThrough

Returns a new instance of PassThrough.



28
29
30
31
# File 'lib/esi_for_rack/lookup.rb', line 28

def initialize(app, env)
  @app = app
  @env = env
end

Instance Method Details

#[](path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/esi_for_rack/lookup.rb', line 33

def [](path)
  return if path[0,4] == 'http'
  
  uri = URI(path)
  
  request = {
    "REQUEST_METHOD" => "GET",
    "SERVER_NAME" => @env['SERVER_NAME'],
    "SERVER_PORT" => @env['SERVER_PORT'],
    "QUERY_STRING" => uri.query.to_s,
    "PATH_INFO" => (!uri.path || uri.path.empty?) ? "/" : uri.path,
    "rack.url_scheme" => uri.scheme || @env['rack.url_scheme'] || 'http',
    "SCRIPT_NAME" => ""
  }

  response = @app.call(request)
  if response.first == 200
    EsiForRack.response_body_to_str(response.last)
  else
    nil
  end

rescue URI::InvalidURIError
  nil
end