Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
msepcot committed Feb 15, 2010
0 parents commit 032399b
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 0 deletions.
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2010 Michael J. Sepcot (michael.sepcot@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
43 changes: 43 additions & 0 deletions README
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,43 @@
After The Deadline - Ruby Library
=================================

Ruby library for playing with After The Deadline service.
See http://www.afterthedeadline.com/api.slp for the API documentation.


Example
=======

require 'after_the_deadline'
atd = AfterTheDeadline.new("YOUR API KEY")
results = atd.check "i made some grammer and spellin misteaks."
pp results

[#<AfterTheDeadline::Error:0x1015a8500
@description="Spelling",
@precontext="some",
@string="grammer",
@suggestions=["grammar", "grammes", "grimmer", "gramme", "rammer"],
@type="spelling",
@url=nil>,
#<AfterTheDeadline::Error:0x1015a8258
@description="Spelling",
@precontext="and",
@string="spellin",
@suggestions=["spell in", "spelling"],
@type="spelling",
@url=nil>,
#<AfterTheDeadline::Error:0x1015a7ee8
@description="Spelling",
@precontext="spellin",
@string="misteaks",
@suggestions=["mistake", "mistakes", "misleads", "misreads", "steaks"],
@type="spelling",
@url=nil>,
#<AfterTheDeadline::Error:0x1015a7ba0
@description="Make I uppercase",
@precontext=nil,
@string="i",
@suggestions=["I"],
@type="grammar",
@url="http://service.afterthedeadline.com/info.slp?text=i&tags=PRP">]
98 changes: 98 additions & 0 deletions after_the_deadline.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,98 @@
require 'crack'
require 'net/http'
require 'uri'

class AfterTheDeadline
attr_accessor :api_key

BASE_URI = 'http://service.afterthedeadline.com'

def initialize(key = nil)
self.api_key = key
end

# Invoke checkDocument service with provided text and optional key.
# If no key is provided, a default key is used.
#
# Returns list of AfterTheDeadline::Error objects.
def check(data, key = nil)
results = Crack::XML.parse(perform('/checkDocument', :key => key, :data => data))['results']
return if results.nil? # we have no errors in our data

raise "Server returned an error: #{results['message']}" if results['message']
results['error'].map { |e| AfterTheDeadline::Error.new(e) }
end
alias :check_document :check

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

private
def perform(action, params)
params[:key] ||= self.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
end
end

class AfterTheDeadline::Error
attr_reader :string, :description, :precontext, :type, :suggestions, :url

def initialize(hash)
raise "#{self.class} must be initialized with a Hash" unless hash.kind_of?(Hash)
[:string, :description, :precontext, :type, :url].each do |attribute|
self.send("#{attribute}=", hash[attribute.to_s])
end
self.suggestions = hash['suggestions'].nil? ? [] : [*hash['suggestions']['option']]
end

def info(theme = nil)
return unless self.url
uri = URI.parse self.url
uri.query = (uri.query || '') + "&theme=#{theme}"
Net::HTTP.get(uri).strip
end

def to_s
"#{self.string} (#{self.description})"
end

private
attr_writer :string, :description, :precontext, :type, :suggestions, :url
end

class AfterTheDeadline::Metrics
attr_reader :spell, :grammer, :stats, :style

def initialize(array)
unless array.kind_of?(Array) && array.all? {|i| i.kind_of?(Hash) }
raise "#{self.class} must be initialized with an Array of Hashes"
end

self.spell, self.grammer, self.stats, self.style = {}, {}, {}, {}
array.each do |metric|
self.send(metric['type'])[metric['key']] = metric['value']
end
end

private
attr_writer :spell, :grammer, :stats, :style
end

0 comments on commit 032399b

Please sign in to comment.