Class: Quaff::SipMessage

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method = nil, status_code = nil, reason = nil, req_uri = nil, body = "", headers = {}) ⇒ SipMessage

Returns a new instance of SipMessage.



6
7
8
9
10
11
12
# File 'lib/message.rb', line 6

def initialize method=nil, status_code=nil, reason=nil,
  req_uri=nil, body="", headers={}
  @headers = headers
  @method, @status_code, @reason, @requri = method, status_code, reason, req_uri
  @body = body
  @headers['Content-Length'] = [body.length]
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



4
5
6
# File 'lib/message.rb', line 4

def body
  @body
end

#headersObject

Returns the value of attribute headers.



3
4
5
# File 'lib/message.rb', line 3

def headers
  @headers
end

#methodObject

Returns the value of attribute method.



3
4
5
# File 'lib/message.rb', line 3

def method
  @method
end

#reasonObject

Returns the value of attribute reason.



3
4
5
# File 'lib/message.rb', line 3

def reason
  @reason
end

#requriObject

Returns the value of attribute requri.



3
4
5
# File 'lib/message.rb', line 3

def requri
  @requri
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/message.rb', line 3

def source
  @source
end

#status_codeObject

Returns the value of attribute status_code.



3
4
5
# File 'lib/message.rb', line 3

def status_code
  @status_code
end

Instance Method Details

#[](key) ⇒ Object



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

def [] key
  if key == "message"
    self
  elsif key == "source"
    @source
  end
end

#all_headers(hdr) ⇒ Object



32
33
34
# File 'lib/message.rb', line 32

def all_headers hdr
  return @headers[hdr]
end

#header(hdr) ⇒ Object Also known as: first_header



36
37
38
# File 'lib/message.rb', line 36

def header hdr
  return @headers[hdr][0] unless @headers[hdr].nil?
end

#to_sObject



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

def to_s
  msg = ""
  if type == :request
    msg << "#{@method} #{@requri} SIP/2.0\r\n"
  else
    msg << "SIP/2.0 #{@status_code} #{@reason}\r\n"
  end

  @headers.each do |key, value|
    if value.nil?
    elsif not value.kind_of? Array
      msg << "#{key}: #{value}\r\n"
    else value.each do |subvalue|
        msg << "#{key}: #{subvalue}\r\n"
      end
    end
  end
  if body and body != ""
    msg << "\r\n"
    msg << body
  else
    msg << "\r\n"
  end


  msg
end

#typeObject



22
23
24
25
26
27
28
29
30
# File 'lib/message.rb', line 22

def type
  if @method
    :request
  elsif @status_code
    :response
  else
    :nil
  end
end