Class: Defensio

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/defensio.rb

Constant Summary collapse

API_VERSION =

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

2.0
API_HOST =
"http://api.defensio.com"
LIB_VERSION =

You should’t modify anything below this line.

"0.9.1"
ROOT_NODE =
"defensio-result"
FORMAT =
:yaml
USER_AGENT =
"Defensio-Ruby #{LIB_VERSION}"
CLIENT =
"Defensio-Ruby | #{LIB_VERSION} | Carl Mercier | [email protected]"
KEEP_ALIVE =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, client = CLIENT) ⇒ Defensio

Returns a new instance of Defensio.



34
35
36
37
# File 'lib/defensio.rb', line 34

def initialize(api_key, client = CLIENT)
  @client = client
  @api_key = api_key
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



32
33
34
# File 'lib/defensio.rb', line 32

def api_key
  @api_key
end

#clientObject (readonly)

Returns the value of attribute client.



32
33
34
# File 'lib/defensio.rb', line 32

def client
  @client
end

Class Method Details

.handle_post_document_async_callback(request) ⇒ Object

See handle_post_document_async_callback



110
111
112
# File 'lib/defensio.rb', line 110

def self.handle_post_document_async_callback(request)
  Defensio.new(nil).handle_post_document_async_callback(request)
end

Instance Method Details

#get_basic_statsArray

Get basic statistics for the current user

Returns:

  • (Array)

    An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio



69
70
71
# File 'lib/defensio.rb', line 69

def get_basic_stats
  respond self.class.get(api_path("basic-stats"))
end

#get_document(signature) ⇒ Array

Get the status of an existing document

Parameters:

  • signature (String)

    The signature of the document to retrieve

Returns:

  • (Array)

    An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio



55
56
57
# File 'lib/defensio.rb', line 55

def get_document(signature)
  respond self.class.get(api_path("documents", signature))
end

#get_extended_stats(data) ⇒ Array

Get more exhaustive statistics for the current user

Parameters:

  • data (Hash)

    The parameters to be sent to Defensio. Keys can either be Strings or Symbols

Returns:

  • (Array)

    An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/defensio.rb', line 76

def get_extended_stats(data)
  result = self.class.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_userObject

Get information about the api key



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

def get_user
  respond self.class.get(api_path)
end

#handle_post_document_async_callback(request) ⇒ Hash

Takes the request object (Rails, Sinatra, Merb) of an async request callback and returns a hash containing the status of the document being analyzed.

Parameters:

  • request (ActionController::Request, Sinatra::Request, String)

    The request object created after Defensio POSTed to your site, or a string representation of the POST data.

Returns:

  • (Hash)

    Status of the document



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/defensio.rb', line 97

def 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

  parse_body(data)
end

#post_document(data) ⇒ Array

Create and analyze a new document

Parameters:

  • data (Hash)

    The parameters to be sent to Defensio. Keys can either be Strings or Symbols

Returns:

  • (Array)

    An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio



47
48
49
50
# File 'lib/defensio.rb', line 47

def post_document(data)
  data = { :client => @client }.merge(data)
  respond self.class.post(api_path("documents"), :body => data)
end

#post_profanity_filter(data) ⇒ Object

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



89
90
91
# File 'lib/defensio.rb', line 89

def post_profanity_filter(data)
  respond self.class.post(api_path("profanity-filter"), :body => data)
end

#put_document(signature, data) ⇒ Array

Modify the properties of an existing document

Parameters:

  • signature (String)

    The signature of the document to modify

  • data (Hash)

    The parameters to be sent to Defensio. Keys can either be Strings or Symbols

Returns:

  • (Array)

    An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio



63
64
65
# File 'lib/defensio.rb', line 63

def put_document(signature, data)
  respond self.class.put(api_path("documents", signature), :body => data)
end