lib/snmp/open/file_reader.rb in snmp-open-0.1.3 vs lib/snmp/open/file_reader.rb in snmp-open-0.1.4

- old
+ new

@@ -1,49 +1,31 @@ -require 'snmp/open/parser' +require 'snmp/open/command_reader' module SNMP class Open - # Test double for SNMP::Open that reads from the filesystem instead of + # Test data source for SNMP::Open that reads from the filesystem instead of # running SNMP commands. Expects a 'walk' directory to be present if #walk # will be used, and a 'get' directory if #get will be used. Within each # directory, files named according to the numeric OIDs to be used are # expected to be present, containing the output of an snmpwalk or snmpget # run over the given OID. # # Produces warnings describing an snmpbulkwalk or snmpget command that could # be used to generate a needed file, if the file is unavailable. Controlled # by the `warnings` option. - class FileReader def initialize(directory, options = {}) @directory = directory @warnings = options.delete(:warnings) - @command_generator = SNMP::Open.new(options.merge(host: '$OPTIONS')) + @command_generator = + SNMP::Open::CommandReader.new(options.merge(host: '$OPTIONS')) end - def get(oids) - return enum_for(:get, oids) unless block_given? - texts = oids.map { |o| File.read(File.join(@directory, 'get', o)) } - Parser.new(oids).parse(texts).each { |arg, *| yield(arg) } + def capture(cmd, oid, _options = {}) + outfile = File.join(cmd.to_s, oid) + File.read(File.join(@directory, outfile)) rescue Errno::ENOENT => err - if @warnings - oids.each do |oid| - warn "#{@command_generator.cli(:get, oid)} > get/#{oid}" - end - end - raise err - end - - def walk(oids, *rest) - return enum_for(:walk, oids, *rest) unless block_given? - texts = oids.map { |o| File.read(File.join(@directory, 'walk', o)) } - Parser.new(oids).parse(texts).each { |*args| yield(*args) } - rescue Errno::ENOENT => err - if @warnings - oids.each do |oid| - warn "#{@command_generator.cli(:bulkwalk, oid)} > walk/#{oid}" - end - end + warn "#{@command_generator.cli(cmd, oid)} > #{outfile}" if @warnings raise err end end # class FileReader end # class Open end # module SNMP