Class: Response

Inherits:
Hash show all
Defined in:
lib/rwd/net.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

file, #ids, #rwd_table, #save, #subset, #to_i

Constructor Details

#initialize(io) ⇒ Response

Returns a new instance of Response.


646
647
648
649
650
651
652
653
654
655
# File 'lib/rwd/net.rb', line 646

def initialize(io)
  @io		= io
  @response	= "HTTP/1.0 200 OK"
  @cookies	= {}
  @data	= ""
  @syncd	= false
  @stop	= false
  @at_stop	= lambda{}
  @file	= nil
end

Instance Attribute Details

#at_stopObject (readonly)

Returns the value of attribute at_stop


644
645
646
# File 'lib/rwd/net.rb', line 644

def at_stop
  @at_stop
end

#cookiesObject (readonly)

Returns the value of attribute cookies


642
643
644
# File 'lib/rwd/net.rb', line 642

def cookies
  @cookies
end

#file=(value) ⇒ Object (writeonly)

Sets the attribute file

Parameters:

  • value

    the value to set the attribute file to.


641
642
643
# File 'lib/rwd/net.rb', line 641

def file=(value)
  @file = value
end

#response=(value) ⇒ Object (writeonly)

Sets the attribute response

Parameters:

  • value

    the value to set the attribute response to.


640
641
642
# File 'lib/rwd/net.rb', line 640

def response=(value)
  @response = value
end

#stop(&block) ⇒ Object (readonly)

Returns the value of attribute stop


643
644
645
# File 'lib/rwd/net.rb', line 643

def stop
  @stop
end

Instance Method Details

#<<(s) ⇒ Object


706
707
708
# File 'lib/rwd/net.rb', line 706

def << (s)
  @data << s
end

#cleanObject


710
711
712
# File 'lib/rwd/net.rb', line 710

def clean
  @data	= ""
end

#flushObject


657
658
659
660
661
662
663
664
665
666
667
668
669
# File 'lib/rwd/net.rb', line 657

def flush
  sync

  if @file
    File.open(@file, "rb") do |f|
      while data = f.read(10_000)
        @io.write data
      end
    end
  end

  @io.close
end

#inspectObject


714
715
716
# File 'lib/rwd/net.rb', line 714

def inspect
  "(Response: %s)" % [@response, @data].join(", ")
end

#stop?Boolean

Returns:

  • (Boolean)

723
724
725
# File 'lib/rwd/net.rb', line 723

def stop?
  @stop
end

#syncObject


684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/rwd/net.rb', line 684

def sync
  size	= (@data or "").length

  if @file
    ext	= @file.scan(/\.[^\.]*$/)
    ext	= ext.shift
    ext	= ext[1..-1]	unless ext.nil?
    mimetype	= EVMime::MimeType[ext]

    self["Content-Type"]	= mimetype		unless mimetype.nil?

    size += File.size(@file)	if File.file?(@file)
  end

  self["Content-Length"]	= size

  @io.write("#{to_s}\r\n")	unless @syncd
  @io.write(@data)
  @data	= ""
  @syncd	= true
end

#to_sObject


671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/rwd/net.rb', line 671

def to_s
  res = "#{@response}\r\n"
  self.each do |k, v|
    res << "#{k}: #{v}\r\n"
  end

  @cookies.each do |k, v|
    res << "Set-Cookie: %s=%s;\r\n" % [k, v]
  end

  res
end