Class: Hector::Response

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, *args) ⇒ Response

Returns a new instance of Response.



26
27
28
29
30
31
32
33
# File 'lib/hector/response.rb', line 26

def initialize(command, *args)
  @command = command.to_s.upcase
  @args = args

  options = args.pop if args.last.is_a?(Hash)
  @text = options[:text] if options
  @source = options[:source] if options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/hector/response.rb', line 3

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



3
4
5
# File 'lib/hector/response.rb', line 3

def command
  @command
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/hector/response.rb', line 3

def source
  @source
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/hector/response.rb', line 4

def text
  @text
end

Class Method Details

.apportion_text(args, *base_args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hector/response.rb', line 7

def apportion_text(args, *base_args)
  base_response = Response.new(*base_args)
  max_length = 510 - base_response.to_s.bytesize

  args.inject([args.shift.dup]) do |texts, arg|
    if texts.last.bytesize + arg.bytesize + 1 >= max_length
      texts << arg.dup
    else
      texts.last << " " << arg
    end
    texts
  end.map do |text|
    base_response.dup.tap do |response|
      response.text = text
    end
  end
end

Instance Method Details

#event_nameObject



35
36
37
# File 'lib/hector/response.rb', line 35

def event_name
  "received_#{command.downcase}"
end

#to_sObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/hector/response.rb', line 39

def to_s
  [].tap do |line|
    line.push(":#{source}") if source
    line.push(command)
    line.concat(args)
    line.push(":#{text}") if text
  end.map do |arg|
    if arg.respond_to?(:force_encoding) then arg.force_encoding("UTF-8") else arg end
  end.join(" ")[0, 510] + "\r\n"
end