class TFClient::ResponseParser

Constants

FIELD_DELIMITER
VARIABLE_REGEX

Attributes

command[R]
lines[R]
response[R]
textflight_command[R]

Public Class Methods

camel_case_from_string(string:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source

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

# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source

returns two values

# 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:) click to toggle source
# 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
new(command:, textflight_command:, response:) click to toggle source
# File lib/textflight-client/response_parser.rb, line 126
def initialize(command:, textflight_command:, response:)
  @command = command
  @textflight_command = textflight_command
  @response = response
end
snake_case_sym_from_string(string:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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:) click to toggle source
# 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

Public Instance Methods

parse() click to toggle source
# 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:) click to toggle source
# 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_scan() click to toggle source
# 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:) click to toggle source
# 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