Class: Romp

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

Defined Under Namespace

Classes: Frame

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Romp

Returns a new instance of Romp.



14
15
16
17
18
19
20
# File 'lib/romp.rb', line 14

def initialize(*args)
  if args.size == 1
    @io = args.first
  else
    @io = TCPSocket.new(*args)
  end
end

Instance Method Details

#closeObject



22
23
24
# File 'lib/romp.rb', line 22

def close
  @io.close
end

#receive(expected_type = nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/romp.rb', line 47

def receive(expected_type = nil)
  type    = @io.gets.chomp.downcase.to_sym
  headers = {}
  while line = @io.gets
    key, val = line.chomp.split(':')
    break unless val
    headers[key.to_sym] = val
  end

  if len = headers[:content_length]
    body = @io.read(len)
    assert_read("\0")
  else
    body = ''
    while c = @io.read(1)
      break if c == "\0"
      body << c
    end
  end
  assert_read("\n")
  raise "expected #{expected_type} frame, got #{type}\n#{headers[:message]}\n#{body}" if expected_type and type != expected_type
  Frame.new(type, headers, body)
end

#send(body, headers = {}) ⇒ Object



33
34
35
# File 'lib/romp.rb', line 33

def send(body, headers = {})
  send_frame(:send, headers, body)
end

#with_connection(headers = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/romp.rb', line 37

def with_connection(headers = {})
  send_frame(:connect, headers)

  begin
    yield receive(:connected)
  ensure
    send_frame(:disconnect)
  end
end