Class: MiddleSquid::Server

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeServer

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

#hostString (readonly)

Returns:

  • (String)


13
14
15
# File 'lib/middle_squid/server.rb', line 13

def host
  @host
end

#portFixnum (readonly)

Returns:

  • (Fixnum)


16
17
18
# File 'lib/middle_squid/server.rb', line 16

def port
  @port
end

Instance Method Details

#startObject



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

#stopObject



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.

Parameters:

  • block (#call)

    called when the token is requested

Returns:

  • (String)

    random 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