Class: WhackANode::Rpc

Inherits:
Object
  • Object
show all
Defined in:
lib/whack_a_node/rpc.rb

Instance Method Summary collapse

Constructor Details

#initialize(path = "/", host = "localhost", port = "8820", redirect = false) ⇒ Rpc

Returns a new instance of Rpc.



6
7
8
9
10
11
12
# File 'lib/whack_a_node/rpc.rb', line 6

def initialize(path="/",host="localhost", port="8820", redirect=false)
  @path = path
  @host = host
  @port = port
  @redirect = redirect

end

Instance Method Details

#call(env) ⇒ Object



34
35
36
# File 'lib/whack_a_node/rpc.rb', line 34

def call(env)
  return @redirect ? forward_request : proxy_request
end

#forward_requestObject



30
31
32
# File 'lib/whack_a_node/rpc.rb', line 30

def forward_request
  [ 302, {'Location'=> uri.to_s }, [] ]
end

#proxy_requestObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/whack_a_node/rpc.rb', line 14

def proxy_request
  uri = self.uri
  session = Net::HTTP.new(uri.host, uri.port)
  session.start {|http|
    req = Net::HTTP::Get.new(uri.request_uri)
    body = ''
    res = http.request(req) do |res|
      res.read_body do |segment|
        body << segment
      end
    end

    [res.code, create_response_headers(res), [body]]
  }
end