class Defensor

Constants

ALLOWED_OPTIONS
API_HOST
API_VERSION

You shouldn't modify these values unless you really know what you are doing. And then again…

CLIENT
FORMAT
KEEP_ALIVE
LIB_VERSION

You should't modify anything below this line.

ROOT_NODE
USER_AGENT

Attributes

options[RW]
signature[RW]

Public Class Methods

api_path(action = nil, id = nil) click to toggle source
# File lib/defensor.rb, line 51
def self.api_path(action = nil, id = nil)
  path = "/#{API_VERSION}/users/#{@@api_key}"
  path += "/#{action}" if action
  path += "/#{id}" if id
  path += ".#{FORMAT}"
end
get_basic_stats() click to toggle source

Get basic statistics for the current user @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio

# File lib/defensor.rb, line 60
def self.get_basic_stats
  respond get(api_path("basic-stats"))
end
get_extended_stats(data) click to toggle source

Get more exhaustive statistics for the current user @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio

# File lib/defensor.rb, line 67
def self.get_extended_stats(data)
  result = get(api_path("extended-stats"), :query => data)
  code = result.code
  result = result[ROOT_NODE]

  0.upto(result["data"].size - 1) do |i|
    result["data"][i]["date"] = Date.parse(result["data"][i]["date"])
  end

  [code, result]
end
get_user() click to toggle source

Get information about the api key

# File lib/defensor.rb, line 47
def self.get_user
  respond get(api_path)
end
handle_post_document_async_callback(request) click to toggle source

Takes the request object (Rails, Sinatra, Merb) of an async request callback and returns a hash containing the status of the document being analyzed. @param [ActionController::Request, Sinatra::Request, String] request The request object created after Defensio POSTed to your site, or a string representation of the POST data. @return [Hash] Status of the document

# File lib/defensor.rb, line 96
def self.handle_post_document_async_callback(request)
  if request.is_a?(String)
    data = request
  elsif request.respond_to?(:body) && request.body.respond_to?(:read)
    data = request.body.read
  else
    raise ArgumentError, "Unknown request type: #{request.class}"
  end

  Defensor.parse_body(data)
end
new(*options) click to toggle source
# File lib/defensor.rb, line 108
def initialize(*options)
  check_key
  @options = options[0].reject {|k, v| !(ALLOWED_OPTIONS.include? k)}
  @signature = @options['signature'] if @options['signature']
end
parse_body(str) click to toggle source
# File lib/defensor.rb, line 84
def self.parse_body(str)
  if FORMAT == :json
    return JSON.parse(str)[ROOT_NODE]
  else
    raise(NotImplementedError, "This library doesn't support this format: #{FORMAT}")
  end
end
post_profanity_filter(data) click to toggle source

Filter a set of values based on a pre-defined dictionary

# File lib/defensor.rb, line 80
def self.post_profanity_filter(data)
  respond post(api_path("profanity-filter"), :body => data)
end
respond(response) click to toggle source
# File lib/defensor.rb, line 42
def self.respond(response)
  [response.code, response[ROOT_NODE]]
end

Public Instance Methods

check_document() click to toggle source
# File lib/defensor.rb, line 118
def check_document
  raise NoContentException if content.nil? || content.empty?
end
check_key() click to toggle source
# File lib/defensor.rb, line 114
def check_key
  raise NoApiKeyException if @@api_key.nil? || @@api_key.empty?
end
content() click to toggle source
# File lib/defensor.rb, line 122
def content
  @options[:content]
end
get_document(signature=nil) click to toggle source

Get the status of an existing document @param [String] signature The signature of the document to retrieve @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio

# File lib/defensor.rb, line 140
def get_document(signature=nil)
  @signature ||= signature
  if @signature
    Defensor.respond Defensor.get(Defensor.api_path("documents", @signature))
  else
    raise NoSignatureException
  end
end
post_document() click to toggle source

Create and analyze a new document @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio

# File lib/defensor.rb, line 129
def post_document
  check_document
  response = Defensor.post(Defensor.api_path("documents"), :body => { :client => CLIENT }.merge(@options))
  status = response[ROOT_NODE]["status"]
  @signature = response[ROOT_NODE]["signature"] if status == "success"
  Defensor.respond response
end
put_document(signature=nil, data) click to toggle source

Modify the properties of an existing document @param [String] signature The signature of the document to modify @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio

# File lib/defensor.rb, line 153
def put_document(signature=nil, data)
  @signature ||= signature
  if @signature
    Defensor.respond Defensor.put(Defensor.api_path("documents", @signature), :body => data)
  else
    raise NoSignatureException
  end
end