Class: Specter::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/specter/request.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, args) ⇒ Request

Returns a new instance of Request.



23
24
25
26
27
28
29
30
# File 'lib/specter/request.rb', line 23

def initialize(command, args)
  if command.nil? || command.empty?
    raise RequestError, 'command not supplied'
  end

  @command = command.to_sym
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



21
22
23
# File 'lib/specter/request.rb', line 21

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



19
20
21
# File 'lib/specter/request.rb', line 19

def command
  @command
end

Class Method Details

.parse(raw) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/specter/request.rb', line 6

def self.parse(raw)
  begin
    message = JSON.parse(raw)
  rescue JSON::ParserError
    raise RequestError, $!
  end

  command = message['command']
  args = message['parameter'].to_s.split(',')

  new(command, args)
end

Instance Method Details

#execute(client) ⇒ Object



32
33
34
# File 'lib/specter/request.rb', line 32

def execute(client)
  client.send(command, *args)
end

#inspectObject



36
37
38
# File 'lib/specter/request.rb', line 36

def inspect
  "#<#{self.class} command:#{command}, args:#{args.inspect}>"
end