Class: RTSP::Response
- Inherits:
-
Object
- Object
- RTSP::Response
- Defined in:
- lib/rtsp/response.rb
Overview
Parses raw response data from the server/client and turns it into attr_readers.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#rtsp_version ⇒ Object
readonly
Returns the value of attribute rtsp_version.
Instance Method Summary collapse
-
#extract_status_line(line) ⇒ Object
Pulls out the RTSP version, response code, and response message (AKA the status line info) into instance variables.
-
#initialize(raw_response) ⇒ Response
constructor
server/client.
-
#inspect ⇒ String
Custom redefine to make sure all the dynamically created instance variables are displayed when this method is called.
-
#parse_body(body) ⇒ SDP::Description, String
Reads through each line of the RTSP response body and parses it if needed.
-
#parse_head(head) ⇒ Object
Reads through each header line of the RTSP response, extracts the response code, response message, response version, and creates a snake-case accessor with that value set.
-
#split_head_and_body_from(raw_response) ⇒ Array<String>
Takes the raw response text and splits it into a 2-element Array, where 0 is the text containing the headers and 1 is the text containing the body.
-
#to_s ⇒ String
The unparsed response as a String.
Constructor Details
#initialize(raw_response) ⇒ Response
server/client.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/rtsp/response.rb', line 17 def initialize(raw_response) if raw_response.nil? || raw_response.empty? raise RTSP::Error, "#{self.class} received nil string--this shouldn't happen." end @raw_response = raw_response head, body = split_head_and_body_from @raw_response parse_head(head) @body = parse_body(body) end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/rtsp/response.rb', line 13 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
11 12 13 |
# File 'lib/rtsp/response.rb', line 11 def code @code end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
12 13 14 |
# File 'lib/rtsp/response.rb', line 12 def @message end |
#rtsp_version ⇒ Object (readonly)
Returns the value of attribute rtsp_version.
10 11 12 |
# File 'lib/rtsp/response.rb', line 10 def rtsp_version @rtsp_version end |
Instance Method Details
#extract_status_line(line) ⇒ Object
Pulls out the RTSP version, response code, and response message (AKA the status line info) into instance variables.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rtsp/response.rb', line 70 def extract_status_line(line) line =~ /(RTSP|HTTP)\/(\d\.\d) (\d\d\d) ([^\r\n]+)/ @rtsp_version = $2 @code = $3.to_i @message = $4 if @rtsp_version.nil? raise RTSP::Error, "Status line corrupted: #{line}" end end |
#inspect ⇒ String
Custom redefine to make sure all the dynamically created instance variables are displayed when this method is called.
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rtsp/response.rb', line 39 def inspect me = "#<#{self.class.name}:#{self.__id__} " self.instance_variables.each do |variable| me << "#{variable}=#{instance_variable_get(variable).inspect}, " end me.sub!(/, $/, "") me << ">" me end |
#parse_body(body) ⇒ SDP::Description, String
Reads through each line of the RTSP response body and parses it if needed. Returns a SDP::Description if the Content-Type is ‘application/sdp’, otherwise returns the String that was passed in.
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rtsp/response.rb', line 119 def parse_body body if body =~ /^(\r\n|\n)/ body.gsub!(/^(\r\n|\n)/, '') end if @content_type == "application/sdp" SDP.parse body else body end end |
#parse_head(head) ⇒ Object
Reads through each header line of the RTSP response, extracts the response code, response message, response version, and creates a snake-case accessor with that value set.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/rtsp/response.rb', line 86 def parse_head head lines = head.split "\r\n" lines.each_with_index do |line, i| if i == 0 extract_status_line(line) next end if line.include? "Session: " value = {} line =~ /Session: ([\w\\$\-\.\+]+)/ value[:session_id] = $1 if line =~ /timeout=(.+)/ value[:timeout] = $1.to_i end create_reader("session", value) elsif line.include? ": " header_and_value = line.strip.split(":", 2) header_name = header_and_value.first.downcase.gsub(/-/, "_") create_reader(header_name, header_and_value[1].strip) end end end |
#split_head_and_body_from(raw_response) ⇒ Array<String>
Takes the raw response text and splits it into a 2-element Array, where 0 is the text containing the headers and 1 is the text containing the body.
58 59 60 61 62 63 64 |
# File 'lib/rtsp/response.rb', line 58 def split_head_and_body_from raw_response head_and_body = raw_response.split("\r\n\r\n", 2) head = head_and_body.first body = head_and_body.last == head ? nil : head_and_body.last [head, body] end |
#to_s ⇒ String
Returns The unparsed response as a String.
31 32 33 |
# File 'lib/rtsp/response.rb', line 31 def to_s @raw_response end |