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



44
45
46
# File 'lib/infrataster/contexts/http_context.rb', line 44

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

#responseObject



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
40
41
42
# File 'lib/infrataster/contexts/http_context.rb', line 7

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

    host = determine_host(address)

    conn = Faraday.new(options) do |faraday|
      faraday.request  :url_encoded
      faraday.response :logger, Logger
      middlewares(host => address).each do |middleware|
        faraday.use(*middleware)
      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'] = host
      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