Class: RackSpellChecker::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rack_spellchecker/request_handler.rb

Overview

Handles incoming requests from the rack application TinyMCE issues two types of requests: checking for errors and suggesting replacements

Class Method Summary collapse

Class Method Details

.check(content) ⇒ Object

checks the content for mispelled words



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack_spellchecker/request_handler.rb', line 16

def self.check(content)
  bad_words = []
  content.each do |word| 
    unless spelling_utility.check(word)
      bad_words << word
    end
  end

  { 
    "id"     => nil,
    "error"  => nil,
    "result" => bad_words.uniq
  }
end

.process(req) ⇒ Object

dispatches the request to a spelling check or alternative suggestion if TinyMCE supplied the method “getSuggestions” it will perform the spelling check



7
8
9
10
11
12
13
# File 'lib/rack_spellchecker/request_handler.rb', line 7

def self.process(req)
  if req["method"] == "getSuggestions"
    suggest_alternatives_for(req["params"][1], req["id"])
  else
    check(req["params"][1])
  end
end

.spelling_utilityObject

nodoc



40
41
42
# File 'lib/rack_spellchecker/request_handler.rb', line 40

def self.spelling_utility #nodoc
  Aspell.new("en")
end

.suggest_alternatives_for(word, position_id) ⇒ Object

suggests alternatives for a mispelled word



32
33
34
35
36
37
38
# File 'lib/rack_spellchecker/request_handler.rb', line 32

def self.suggest_alternatives_for(word, position_id)
  {
    "id"     => position_id,
    "error"  => nil,
    "result" => spelling_utility.suggest(word)[0..8]
  }
end