Class: Defensor

Inherits:
Object
  • Object
show all
Includes:
Version, HTTParty
Defined in:
lib/defensor.rb

Defined Under Namespace

Modules: Version Classes: NoApiKeyException, NoContentException, NoSignatureException

Constant Summary collapse

ALLOWED_OPTIONS =
[:type, :platform, "author-email", "author-ip", :content, :signature]
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.

Defensor::VERSION
ROOT_NODE =
"defensio-result"
FORMAT =
:json
USER_AGENT =
"Defensor #{LIB_VERSION}"
CLIENT =
"Defensor | #{LIB_VERSION} | Oleg Bovykin | [email protected]"
KEEP_ALIVE =
false

Constants included from Version

Version::VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Defensor

Returns a new instance of Defensor.



108
109
110
111
112
# 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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



36
37
38
# File 'lib/defensor.rb', line 36

def options
  @options
end

#signatureObject

Returns the value of attribute signature.



36
37
38
# File 'lib/defensor.rb', line 36

def signature
  @signature
end

Class Method Details

.api_path(action = nil, id = nil) ⇒ Object



51
52
53
54
55
56
# 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_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



60
61
62
# File 'lib/defensor.rb', line 60

def self.get_basic_stats
  respond get(api_path("basic-stats"))
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



67
68
69
70
71
72
73
74
75
76
77
# 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_userObject

Get information about the api key



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

def self.get_user
  respond 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



96
97
98
99
100
101
102
103
104
105
106
# 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

.parse_body(str) ⇒ Object



84
85
86
87
88
89
90
# 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) ⇒ Object

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



80
81
82
# File 'lib/defensor.rb', line 80

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

.respond(response) ⇒ Object



42
43
44
# File 'lib/defensor.rb', line 42

def self.respond(response)
  [response.code, response[ROOT_NODE]]
end

Instance Method Details

#check_documentObject

Raises:



118
119
120
# File 'lib/defensor.rb', line 118

def check_document
  raise NoContentException if content.nil? || content.empty?
end

#check_keyObject

Raises:



114
115
116
# File 'lib/defensor.rb', line 114

def check_key
  raise NoApiKeyException if @@api_key.nil? || @@api_key.empty?
end

#contentObject



122
123
124
# File 'lib/defensor.rb', line 122

def content
  @options[:content]
end

#get_document(signature = nil) ⇒ Array

Get the status of an existing document

Parameters:

  • signature (String) (defaults to: nil)

    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



140
141
142
143
144
145
146
147
# 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_documentArray

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



129
130
131
132
133
134
135
# 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) ⇒ Array

Modify the properties of an existing document

Parameters:

  • signature (String) (defaults to: nil)

    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



153
154
155
156
157
158
159
160
# 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