Class: SNMP::ObjectId

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

Direct Known Subclasses

ObjectName

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = []) ⇒ ObjectId

Create an object id. The input is expected to be either a string in the format “n.n.n.n.n.n” or an array of integers.



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/snmp/varbind.rb', line 146

def initialize(id=[])
    if id.nil?
        raise ArgumentError
    elsif id.respond_to? :to_str
        super(make_integers(id.to_str.split(".")))
    else
        super(make_integers(id.to_ary))
    end
rescue ArgumentError
    raise ArgumentError, "#{id.inspect}:#{id.class} not a valid object ID"
end

Class Method Details

.decode(value_data) ⇒ Object



134
135
136
# File 'lib/snmp/varbind.rb', line 134

def self.decode(value_data)
    ObjectId.new(decode_object_id_value(value_data))
end

Instance Method Details

#asn1_typeObject



138
139
140
# File 'lib/snmp/varbind.rb', line 138

def asn1_type
    "OBJECT IDENTIFIER"
end

#encodeObject



174
175
176
# File 'lib/snmp/varbind.rb', line 174

def encode
    encode_object_id(self)
end

#index(parent_tree) ⇒ Object

Returns an index based on the difference between this ObjectId and the provided parent ObjectId.

For example, ObjectId.new(“1.3.6.1.5”).index(“1.3.6.1”) returns an ObjectId of “5”.



201
202
203
204
205
206
207
208
209
210
# File 'lib/snmp/varbind.rb', line 201

def index(parent_tree)
    parent_tree = make_object_id(parent_tree)
    if not subtree_of?(parent_tree)
        raise ArgumentError, "#{self.to_s} not a subtree of #{parent_tree.to_s}"
    elsif self.length == parent_tree.length
        raise ArgumentError, "OIDs are the same"
    else
        ObjectId.new(self[parent_tree.length..-1])
    end
end

#inspectObject



170
171
172
# File 'lib/snmp/varbind.rb', line 170

def inspect
    "[#{self.to_s}]"
end

#subtree_of?(parent_tree) ⇒ Boolean

Returns true if this ObjectId is a subtree of the provided parent tree ObjectId. For example, “1.3.6.1.5” is a subtree of “1.3.6.1”.

Returns:

  • (Boolean)


182
183
184
185
186
187
188
189
190
191
192
# File 'lib/snmp/varbind.rb', line 182

def subtree_of?(parent_tree)
    parent_tree = make_object_id(parent_tree)
    if parent_tree.length > self.length
        false
    else
        parent_tree.each_index do |i|
            return false if parent_tree[i] != self[i]
        end
        true
    end
end

#to_oidObject



162
163
164
# File 'lib/snmp/varbind.rb', line 162

def to_oid
    self
end

#to_sObject



166
167
168
# File 'lib/snmp/varbind.rb', line 166

def to_s
    self.join('.')
end

#to_varbindObject



158
159
160
# File 'lib/snmp/varbind.rb', line 158

def to_varbind
    VarBind.new(self, Null)
end