Class: Net::DND::Response

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

Overview

For good responses that contain multiple lines– fields and lookup commands –it parses those lines into an items array that it makes avaialable back to calling method.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Response

Constructor method for a Response object. This method is only called directly by our unit tests (specs). Normal interaction with the Response class is via the ‘process’ class method.



19
20
21
22
23
# File 'lib/net/dnd/response.rb', line 19

def initialize(socket)
  @items = []
  @count, @sub_count = 0, 0
  @socket = socket
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



13
14
15
# File 'lib/net/dnd/response.rb', line 13

def code
  @code
end

#countObject (readonly)

Returns the value of attribute count.



13
14
15
# File 'lib/net/dnd/response.rb', line 13

def count
  @count
end

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/net/dnd/response.rb', line 13

def error
  @error
end

#itemsObject (readonly)

Returns the value of attribute items.



13
14
15
# File 'lib/net/dnd/response.rb', line 13

def items
  @items
end

#sub_countObject (readonly)

Returns the value of attribute sub_count.



13
14
15
# File 'lib/net/dnd/response.rb', line 13

def sub_count
  @sub_count
end

Class Method Details

.process(socket) ⇒ Object

Convenience method for creating a new Response object and automatically parse data items, if they exist.



28
29
30
31
32
33
# File 'lib/net/dnd/response.rb', line 28

def self.process(socket)
  resp = Response.new(socket)
  resp.status_line
  resp.parse_items if [101, 102].include?(resp.code)
  resp
end

Instance Method Details

#ok?Boolean

Was the result of the last command a ‘good’ respose?

Returns:

  • (Boolean)


37
38
39
# File 'lib/net/dnd/response.rb', line 37

def ok?
  (200..299) === code
end

#parse_itemsObject

The result of our command has told us there are data items that need to be read and parsed. This method sets up the loops used to read the correct number of data lines. If we have a postive sub_count value, we actually build a nested array of arrays, otherwise, we build a single level array of data lines.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/net/dnd/response.rb', line 70

def parse_items
  count.times do # loop at least count times
    if sub_count > 0 # do we have an inner loop
      sub_ary = []
      sub_count.times { sub_ary << data_line }
      @items << sub_ary
    else
      @items << data_line
    end
  end
  status_line
end

#status_lineObject

The first line returned from all command sent to a DND server is the Status Line. The makup of this line not only tells us the success/failuer of the command, but whether there is more data to be read.

When there is more data, it will be contained on one or more additional data lines, called ‘items’ internally. The status line tells us if we have 1 level of n items, or 2 levels of n items, each of which has m sub-items. In the class, n and m are the count and sub_count attributes, respectively.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/net/dnd/response.rb', line 50

def status_line
  line = @socket.gets.chomp
  @code = line.match(/^(\d\d\d) /).captures[0].to_i
  case code
    when 200..299 # Command successful, ready for next
      true
    when 500..599 # Command error, set the error value to the line text
      @error = line.match(/^\d\d\d (.*)/).captures[0]
    when 101, 102 # Data command status, set the count and sub_count values
      counts = line.match(/^\d\d\d (\d+) *(\d*)/).captures
      @count = counts[0].to_i
      @sub_count = counts[1].to_i
  end
end