class Prettyp::LanguageDetectionService

Constants

POSSIBLE_LANGUAGE_NAMES

Our supported languages

Public Instance Methods

detect_from_file(file) click to toggle source
# File lib/prettyp/language_detection_service.rb, line 8
def detect_from_file file
  file_blob = Linguist::FileBlob.new(file.path)
  language = file_blob.language
  return format_language_key(language.name) if language

  detect_from_input(file_blob.data)
end
detect_from_input(input) click to toggle source
# File lib/prettyp/language_detection_service.rb, line 16
def detect_from_input input
  classified = Linguist::Classifier.classify(Linguist::Samples::DATA, input, possible).first
  format_language_key(Linguist::Language[classified[0]].name)
end

Private Instance Methods

format_language_key(l) click to toggle source
# File lib/prettyp/language_detection_service.rb, line 26
def format_language_key l
  l.downcase.to_sym
end
possible() click to toggle source
# File lib/prettyp/language_detection_service.rb, line 22
def possible
  @possible_languaes_list ||= POSSIBLE_LANGUAGE_NAMES.map { |n| Linguist::Language.find_by_name(n) }.map(&:name)
end