Parent

DNSSD::TextRecord

DNSSD::TextRecord is a Hash delegate that can encode its contents for DNSSD.

Public Class Methods

new(text_record = nil) click to toggle source

Creates a new TextRecord decoding an encoded text_record if given or from a given Hash.

  DNSSD::TextRecord.new "\003k=v"

or

  DNSSD::TextRecord.new 'k' => 'v'
    # File lib/dnssd/text_record.rb, line 18
18:   def initialize(text_record = nil)
19:     super case text_record
20:           when Hash then
21:             text_record.dup
22:           when String then
23:             decode(text_record.dup)
24:           else
25:             Hash.new
26:           end
27:   end

Public Instance Methods

decode(text_record) click to toggle source

Decodes text_record and returns a Hash

    # File lib/dnssd/text_record.rb, line 32
32:   def decode(text_record)
33:     record = {}
34: 
35:     tr = text_record.unpack 'C*'
36: 
37:     until tr.empty? do
38:       size  = tr.shift
39: 
40:       next if size.zero?
41: 
42:       raise ArgumentError, 'ran out of data in text record' if tr.length < size
43: 
44:       entry = tr.shift(size).pack('C*')
45: 
46:       raise ArgumentError, 'key not found' unless entry =~ /^[^=]/
47: 
48:       key, value = entry.split '=', 2
49: 
50:       next unless key
51: 
52:       record[key] = value
53:     end
54: 
55:     record
56:   end
encode() click to toggle source

Encodes this TextRecord. A key value pair must be less than 255 bytes in length. Keys longer than 14 bytes may not be compatible with all clients.

    # File lib/dnssd/text_record.rb, line 63
63:   def encode
64:     sort.map do |key, value|
65:       key = key.to_s
66: 
67:       raise DNSSD::Error, "empty key" if key.empty?
68:       raise DNSSD::Error, "key '#{key}' contains =" if key =~ /=/
69: 
70:       record = value ? [key, value.to_s].join('=') : key
71: 
72:       raise DNSSD::Error, "key value pair at '#{key}' too large to encode" if
73:         record.length > 255
74: 
75:       "#{record.length.chr}#{record}"
76:     end.join ''
77:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.