Skip to content

Commit

Permalink
AtD no longer requires you to register for an API key to use the serv…
Browse files Browse the repository at this point in the history
…ice.
  • Loading branch information
msepcot committed Apr 21, 2010
1 parent 13c36e7 commit 95637dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 28 deletions.
6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Examples
========

require 'after_the_deadline'
AfterTheDeadline('xxx', nil, nil) # set the API Key, no custom dictionary, accept all error types
AfterTheDeadline(nil, nil) # no custom dictionary, accept all error types

# No Errors
AfterTheDeadline.check 'this text is clean.'
Expand All @@ -36,7 +36,7 @@ Ignoring Specific Types of Errors
=================================

require 'after_the_deadline'
AfterTheDeadline('xxx', nil, ['Passive voice'])
AfterTheDeadline(nil, ['Passive voice'])

# Skip the Passive Voice Error
errors = AfterTheDeadline.check 'this text should be written in a passive voice.'
Expand All @@ -47,6 +47,6 @@ Using a Custom Dictionary
=========================

require 'after_the_deadline'
AfterTheDeadline('xxx', ['Sepcot']) # or AfterTheDeadline('xxx', 'path/to/filename')
AfterTheDeadline(['Sepcot']) # or AfterTheDeadline('path/to/filename')
AfterTheDeadline.check "My last name, Sepcot, is very unique."
=> []
32 changes: 7 additions & 25 deletions after_the_deadline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@
require 'net/http'
require 'uri'

def AfterTheDeadline(key, dictionary = nil, types = AfterTheDeadline::DEFAULT_IGNORE_TYPES)
AfterTheDeadline.set_api_key(key)
def AfterTheDeadline(dictionary = nil, types = AfterTheDeadline::DEFAULT_IGNORE_TYPES)
AfterTheDeadline.set_custom_dictionary(dictionary)
AfterTheDeadline.set_ignore_types(types)
nil
end

class AfterTheDeadline
@@api_key = nil
@@custom_dictionary = []
@@ignore_types = []

BASE_URI = 'http://service.afterthedeadline.com'
DEFAULT_IGNORE_TYPES = ['Bias Language', 'Cliches', 'Complex Expression', 'Diacritical Marks', 'Double Negatives', 'Hidden Verbs', 'Jargon Language', 'Passive voice', 'Phrases to Avoid', 'Redundant Expression']

class <<self
def set_api_key(key)
@@api_key = key
end

def set_custom_dictionary(dict)
if dict.kind_of?(Array)
@@custom_dictionary = dict
Expand All @@ -34,12 +28,11 @@ def set_ignore_types(types)
@@ignore_types = types if types.kind_of?(Array)
end

# Invoke checkDocument service with provided text and optional key.
# If no key is provided, a default key is used.
# Invoke the checkDocument service with provided text.
#
# Returns list of AfterTheDeadline::Error objects.
def check(data, key = nil)
results = Crack::XML.parse(perform('/checkDocument', :key => key, :data => data))['results']
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']
Expand All @@ -58,28 +51,17 @@ def check(data, key = nil)
end
alias :check_document :check

# Invoke stats service with provided text and optional key.
# If no key is provided, a default key is used.
# Invoke the stats service with provided text.
#
# Returns AfterTheDeadline::Metrics object.
def metrics(data, key = nil)
results = Crack::XML.parse(perform('/stats', :key => key, :data => data))['scores']
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
alias :stats :metrics

# Invoke the verify service with optional key.
# If no key is provided, a default key is used.
#
# Returns boolean indicating validity of key.
def verify(key = nil)
'valid' == perform('/verify', :key => key).strip
end

def perform(action, params)
params[:key] ||= @@api_key
raise 'Please provide key as argument or set the api_key attribute first' unless params[:key]
response = Net::HTTP.post_form URI.parse(BASE_URI + action), params
raise "Unexpected response code from AtD service: #{response.code} #{response.message}" unless response.is_a? Net::HTTPSuccess
response.body
Expand Down

0 comments on commit 95637dd

Please sign in to comment.