Class: PowProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/pow_proxy.rb,
lib/pow_proxy/version.rb

Constant Summary collapse

DEFAULT_HOST =
"127.0.0.1"
DEFAULT_PORT =
3000
VERSION =
"0.2.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PowProxy

Returns a new instance of PowProxy.



10
11
12
13
# File 'lib/pow_proxy.rb', line 10

def initialize(options = {})
  @host = options.delete(:host) || ENV['POW_PROXY_HOST'] || DEFAULT_HOST
  @port = options.delete(:port) || ENV['POW_PROXY_PORT'] || DEFAULT_PORT
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



8
9
10
# File 'lib/pow_proxy.rb', line 8

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/pow_proxy.rb', line 8

def port
  @port
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/pow_proxy.rb', line 15

def call(env)
  begin
    request = Rack::Request.new(env)
    headers = {}
    env.each do |key, value|
      if key =~ /^http_(.*)/i
        headers[$1] = value
      end
    end

    headers["Content-Type"] = request.content_type if request.content_type
    headers["Content-Length"] = request.content_length if request.content_length

    http = Net::HTTP.new(@host, @port)
    http.start do |http|
      response = http.send_request(request.request_method, request.fullpath, request.body.read, headers)
      headers = response.to_hash
      headers.delete "transfer-encoding"
      [response.code, headers, [response.body]]
    end
  rescue Errno::ECONNREFUSED
    [500, {}, ["Could not establish a connection to #{@host}:#{@port}, make sure your that the process you are proxying to is running."]]
  end
end