Class: Mikrotik::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/mikrotik/command.rb

Overview

Class encapsulating a API command to be executed on the device

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Command) initialize(*command_path)

Creates a new command object

Parameters:

  • path (Array<String, Symbol>)

    Path of the API command



31
32
33
34
35
36
# File 'lib/mikrotik/command.rb', line 31

def initialize(*command_path)    
  @command = command_path.collect { |part| "#{part}".gsub('_', '-') }.join('/')
  @command = "/#{@command}" unless @command.start_with?('/')
  @options = {}
  @replies = []
end

Instance Attribute Details

- (String) command (readonly)

Path to the command

Returns:

  • (String)

    Path to the command



27
28
29
# File 'lib/mikrotik/command.rb', line 27

def command
  @command
end

- (Hash) options (readonly)

All option name/value pairs set on the command

Returns:

  • (Hash)

    All option name/value pairs set on the command



24
25
26
# File 'lib/mikrotik/command.rb', line 24

def options
  @options
end

- (Array<Mikrotik::Protocol::Sentence>) replies (readonly)

All sentences sent in response to this command so far

Returns:



21
22
23
# File 'lib/mikrotik/command.rb', line 21

def replies
  @replies
end

Instance Method Details

- (Object) done {|replies| ... }

Fired when the command completes

Yields:

  • (replies)

Yield Parameters:



8
# File 'lib/mikrotik/command.rb', line 8

has_events :done

- (String) encoded

Encodes the command for transmission

Returns:

  • (String)

    Command encoded as an API sentence in binary string format



70
71
72
73
74
75
76
77
78
79
# File 'lib/mikrotik/command.rb', line 70

def encoded    
  @command.to_mikrotik_word + @options.collect { |key, value|
    case value.class.name
    when 'Array'        
      [key, value.collect { |item| "#{item}" }.join(',')].join '='        
    else
      "#{key}=#{value}"
    end
  }.map { |option| option.to_mikrotik_word }.join + 0x00.chr
end

- (Object) reply {|reply| ... }

Fired on each reply received from the command

Yields:

  • (reply)

Yield Parameters:



13
# File 'lib/mikrotik/command.rb', line 13

has_event :reply

- (self) returning(*properties)

Specifies what properties should be returned by the device in response to this command

Parameters:

  • properties (Array<String, Symbol>)

    List of the names of properties to request in response

Returns:

  • (self)


60
61
62
63
64
65
66
# File 'lib/mikrotik/command.rb', line 60

def returning(*properties)
  @options['=.proplist'] ||= []
  properties.each do |property|
    @options['=.proplist'] << "#{property}"
  end
  self
end

- (Object) trap {|error_message| ... }

Fired when an error response is received from the command

Yields:

  • (error_message)

Yield Parameters:

  • error_message (false, String)

    Error description



18
# File 'lib/mikrotik/command.rb', line 18

has_event :trap

- (Object) where(conditions = {})

Adds querying conditions to the command

Parameters:

  • conditions (Hash) (defaults to: {})


49
50
51
52
53
54
# File 'lib/mikrotik/command.rb', line 49

def where(conditions = {})
  conditions.each_pair do |property, value|
    @options["?#{property}"] = value
  end
  self
end

- (Object) with(options = {})

Adds property names and values to the command

Parameters:

  • options (Hash) (defaults to: {})


40
41
42
43
44
45
# File 'lib/mikrotik/command.rb', line 40

def with(options = {})
  options.each_pair do |option, value|
    @options["=#{option}"] = value
  end
  self
end