Class: MiddleSquid::Server
- Inherits:
-
Object
- Object
- MiddleSquid::Server
- Defined in:
- lib/middle_squid/server.rb
Overview
Manages the internal HTTP server.
Constant Summary collapse
- DEFAULT_HOST =
'127.0.0.1'.freeze
- DEFAULT_PORT =
0
- TOKEN_TIMEOUT =
10
Instance Attribute Summary collapse
- #host ⇒ String readonly
- #port ⇒ Fixnum readonly
Instance Method Summary collapse
-
#initialize ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #stop ⇒ Object
-
#token_for(block) ⇒ String
Creates a temporary token.
Constructor Details
#initialize ⇒ Server
Returns a new instance of Server.
18 19 20 21 22 23 |
# File 'lib/middle_squid/server.rb', line 18 def initialize @tokens = {} @thin = Thin::Server.new DEFAULT_HOST, DEFAULT_PORT, method(:handler), :backend => Backends::Thin, :signals => false end |
Instance Attribute Details
#host ⇒ String (readonly)
13 14 15 |
# File 'lib/middle_squid/server.rb', line 13 def host @host end |
#port ⇒ Fixnum (readonly)
16 17 18 |
# File 'lib/middle_squid/server.rb', line 16 def port @port end |
Instance Method Details
#start ⇒ Object
25 26 27 28 29 30 |
# File 'lib/middle_squid/server.rb', line 25 def start @thin.start sockname = EM.get_sockname @thin.backend.signature @port, @host = Socket.unpack_sockaddr_in sockname end |
#stop ⇒ Object
32 33 34 35 |
# File 'lib/middle_squid/server.rb', line 32 def stop @thin.stop @port = @host = nil end |
#token_for(block) ⇒ String
Creates a temporary token.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/middle_squid/server.rb', line 41 def token_for(block) token = SecureRandom.uuid @tokens[token] = block EM.add_timer(TOKEN_TIMEOUT) { @tokens.delete token } token end |