class RaLoSe::Command

Constants

RED
REQUEST_ID_RE
a9c56d04-d706-49ae-b0a2-e7636c650d5e
RESET_COLOR

Public Class Methods

new() click to toggle source
# File lib/ralose/command.rb, line 12
def initialize
  @colorized_output = true
  @case_insensitive = false

  parse_options

  @current_request_id    = nil
  @current_request_lines = []
  @print_current_request = false

  @query = Regexp.new(ARGV.shift, @case_insensitive)
end
run!() click to toggle source
# File lib/ralose/command.rb, line 25
def self.run!
  new.run!
end

Public Instance Methods

run!() click to toggle source
# File lib/ralose/command.rb, line 29
def run!
  # Pipe handling based on: https://www.jstorimer.com/blogs/workingwithcode/7766125-writing-ruby-scripts-that-respect-pipelines

  # Read from file passed in ARGV, or STDIN.
  ARGF.each_line do |line|

    request_id = line[REQUEST_ID_RE]

    if request_id.nil?
      next
    end

    if request_id != @current_request_id
      print_current_request
      new_request(request_id)
    end

    if line.match?(@query)
      @print_current_request = true

      if @colorized_output
        line.gsub!(@query, "#{RED}\\0#{RESET_COLOR}")
      end
    end

    @current_request_lines << line

  end

  print_current_request

end

Private Instance Methods

new_request(request_id) click to toggle source
# File lib/ralose/command.rb, line 64
def new_request(request_id)
  @current_request_id    = request_id
  @current_request_lines = []
  @print_current_request = false
end
parse_options() click to toggle source
# File lib/ralose/command.rb, line 93
def parse_options
  OptionParser.new do |options|
    # This banner is the first line of the help documentation.
    options.banner = "Usage: ralose [options] [files]\n\n" \
      "Prints out all lines for a Rails request where one line in that request " \
      "matches the passed search string."

    # Separator just adds a new line with the specified text.
    options.separator ""
    options.separator "Specific options:"

    options.on("-i", "Perform case insensitive matching. By default, ralose is case sensitive.") do |be_case_insensitive|
      @case_insensitive = be_case_insensitive
    end

    options.on("--no-color", "Disable colorized output") do |use_color|
      @colorized_output = use_color
    end

    # on_tail says that this option should appear at the bottom of
    # the options list.
    options.on_tail("-h", "--help", "You're looking at it!") do
      $stderr.puts options
      exit 1
    end
  end.parse!
end
print_current_request() click to toggle source
print_line(line) click to toggle source