Class: Arista::EAPI::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/arista/eapi/response.rb

Constant Summary collapse

ERROR_CODES =
{
  1000 => Arista::EAPI::Error::GeneralError,
  1001 => Arista::EAPI::Error::InternalException,
  1002 => Arista::EAPI::Error::InvalidCommand,
  1003 => Arista::EAPI::Error::TextOnly,
  1004 => Arista::EAPI::Error::IncomptibleCommand,
  1005 => Arista::EAPI::Error::AmbiguousCommand
}
PARSERS =
[ Arista::EAPI::Parser::Show ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commands, body) ⇒ Response

Returns a new instance of Response.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/arista/eapi/response.rb', line 26

def initialize(commands, body)
  self.response = JSON.parse(body)
  self.results = []

  if response['error']
    code = response['error']['code']
    raise code < 0 ? Exception.new(response['message']) : ERROR_CODES[code]
  end

  commands.each_with_index do |cmd, idx|
    results << process_result(cmd, response['result'][idx])
  end

  results
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



24
25
26
# File 'lib/arista/eapi/response.rb', line 24

def code
  @code
end

#responseObject

Returns the value of attribute response.



24
25
26
# File 'lib/arista/eapi/response.rb', line 24

def response
  @response
end

#resultsObject

Returns the value of attribute results.



24
25
26
# File 'lib/arista/eapi/response.rb', line 24

def results
  @results
end

Instance Method Details

#process_result(cmd, result) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/arista/eapi/response.rb', line 42

def process_result(cmd, result)
  case Arista::EAPI.format_for(cmd)
  when 'json' then
    convert_hash_keys(result)
  else
    Arista::EAPI::Parser.parse(cmd, result['output'])
  end
end