Class: DevelopmentServer::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/yodel/middleware/development_server.rb

Constant Summary collapse

MESSAGE_TYPES =
{request: 0, response: 1, restart: 2, exit: 3}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message_type) ⇒ Message

Returns a new instance of Message.



20
21
22
# File 'lib/yodel/middleware/development_server.rb', line 20

def initialize(message_type)
  @message_type = message_type
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



12
13
14
# File 'lib/yodel/middleware/development_server.rb', line 12

def body
  @body
end

#dataObject

Returns the value of attribute data.



12
13
14
# File 'lib/yodel/middleware/development_server.rb', line 12

def data
  @data
end

#envObject

Returns the value of attribute env.



12
13
14
# File 'lib/yodel/middleware/development_server.rb', line 12

def env
  @env
end

#headersObject

Returns the value of attribute headers.



12
13
14
# File 'lib/yodel/middleware/development_server.rb', line 12

def headers
  @headers
end

#message_typeObject

Returns the value of attribute message_type.



12
13
14
# File 'lib/yodel/middleware/development_server.rb', line 12

def message_type
  @message_type
end

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/yodel/middleware/development_server.rb', line 12

def status
  @status
end

Class Method Details

.read(socket) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/yodel/middleware/development_server.rb', line 24

def self.read(socket)
  message = Message.new(nil)
  message.message_type = MESSAGE_TYPES.keys[read_int(socket)]
  
  if message.request? || message.response?
    length = read_int(socket)
    message.data = Marshal.load(socket.read(length))
    
    if message.request?
      message.data['rack.input'] = StringIO.new(message.data['rack.input'])
      message.data['rack.errors'] = STDERR
    elsif message.response?
      message.data[2] = [message.data[2]]
    end
  end
  
  message
end

Instance Method Details

#exit?Boolean

Returns:

  • (Boolean)


18
# File 'lib/yodel/middleware/development_server.rb', line 18

def exit?; @message_type == :exit; end

#request?Boolean

Returns:

  • (Boolean)


15
# File 'lib/yodel/middleware/development_server.rb', line 15

def request?; @message_type == :request; end

#response?Boolean

Returns:

  • (Boolean)


16
# File 'lib/yodel/middleware/development_server.rb', line 16

def response?; @message_type == :response; end

#restart?Boolean

Returns:

  • (Boolean)


17
# File 'lib/yodel/middleware/development_server.rb', line 17

def restart?; @message_type == :restart; end

#write(socket) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yodel/middleware/development_server.rb', line 43

def write(socket)
  write_int(MESSAGE_TYPES[@message_type], socket)
  payload = nil
  
  if request?
    # convert input (StringIO) to a normal string, and
    # remove the reference to STDERR for transmission
    @env['rack.input'] = @env['rack.input'].read if @env['rack.input'].is_a?(StringIO)
    @env['rack.errors'] = nil
    payload = Marshal.dump(@env)
  elsif response?
    body = ''
    @body.each {|chunk| body << chunk.to_s}
    payload = Marshal.dump([@status, @headers, body])
  end
  
  if payload
    write_int(payload.length, socket)
    socket.write(payload)
  end
end