class UPnP::SSDP::Notification

Holds information about a NOTIFY message. For an alive notification, all fields will be present. For a byebye notification, location, #max_age and server will be nil.

Attributes

date[R]

Date the notification was received

host[R]

Host the notification was sent from

location[R]

Location of the advertised service or device

max_age[R]

Maximum age the advertisement is valid for

name[R]

Unique Service Name of the advertisement

port[R]

Port the notification was sent from

server[R]

Server name and version of the advertised service or device

sub_type[R]

Notification sub-type

type[R]

Type of the advertised service or device

Public Class Methods

new(date, max_age, host, port, location, type, sub_type, server, name) click to toggle source

Creates a new Notification

# File lib/UPnP/SSDP.rb, line 147
def initialize(date, max_age, host, port, location, type, sub_type,
               server, name)
  @date = date
  @max_age = max_age
  @host = host
  @port = port
  @location = location
  @type = type
  @sub_type = sub_type
  @server = server
  @name = name
end
parse(advertisement) click to toggle source

Parses a NOTIFY advertisement into its component pieces

# File lib/UPnP/SSDP.rb, line 115
def self.parse(advertisement)
  advertisement = advertisement.gsub "\r", ''

  advertisement =~ %r^host:\s*(\S*)/
  host, port = $1.split ':'

  advertisement =~ %r^nt:\s*(\S*)/
  type = $1

  advertisement =~ %r^nts:\s*(\S*)/
  sub_type = $1

  advertisement =~ %r^usn:\s*(\S*)/
  name = $1

  if sub_type == 'ssdp:alive' then
    advertisement =~ %r^cache-control:\s*max-age\s*=\s*(\d+)/
    max_age = Integer $1

    advertisement =~ %r^location:\s*(\S*)/
    location = URI.parse $1

    advertisement =~ %r^server:\s*(.*)/
    server = $1.strip
  end

  new Time.now, max_age, host, port, location, type, sub_type, server, name
end

Public Instance Methods

alive?() click to toggle source

Returns true if this is a notification for a resource being alive

# File lib/UPnP/SSDP.rb, line 163
def alive?
  sub_type == 'ssdp:alive'
end
byebye?() click to toggle source

Returns true if this is a notification for a resource going away

# File lib/UPnP/SSDP.rb, line 170
def byebye?
  sub_type == 'ssdp:byebye'
end
inspect() click to toggle source

A friendlier inspect

# File lib/UPnP/SSDP.rb, line 177
def inspect
  location = " #{@location}" if @location
  "#<#{self.class}:0x#{object_id.to_s 16} #{@type} #{@sub_type}#{location}>"
end