Class: EsiForRack::Lookup::PassThrough

Inherits:
EsiForRack::Lookup 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.



7
8
9
10
# File 'lib/esi_for_rack/lookup.rb', line 7

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

Instance Method Details

#[](path) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/esi_for_rack/lookup.rb', line 12

def [](path)
  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
    response_body = response.last
    if response_body.respond_to? :to_str
      response_body.to_str
    elsif response_body.respond_to?(:each)
      body = ''
      response_body.each { |part|
        body << part.to_s
      }
      body
    else
      raise TypeError, "stringable or iterable required"
    end
  else
    nil
  end

rescue URI::InvalidURIError
  nil
end