Class: Billy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/billy/proxy.rb

Instance Method Summary collapse

Constructor Details

#initializeProxy

Returns a new instance of Proxy.



7
8
9
# File 'lib/billy/proxy.rb', line 7

def initialize
  reset
end

Instance Method Details

#call(method, url, headers, body) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/billy/proxy.rb', line 32

def call(method, url, headers, body)
  stub = find_stub(method, url)
  unless stub.nil?
    query_string = URI.parse(url).query || ""
    params = CGI.parse(query_string)
    stub.call(params, headers, body)
  end
end

#hostObject



24
25
26
# File 'lib/billy/proxy.rb', line 24

def host
  'localhost'
end

#portObject



28
29
30
# File 'lib/billy/proxy.rb', line 28

def port
  Socket.unpack_sockaddr_in(EM.get_sockname(@signature)).first
end

#resetObject



47
48
49
# File 'lib/billy/proxy.rb', line 47

def reset
  @stubs = []
end

#start(threaded = true) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/billy/proxy.rb', line 11

def start(threaded = true)
  if threaded
    Thread.new { main_loop }
    sleep(0.01) while @signature.nil?
  else
    main_loop
  end
end

#stub(url, options = {}) ⇒ Object



41
42
43
44
45
# File 'lib/billy/proxy.rb', line 41

def stub(url, options = {})
  ret = ProxyRequestStub.new(url, options)
  @stubs << ret
  ret
end

#urlObject



20
21
22
# File 'lib/billy/proxy.rb', line 20

def url
  "http://#{host}:#{port}"
end