module GreenHat::Kind

Overall Type Parsing

Public Instance Methods

check_oj_parse?(first_line) click to toggle source
# File lib/greenhat/thing/kind.rb, line 46
def check_oj_parse?(first_line)
  Oj.load(first_line)
  true
rescue StandardError
  false
end
kind_collect() click to toggle source

rubobop:disable *

# File lib/greenhat/thing/kind.rb, line 5
def kind_collect
  # If Direct Match
  if types.key? name
    self.type = name

    return true
  end

  # Check Pattern Matches
  matches = types.select do |_k, v|
    v.pattern.any? { |x| x =~ name }
  end

  # If there is only one match
  if matches.keys.count == 1
    self.type = matches.keys.first

    true

  # TODO: Prompt for smaller selection
  elsif matches.keys.count > 1
    puts 'Multiple!'
  # History Match
  elsif ThingHistory.match? name
    self.type = ThingHistory.match(name)
  else
    prompt_for_kind
  end
end
kind_pattern_match(name) click to toggle source

Pattern Match / Look for `match_` patterns and then strip name for kind type

# File lib/greenhat/thing/kind.rb, line 89
def kind_pattern_match(name)
  pattern_match = methods.grep(/^match_/).find do |pattern|
    send(pattern).any? { |x| name =~ x }
  end

  if pattern_match
    pattern_match.to_s.split('match_', 2).last.to_sym
  else
    false
  end
end
kind_setup() click to toggle source

File Identifier

# File lib/greenhat/thing/kind.rb, line 36
def kind_setup
  self.kind = types.dig(type, :format)
  self.log = types.dig(type, :log)
end
prompt() click to toggle source
# File lib/greenhat/thing/kind.rb, line 41
def prompt
  # Quiet Exit
  @prompt ||= TTY::Prompt.new(interrupt: :exit)
end
prompt_for_kind() click to toggle source

rubocop:disable Style/SymbolProc

# File lib/greenhat/thing/kind.rb, line 54
def prompt_for_kind
  # rubocop:disable Lint/Debugger
  binding.pry if ENV['DEBUG']
  # rubocop:enable Lint/Debugger

  # Default to everything
  prompt_list = types.clone

  first_line = File.open(file) { |f| f.readline }
  json = check_oj_parse?(first_line)

  if json
    if Settings.assume_json?
      self.type = 'json'
      return true
    end

    prompt_list.select! do |_k, v|
      v.to_s.include? 'json'
    end
  end

  puts "Unable to determine file type for #{name.pastel(:yellow)}"
  puts "Use '#{'json'.pastel(:cyan)}' or '#{'raw'.pastel(:cyan)}' if there are no matches (see file_types.rb)"

  option = prompt.select('Wat is this?', prompt_list.keys.sort_by(&:length), filter: true)

  # Store for later
  ThingHistory.add(name, option)

  self.type = option
end