Class: Defensor
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
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#signature ⇒ Object
Returns the value of attribute signature.
Class Method Summary collapse
- .api_path(action = nil, id = nil) ⇒ Object
-
.get_basic_stats ⇒ Array
Get basic statistics for the current user.
-
.get_extended_stats(data) ⇒ Array
Get more exhaustive statistics for the current user.
-
.get_user ⇒ Object
Get information about the api key.
-
.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.
- .parse_body(str) ⇒ Object
-
.post_profanity_filter(data) ⇒ Object
Filter a set of values based on a pre-defined dictionary.
- .respond(response) ⇒ Object
Instance Method Summary collapse
- #check_document ⇒ Object
- #check_key ⇒ Object
- #content ⇒ Object
-
#get_document(signature = nil) ⇒ Array
Get the status of an existing document.
-
#initialize(*options) ⇒ Defensor
constructor
A new instance of Defensor.
-
#post_document ⇒ Array
Create and analyze a new document.
-
#put_document(signature = nil, data) ⇒ Array
Modify the properties of an existing document.
Constructor Details
#initialize(*options) ⇒ Defensor
Returns a new instance of Defensor.
108 109 110 111 112 |
# File 'lib/defensor.rb', line 108 def initialize(*) check_key @options = [0].reject {|k, v| !(ALLOWED_OPTIONS.include? k)} @signature = @options['signature'] if @options['signature'] end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
36 37 38 |
# File 'lib/defensor.rb', line 36 def @options end |
#signature ⇒ Object
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_stats ⇒ Array
Get basic statistics for the current user
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
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_user ⇒ Object
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.
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_document ⇒ Object
118 119 120 |
# File 'lib/defensor.rb', line 118 def check_document raise NoContentException if content.nil? || content.empty? end |
#check_key ⇒ Object
114 115 116 |
# File 'lib/defensor.rb', line 114 def check_key raise NoApiKeyException if @@api_key.nil? || @@api_key.empty? end |
#content ⇒ Object
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
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_document ⇒ Array
Create and analyze a new document
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
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 |