Class: SNMP::IpAddress

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value_data) ⇒ IpAddress

Create an IpAddress object. The constructor accepts either a raw four-octet string or a formatted string of integers separated by dots (i.e. “10.1.2.3”).



246
247
248
249
250
251
252
253
254
# File 'lib/snmp/varbind.rb', line 246

def initialize(value_data)
    ip = value_data.to_str
    if ip.length > 4
        ip = parse_string(ip)
    elsif ip.length != 4
        raise InvalidIpAddress, "Expected 4 octets or formatted string, got #{value_data.inspect}"
    end
    @value = ip
end

Class Method Details

.decode(value_data) ⇒ Object



232
233
234
# File 'lib/snmp/varbind.rb', line 232

def decode(value_data)
    IpAddress.new(value_data)
end

Instance Method Details

#==(other) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/snmp/varbind.rb', line 278

def ==(other)
    if other.respond_to? :to_str
        return @value.eql?(other.to_str)
    else
        return false
    end
end

#asn1_typeObject



237
238
239
# File 'lib/snmp/varbind.rb', line 237

def asn1_type
    "IpAddress"
end

#encodeObject



294
295
296
# File 'lib/snmp/varbind.rb', line 294

def encode
    encode_tlv(IpAddress_TAG, @value)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


286
287
288
# File 'lib/snmp/varbind.rb', line 286

def eql?(other)
    self == other
end

#hashObject



290
291
292
# File 'lib/snmp/varbind.rb', line 290

def hash
    @value.hash
end

#to_oidObject



272
273
274
275
276
# File 'lib/snmp/varbind.rb', line 272

def to_oid
    oid = ObjectId.new
    @value.each_byte { |b| oid << b }
    oid
end

#to_sObject

Returns a formatted, dot-separated string representing this IpAddress.



266
267
268
269
270
# File 'lib/snmp/varbind.rb', line 266

def to_s
    octets = []
    @value.each_byte { |b| octets << b.to_s }
    octets.join('.')    
end

#to_strObject

Returns a raw four-octet string representing this IpAddress.



259
260
261
# File 'lib/snmp/varbind.rb', line 259

def to_str
    @value.dup
end