Class: DNSSD::TextRecord
- Inherits:
-
Hash
- Object
- Hash
- DNSSD::TextRecord
- Defined in:
- lib/dnssd/text_record.rb
Overview
DNSSD::TextRecord is a Hash delegate that can encode its contents for DNSSD.
Class Method Summary collapse
Instance Method Summary collapse
-
#decode(text_record) ⇒ Object
Decodes
text_record
and returns a Hash. -
#encode ⇒ Object
Encodes this TextRecord.
-
#initialize(text_record = nil) ⇒ TextRecord
constructor
Creates a new TextRecord decoding an encoded
text_record
if given or from a given Hash.
Constructor Details
#initialize(text_record = nil) ⇒ TextRecord
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'
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dnssd/text_record.rb', line 43 def initialize(text_record = nil) super case text_record when Hash then text_record.dup when String then decode(text_record.dup) else Hash.new end end |
Class Method Details
.decode(text_record) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dnssd/text_record.rb', line 7 def self.decode text_record record = {} tr = text_record.unpack 'C*' until tr.empty? do size = tr.shift next if size.zero? raise ArgumentError, 'ran out of data in text record' if tr.length < size entry = tr.shift(size).pack('C*') raise ArgumentError, 'key not found' unless entry =~ /^[^=]/ key, value = entry.split '=', 2 next unless key record[key] = value end new record end |
Instance Method Details
#decode(text_record) ⇒ Object
Decodes text_record
and returns a Hash
57 58 59 |
# File 'lib/dnssd/text_record.rb', line 57 def decode(text_record) self.class.decode text_record end |
#encode ⇒ Object
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.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/dnssd/text_record.rb', line 66 def encode sort.map do |key, value| key = key.to_s raise DNSSD::Error, "empty key" if key.empty? raise DNSSD::Error, "key '#{key}' contains =" if key =~ /=/ record = value ? [key, value.to_s].join('=') : key raise DNSSD::Error, "key value pair at '#{key}' too large to encode" if record.length > 255 "#{record.length.chr}#{record}" end.join '' end |