Class: Infrataster::Contexts::HttpContext

Inherits:
BaseContext
  • Object
show all
Defined in:
lib/infrataster/contexts/http_context.rb

Instance Attribute Summary

Attributes inherited from BaseContext

#resource, #server

Instance Method Summary collapse

Methods inherited from BaseContext

#current_server, #initialize

Constructor Details

This class inherits a constructor from Infrataster::Contexts::BaseContext

Instance Method Details

#determine_host(default) ⇒ Object



41
42
43
# File 'lib/infrataster/contexts/http_context.rb', line 41

def determine_host(default)
  resource.uri.host || (server.options[:http] && server.options[:http][:host]) || default
end

#responseObject



6
7
8
9
10
11
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
# File 'lib/infrataster/contexts/http_context.rb', line 6

def response
  server.forward_port(resource.uri.port) do |address, port|
    url = "#{resource.uri.scheme}://#{address}:#{port}"
    options = {:url => url}
    
    if resource.uri.scheme == 'https'
      options[:ssl] = resource.ssl_option
    end
    
    conn = Faraday.new(options) do |faraday|
      faraday.request  :url_encoded
      faraday.response :logger, Logger
      if resource.inflate_gzip?
        faraday.use      Infrataster::FaradayMiddleware::Gzip
      end
      faraday.adapter  Faraday.default_adapter
      faraday.basic_auth(*resource.basic_auth) if resource.basic_auth
    end

    conn.public_send(resource.method) do |req|
      resource.params.each_pair do |k, v|
        req.params[k] = v
      end
      req.headers['Host'] = determine_host(address)
      resource.headers.each_pair do |k, v|
        req.headers[k] = v
      end

      req.body = resource.body if resource.body

      req.url resource.uri.path
    end
  end
end