Class: TFClient::ResponseParser

Inherits:
Object
  • Object
show all
Defined in:
lib/textflight-client/response_parser.rb

Constant Summary collapse

FIELD_DELIMITER =
"|".freeze
VARIABLE_REGEX =
/(\{[a-z_]+\}+)/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command:, textflight_command:, response:) ⇒ ResponseParser



126
127
128
129
130
# File 'lib/textflight-client/response_parser.rb', line 126

def initialize(command:, textflight_command:, response:)
  @command = command
  @textflight_command = textflight_command
  @response = response
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



121
122
123
# File 'lib/textflight-client/response_parser.rb', line 121

def command
  @command
end

#linesObject (readonly)

Returns the value of attribute lines.



124
125
126
# File 'lib/textflight-client/response_parser.rb', line 124

def lines
  @lines
end

#responseObject (readonly)

Returns the value of attribute response.



123
124
125
# File 'lib/textflight-client/response_parser.rb', line 123

def response
  @response
end

#textflight_commandObject (readonly)

Returns the value of attribute textflight_command.



122
123
124
# File 'lib/textflight-client/response_parser.rb', line 122

def textflight_command
  @textflight_command
end

Class Method Details

.camel_case_from_string(string:) ⇒ Object



99
100
101
102
103
# File 'lib/textflight-client/response_parser.rb', line 99

def self.camel_case_from_string(string:)
  string.split(" ").map do |token|
    token.capitalize
  end.join("")
end

.collect_list_items(lines:, start_index:) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/textflight-client/response_parser.rb', line 76

def self.collect_list_items(lines:, start_index:)
  items = []
  index = start_index
  loop do
    line = lines[index]
    if self.is_list_item?(line: line)
      items << line.strip
      index = index + 1
    else
      break
    end
  end
  items
end

.hash_from_line_values(line:) ⇒ Object

Returns a hash of the key=value pairs found at the end of lines



58
59
60
61
62
63
64
65
66
# File 'lib/textflight-client/response_parser.rb', line 58

def self.hash_from_line_values(line:)
  tokens = self.tokenize_line(line: line)[2..-1]
  hash = {}
  tokens.each do |token|
    key_value = token.split("=")
    hash[key_value[0].to_sym] = key_value[1]
  end
  hash
end

.is_list_item?(line:) ⇒ Boolean



68
69
70
71
72
73
74
# File 'lib/textflight-client/response_parser.rb', line 68

def self.is_list_item?(line:)
  if line && line.length != 0 && line.start_with?("\t")
    true
  else
    false
  end
end

.label_and_translation(tokens:) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/textflight-client/response_parser.rb', line 91

def self.label_and_translation(tokens:)
  if tokens[0][/Claimed by/]
    {label: "Claimed by", translation: tokens[1].split("'")[0].strip}
  else
    {label: tokens[0].split(":")[0], translation: tokens[1].split(":")[0] }
  end
end

.line_and_index_for_beginning_with(lines:, string:) ⇒ Object

returns two values



50
51
52
53
54
55
# File 'lib/textflight-client/response_parser.rb', line 50

def self.line_and_index_for_beginning_with(lines:, string:)
  lines.each_with_index do |line, index|
    return line.chomp, index if line.start_with?(string)
  end
  return nil, -1
end

.model_class_from_string(string:) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/textflight-client/response_parser.rb', line 111

def self.model_class_from_string(string:)
  if !TFClient::Models.constants.include?(string.to_sym)
    return nil
  end

  "TFClient::Models::#{string}".split("::").reduce(Object) do |obj, cls|
    obj.const_get(cls)
  end
end

.snake_case_sym_from_string(string:) ⇒ Object



105
106
107
108
109
# File 'lib/textflight-client/response_parser.rb', line 105

def self.snake_case_sym_from_string(string:)
  string.split(" ").map do |token|
    token.downcase
  end.join("_").to_sym
end

.substitute_line_values(line:) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/textflight-client/response_parser.rb', line 8

def self.substitute_line_values(line:)
  return line.chomp if !line[/\|/]
  tokens = line.chomp.split("|")

  translation = tokens[1]

  matches = translation.scan(VARIABLE_REGEX)

  return translation  if matches.empty?

  values = self.hash_from_line_values(line: line.chomp)

  with_substitutes = translation.chomp

  matches.each do |match|
    key = match[0].sub("{", "").sub("}", "").to_sym
    with_substitutes.gsub!(match[0], values[key])
  end

  with_substitutes
end

.substitute_values(lines:) ⇒ Object



30
31
32
33
34
# File 'lib/textflight-client/response_parser.rb', line 30

def self.substitute_values(lines:)
  lines.map do |line|
    self.substitute_line_values(line:line.chomp)
  end
end

.tokenize_line(line:) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/textflight-client/response_parser.rb', line 36

def self.tokenize_line(line:)
  lines = line.split(FIELD_DELIMITER)
  stripped = []
  lines.each_with_index do |line, index|
    if index == 0
      stripped << line
    else
      stripped << line.strip
    end
  end
  stripped
end

Instance Method Details

#parseObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/textflight-client/response_parser.rb', line 132

def parse
  @lines = @response.lines(chomp: true).reject { |line| line.length == 0 }
  case @textflight_command
  when "nav"
    parse_nav(command: @command)
  when "scan"
    parse_scan
  when "status"
    parse_status(command: @command)
  else
    if @response[/#{Models::STATUS_BEGIN}/]
      @response = @lines[0].chomp
      @lines = [@response]
    end

    puts ResponseParser.substitute_values(lines: @lines).join("\n")
  end
end

#parse_nav(command:) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/textflight-client/response_parser.rb', line 151

def parse_nav(command:)
  nav = TFClient::Models::Nav.new(lines: lines)
  if command != "nav-for-prompt"
    puts nav.response
  end
  nav
end

#parse_scanObject



159
160
161
162
163
# File 'lib/textflight-client/response_parser.rb', line 159

def parse_scan
  scan = TFClient::Models::Scan.new(lines: lines)
  puts scan.response
  scan
end

#parse_status(command:) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/textflight-client/response_parser.rb', line 165

def parse_status(command:)
  if command == "status-for-prompt"
    TFClient::Models::Status.new(lines: lines)
  else
    _, index_start =
      ResponseParser.line_and_index_for_beginning_with(
        lines: @lines,
        string: Models::STATUS_BEGIN
      )
    if index_start == -1
      puts ResponseParser.substitute_values(lines: @lines).join("\n")
    end

    _, index_end =
      ResponseParser.line_and_index_for_beginning_with(
        lines: @lines,
        string: Models::STATUS_END
      )

    if index_start != 0
      lines_before_status = @lines[0..index_start - 1]
      puts ResponseParser.substitute_values(
        lines: lines_before_status
      ).join("\n")
    else
      lines_after_status = @lines[index_end + 1..-1]
      puts ResponseParser.substitute_values(
        lines: lines_after_status
      ).join("\n")

      Models::StatusReport.new(lines: @lines[index_start...index_end])
    end
  end
end