Class: Subscriber::MultiparMixedClient::MultipartMixedParser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(multipart_header) ⇒ MultipartMixedParser

Returns a new instance of MultipartMixedParser.

Raises:



1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
# File 'lib/nchan_tools/pubsub.rb', line 1304

def initialize(multipart_header)
  matches=/^multipart\/mixed; boundary=(?<boundary>.*)/.match multipart_header
  raise SubscriberError, "malformed Content-Type multipart/mixed header" unless matches[:boundary]
  @bound = matches[:boundary]
  @buf = ""
  @preambled = false
  @headered = nil
  @headers = {}
  @ninished = nil
end

Instance Attribute Details

#boundObject

Returns the value of attribute bound.



1303
1304
1305
# File 'lib/nchan_tools/pubsub.rb', line 1303

def bound
  @bound
end

#bufObject

Returns the value of attribute buf.



1303
1304
1305
# File 'lib/nchan_tools/pubsub.rb', line 1303

def buf
  @buf
end

#finishedObject

Returns the value of attribute finished.



1303
1304
1305
# File 'lib/nchan_tools/pubsub.rb', line 1303

def finished
  @finished
end

Instance Method Details

#<<(chunk) ⇒ Object



1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
# File 'lib/nchan_tools/pubsub.rb', line 1322

def <<(chunk)
  @buf << chunk
  #puts @buf
  repeat = true
  while repeat do
    if !@preambled && @buf.slice!(/^--#{Regexp.escape @bound}/)
      @finished = nil
      @preambled = true
      @headered = nil
    end
    if @preambled && @buf.slice!(/^(\r\n(.*?))?\r\n\r\n/m)
      @headered = true
      ($~[2]).each_line do |l|
        if l.match(/(?<name>[^:]+):\s(?<val>[^\r\n]*)/)
          @headers[$~[:name]]=$~[:val]
        end
      end
    else
      repeat = false
    end
    
    if @headered && @buf.slice!(/^(.*?)\r\n--#{Regexp.escape @bound}/m)
      @on_part.call @headers, $~[1]
      @headered = nil
      @headers.clear
      repeat = true
    else
      repeat = false
    end
    
    if (@preambled && !@headered && @buf.slice!(/^--\r\n/)) ||
      (!@preambled && @buf.slice!(/^--#{Regexp.escape @bound}--\r\n/))
      @on_finish.call
      repeat = false
    end
  end
end

#on_finish(&block) ⇒ Object



1318
1319
1320
# File 'lib/nchan_tools/pubsub.rb', line 1318

def on_finish(&block)
  @on_finish = block
end

#on_part(&block) ⇒ Object



1315
1316
1317
# File 'lib/nchan_tools/pubsub.rb', line 1315

def on_part(&block)
  @on_part = block
end