class AfterTheDeadline
Constants
- BASE_URI
- DEFAULT_IGNORE_TYPES
- SUPPORTED_LANGUAGES
Public Class Methods
check(data)
click to toggle source
Invoke the checkDocument service with provided text.
Returns list of AfterTheDeadline::Error
objects.
# File lib/after_the_deadline.rb, line 52 def check(data) results = Crack::XML.parse(perform('/checkDocument', :data => data))['results'] return [] if results.nil? # we have no errors in our data raise "Server returned an error: #{results['message']}" if results['message'] errors = if results['error'].kind_of?(Array) results['error'].map { |e| AfterTheDeadline::Error.new(e) } else [AfterTheDeadline::Error.new(results['error'])] end # Remove any error types we don't care about errors.reject! { |e| @@ignore_types.include?(e.description) } # Remove spelling errors from our custom dictionary errors.reject! { |e| e.type == 'spelling' && @@custom_dictionary.include?(e.string) } return errors end
Also aliased as: check_document
metrics(data)
click to toggle source
Invoke the stats service with provided text.
Returns AfterTheDeadline::Metrics
object.
# File lib/after_the_deadline.rb, line 75 def metrics(data) results = Crack::XML.parse(perform('/stats', :data => data))['scores'] return if results.nil? # we have no stats about our data AfterTheDeadline::Metrics.new results['metric'] end
Also aliased as: stats
set_api_key(key)
click to toggle source
# File lib/after_the_deadline.rb, line 34 def set_api_key(key) @@api_key = key end
set_custom_dictionary(dict)
click to toggle source
# File lib/after_the_deadline.rb, line 22 def set_custom_dictionary(dict) if dict.kind_of?(Array) @@custom_dictionary = dict elsif dict.kind_of?(String) File.open(dict) { |f| @@custom_dictionary = f.readlines.map &:strip } end end
set_ignore_types(types)
click to toggle source
# File lib/after_the_deadline.rb, line 30 def set_ignore_types(types) @@ignore_types = types if types.kind_of?(Array) end
set_language(language)
click to toggle source
# File lib/after_the_deadline.rb, line 38 def set_language(language) language.downcase! unless AfterTheDeadline::SUPPORTED_LANGUAGES.include? language raise AfterTheDeadline::Exception.new ("Unsupported language #{language}. Supported languages are #{AfterTheDeadline::SUPPORTED_LANGUAGES}") end @uri = 'en'.eql?(language) ? BASE_URI : "http://#{language}.service.afterthedeadline.com" # do not return anything (the assignation in this case) nil end
Private Class Methods
perform(action, params)
click to toggle source
# File lib/after_the_deadline.rb, line 82 def perform(action, params) params[:key] = @@api_key if @@api_key response = Net::HTTP.post_form URI.parse("#{@uri}#{action}"), params raise "Unexpected response code from AtD service: #{response.code} #{response.message}" unless response.is_a? Net::HTTPSuccess response.body end