defensio 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/defensio.gemspec +51 -0
- data/lib/defensio.rb +27 -66
- data/test/defensio_test.rb +78 -204
- metadata +6 -4
data/Rakefile
CHANGED
@@ -9,8 +9,8 @@ begin
|
|
9
9
|
gemspec.description = "Official Ruby library for Defensio 2.0"
|
10
10
|
gemspec.email = "support@defensio.com"
|
11
11
|
gemspec.homepage = "http://github.com/defensio/defensio-ruby"
|
12
|
-
gemspec.authors = ["Carl Mercier"]
|
13
|
-
gemspec.add_dependency('
|
12
|
+
gemspec.authors = ["The Defensio Team", "Carl Mercier"]
|
13
|
+
gemspec.add_dependency('httparty', '>= 0.5.0')
|
14
14
|
end
|
15
15
|
Jeweler::GemcutterTasks.new
|
16
16
|
rescue LoadError
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.9.
|
1
|
+
0.9.1
|
data/defensio.gemspec
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{defensio}
|
8
|
+
s.version = "0.9.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["The Defensio Team", "Carl Mercier"]
|
12
|
+
s.date = %q{2010-01-21}
|
13
|
+
s.description = %q{Official Ruby library for Defensio 2.0}
|
14
|
+
s.email = %q{support@defensio.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".gitignore",
|
21
|
+
"LICENSE",
|
22
|
+
"README",
|
23
|
+
"Rakefile",
|
24
|
+
"VERSION",
|
25
|
+
"defensio.gemspec",
|
26
|
+
"lib/defensio.rb",
|
27
|
+
"test/defensio_test.rb"
|
28
|
+
]
|
29
|
+
s.homepage = %q{http://github.com/defensio/defensio-ruby}
|
30
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
s.rubygems_version = %q{1.3.5}
|
33
|
+
s.summary = %q{Official Ruby library for Defensio 2.0}
|
34
|
+
s.test_files = [
|
35
|
+
"test/defensio_test.rb"
|
36
|
+
]
|
37
|
+
|
38
|
+
if s.respond_to? :specification_version then
|
39
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
40
|
+
s.specification_version = 3
|
41
|
+
|
42
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
43
|
+
s.add_runtime_dependency(%q<httparty>, [">= 0.5.0"])
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<httparty>, [">= 0.5.0"])
|
46
|
+
end
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<httparty>, [">= 0.5.0"])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
data/lib/defensio.rb
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
#
|
10
10
|
|
11
11
|
require 'rubygems'
|
12
|
-
require '
|
12
|
+
require 'httparty'
|
13
13
|
require 'uri'
|
14
14
|
|
15
15
|
class Defensio
|
@@ -18,14 +18,18 @@ class Defensio
|
|
18
18
|
API_HOST = "http://api.defensio.com"
|
19
19
|
|
20
20
|
# You should't modify anything below this line.
|
21
|
-
LIB_VERSION = "0.9"
|
21
|
+
LIB_VERSION = "0.9.1"
|
22
22
|
ROOT_NODE = "defensio-result"
|
23
23
|
FORMAT = :yaml
|
24
24
|
USER_AGENT = "Defensio-Ruby #{LIB_VERSION}"
|
25
25
|
CLIENT = "Defensio-Ruby | #{LIB_VERSION} | Carl Mercier | cmercier@websense.com"
|
26
26
|
KEEP_ALIVE = false
|
27
27
|
|
28
|
-
|
28
|
+
include HTTParty
|
29
|
+
format FORMAT
|
30
|
+
base_uri API_HOST
|
31
|
+
|
32
|
+
attr_reader :api_key, :client
|
29
33
|
|
30
34
|
def initialize(api_key, client = CLIENT)
|
31
35
|
@client = client
|
@@ -34,7 +38,7 @@ class Defensio
|
|
34
38
|
|
35
39
|
# Get information about the api key
|
36
40
|
def get_user
|
37
|
-
|
41
|
+
respond self.class.get(api_path)
|
38
42
|
end
|
39
43
|
|
40
44
|
# Create and analyze a new document
|
@@ -42,14 +46,14 @@ class Defensio
|
|
42
46
|
# @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio
|
43
47
|
def post_document(data)
|
44
48
|
data = { :client => @client }.merge(data)
|
45
|
-
|
49
|
+
respond self.class.post(api_path("documents"), :body => data)
|
46
50
|
end
|
47
51
|
|
48
52
|
# Get the status of an existing document
|
49
53
|
# @param [String] signature The signature of the document to retrieve
|
50
54
|
# @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio
|
51
55
|
def get_document(signature)
|
52
|
-
|
56
|
+
respond self.class.get(api_path("documents", signature))
|
53
57
|
end
|
54
58
|
|
55
59
|
# Modify the properties of an existing document
|
@@ -57,30 +61,33 @@ class Defensio
|
|
57
61
|
# @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols
|
58
62
|
# @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio
|
59
63
|
def put_document(signature, data)
|
60
|
-
|
64
|
+
respond self.class.put(api_path("documents", signature), :body => data)
|
61
65
|
end
|
62
66
|
|
63
67
|
# Get basic statistics for the current user
|
64
68
|
# @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio
|
65
69
|
def get_basic_stats
|
66
|
-
|
70
|
+
respond self.class.get(api_path("basic-stats"))
|
67
71
|
end
|
68
72
|
|
69
73
|
# Get more exhaustive statistics for the current user
|
70
74
|
# @param [Hash] data The parameters to be sent to Defensio. Keys can either be Strings or Symbols
|
71
75
|
# @return [Array] An array containing 2 values: the HTTP status code & a Hash with the values returned by Defensio
|
72
76
|
def get_extended_stats(data)
|
73
|
-
result =
|
74
|
-
|
75
|
-
|
77
|
+
result = self.class.get(api_path("extended-stats"), :query => data)
|
78
|
+
code = result.code
|
79
|
+
result = result[ROOT_NODE]
|
80
|
+
|
81
|
+
0.upto(result["data"].size - 1) do |i|
|
82
|
+
result["data"][i]["date"] = Date.parse(result["data"][i]["date"])
|
76
83
|
end
|
77
84
|
|
78
|
-
result
|
85
|
+
[code, result]
|
79
86
|
end
|
80
87
|
|
81
88
|
# Filter a set of values based on a pre-defined dictionary
|
82
89
|
def post_profanity_filter(data)
|
83
|
-
|
90
|
+
respond self.class.post(api_path("profanity-filter"), :body => data)
|
84
91
|
end
|
85
92
|
|
86
93
|
# Takes the request object (Rails, Sinatra, Merb) of an async request callback and returns a hash
|
@@ -90,7 +97,7 @@ class Defensio
|
|
90
97
|
def handle_post_document_async_callback(request)
|
91
98
|
if request.is_a?(String)
|
92
99
|
data = request
|
93
|
-
elsif request.respond_to?(:body) && request.body.
|
100
|
+
elsif request.respond_to?(:body) && request.body.respond_to?(:read)
|
94
101
|
data = request.body.read
|
95
102
|
else
|
96
103
|
raise ArgumentError, "Unknown request type: #{request.class}"
|
@@ -105,47 +112,16 @@ class Defensio
|
|
105
112
|
end
|
106
113
|
|
107
114
|
protected
|
108
|
-
def
|
109
|
-
|
115
|
+
def respond(response)
|
116
|
+
[response.code, response[ROOT_NODE]]
|
117
|
+
end
|
118
|
+
|
119
|
+
def api_path(action = nil, id = nil)
|
120
|
+
path = "/#{API_VERSION}/users/#{@api_key}"
|
110
121
|
path += "/#{action}" if action
|
111
122
|
path += "/#{id}" if id
|
112
123
|
path += ".#{FORMAT}"
|
113
124
|
end
|
114
|
-
|
115
|
-
def http_session
|
116
|
-
return @http_session if KEEP_ALIVE && @http_session
|
117
|
-
@http_session = Patron::Session.new
|
118
|
-
@http_session.timeout = 20
|
119
|
-
@http_session.headers['User-Agent'] = USER_AGENT
|
120
|
-
@http_session.headers['Content-Type'] = "text/#{FORMAT}"
|
121
|
-
@http_session
|
122
|
-
end
|
123
|
-
|
124
|
-
def http_session=(session)
|
125
|
-
@http_session = session
|
126
|
-
end
|
127
|
-
|
128
|
-
def call(method, url, data = nil)
|
129
|
-
data = hash_to_query_string(data) if data.is_a?(Hash)
|
130
|
-
url = url + "?#{data}" unless data.nil? || data.empty?
|
131
|
-
|
132
|
-
response = case method
|
133
|
-
when :get
|
134
|
-
http_session.get(url)
|
135
|
-
when :delete
|
136
|
-
http_session.delete(url)
|
137
|
-
when :post
|
138
|
-
http_session.post(url, {})
|
139
|
-
when :put
|
140
|
-
http_session.put(url, {})
|
141
|
-
else
|
142
|
-
raise(ArgumentError, "Invalid HTTP method: #{method}")
|
143
|
-
end
|
144
|
-
|
145
|
-
http_session = nil unless KEEP_ALIVE
|
146
|
-
|
147
|
-
[response.status, parse_body(response.body)]
|
148
|
-
end
|
149
125
|
|
150
126
|
def parse_body(str)
|
151
127
|
if FORMAT == :yaml
|
@@ -155,19 +131,4 @@ class Defensio
|
|
155
131
|
end
|
156
132
|
end
|
157
133
|
|
158
|
-
def hash_to_query_string(data)
|
159
|
-
return nil unless data.is_a?(Hash)
|
160
|
-
out = ""
|
161
|
-
sort_hash_by_key(data).each do |item|
|
162
|
-
k, v = item[0], item[1]
|
163
|
-
out += "&" unless out.empty?
|
164
|
-
k = k.to_s.gsub(/_/, "-") if k.is_a?(Symbol)
|
165
|
-
out += "#{k}=#{URI.escape(v.to_s)}"
|
166
|
-
end
|
167
|
-
out
|
168
|
-
end
|
169
|
-
|
170
|
-
def sort_hash_by_key(hash)
|
171
|
-
hash.keys.sort_by {|s| s.to_s}.map {|key| [key, hash[key]] }
|
172
|
-
end
|
173
134
|
end
|
data/test/defensio_test.rb
CHANGED
@@ -1,78 +1,70 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/../lib/defensio"
|
2
1
|
DEFENSIO_ENV = "test"
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + "/../lib/defensio"
|
3
4
|
require 'test/unit'
|
4
5
|
require 'mocha'
|
5
6
|
require 'redgreen'
|
6
7
|
require 'ostruct'
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
API_KEY = "1234567890"
|
11
|
-
OWNER_URL = "http://example.org"
|
12
|
-
SIGNATURE = "abcdefghijklmnop"
|
9
|
+
# You must run this script as following:
|
10
|
+
# $ DEFENSIO_KEY=<your api key here> ruby test/defensio_test.rb
|
13
11
|
|
14
|
-
|
12
|
+
class DefensioTest < Test::Unit::TestCase
|
15
13
|
API_HOST = "http://api.defensio.com"
|
14
|
+
API_VERSION = 2.0
|
15
|
+
OWNER_URL = "http://example.org"
|
16
16
|
FORMAT = :yaml
|
17
17
|
HEADERS = {"User-Agent" => "Defensio-Ruby #{Defensio::LIB_VERSION}", "Content-Type" => "text/yaml"}
|
18
18
|
|
19
19
|
# API METHOD TESTS -- Useful to learn how to use the library
|
20
20
|
def test_get_user
|
21
|
-
if MOCK_RESPONSE
|
22
|
-
Patron::Session.any_instance.expects(:get).with("#{API_HOST}/#{API_VERSION}/users/#{API_KEY}.#{FORMAT}").once.returns(FakePatronResponse.new(200, user_body))
|
23
|
-
end
|
24
|
-
|
25
21
|
status, body = @d.get_user
|
26
22
|
assert body.is_a?(Hash)
|
27
23
|
assert_equal 200, status
|
28
24
|
assert_equal "success", body["status"]
|
29
25
|
end
|
30
26
|
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
Patron::Session.any_instance.expects(:post).with("#{API_HOST}/#{API_VERSION}/users/#{API_KEY}/documents.#{FORMAT}?#{query}", {}).once.returns(FakePatronResponse.new(200, document_body(SIGNATURE)))
|
35
|
-
end
|
36
|
-
|
37
|
-
data = { :content => "We sell cheap Viagra! [spam,0.95]", :platform => "my_awesome_app", :type => "test" }
|
27
|
+
def test_post_get_put_document
|
28
|
+
# POST
|
29
|
+
data = { :content => "This is a simple test", :platform => "my_awesome_app", :type => "comment" }
|
38
30
|
status, body = @d.post_document(data)
|
39
31
|
assert body.is_a?(Hash)
|
40
32
|
assert_equal 200, status
|
41
33
|
assert_equal "success", body["status"]
|
42
34
|
assert body["signature"].is_a?(String)
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
35
|
+
|
36
|
+
# Keep some variables around
|
37
|
+
original_allow_result = body["allow"]
|
38
|
+
signature = body["signature"]
|
39
|
+
|
40
|
+
# Give Defensio some time to process
|
41
|
+
sleep 0.5
|
42
|
+
|
43
|
+
# GET
|
44
|
+
status, body = @d.get_document(signature)
|
51
45
|
assert body.is_a?(Hash)
|
52
46
|
assert_equal 200, status
|
53
47
|
assert_equal "success", body["status"]
|
54
|
-
assert_equal
|
55
|
-
end
|
48
|
+
assert_equal signature, body["signature"]
|
56
49
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
50
|
+
# PUT
|
51
|
+
status, body = @d.put_document(signature, :allow => !original_allow_result)
|
52
|
+
assert body.is_a?(Hash)
|
53
|
+
assert_equal 200, status
|
54
|
+
assert_equal "success", body["status"]
|
55
|
+
assert_equal signature, body["signature"]
|
56
|
+
assert_equal !original_allow_result, body["allow"]
|
62
57
|
|
63
|
-
|
58
|
+
# PUT (back to original value)
|
59
|
+
status, body = @d.put_document(signature, :allow => original_allow_result)
|
64
60
|
assert body.is_a?(Hash)
|
65
61
|
assert_equal 200, status
|
66
62
|
assert_equal "success", body["status"]
|
67
|
-
assert_equal
|
68
|
-
assert_equal
|
63
|
+
assert_equal signature, body["signature"]
|
64
|
+
assert_equal original_allow_result, body["allow"]
|
69
65
|
end
|
70
66
|
|
71
67
|
def test_get_basic_stats
|
72
|
-
if MOCK_RESPONSE
|
73
|
-
Patron::Session.any_instance.expects(:get).with("#{API_HOST}/#{API_VERSION}/users/#{API_KEY}/basic-stats.#{FORMAT}").once.returns(FakePatronResponse.new(200, basic_stats_body))
|
74
|
-
end
|
75
|
-
|
76
68
|
status, body = @d.get_basic_stats
|
77
69
|
assert body.is_a?(Hash)
|
78
70
|
assert_equal 200, status
|
@@ -81,63 +73,60 @@ class DefensioTest < Test::Unit::TestCase
|
|
81
73
|
end
|
82
74
|
|
83
75
|
def test_get_extended_stats
|
84
|
-
if MOCK_RESPONSE
|
85
|
-
query="from=2009-09-01&to=2009-09-03"
|
86
|
-
Patron::Session.any_instance.expects(:get).with("#{API_HOST}/#{API_VERSION}/users/#{API_KEY}/extended-stats.#{FORMAT}?#{query}").once.returns(FakePatronResponse.new(200, extended_stats_body))
|
87
|
-
end
|
88
|
-
|
89
76
|
status, body = @d.get_extended_stats(:from => Date.new(2009, 9, 1), :to => Date.new(2009, 9, 3))
|
90
77
|
assert body.is_a?(Hash)
|
91
78
|
assert_equal 200, status
|
92
79
|
assert_equal "success", body["status"]
|
93
80
|
assert body["data"].is_a?(Array)
|
94
|
-
assert body["data"][0]["date"].is_a?(Date)
|
81
|
+
assert body["data"][0]["date"].is_a?(Date) if body["data"].size > 0
|
95
82
|
end
|
96
83
|
|
97
84
|
def test_post_profanity_filter
|
98
|
-
|
99
|
-
query="OtherField=hello%20again&field1=hello%20world"
|
100
|
-
Patron::Session.any_instance.expects(:post).with("#{API_HOST}/#{API_VERSION}/users/#{API_KEY}/profanity-filter.#{FORMAT}?#{query}", {}).once.returns(FakePatronResponse.new(200, profanity_filter_body))
|
101
|
-
end
|
102
|
-
|
103
|
-
status, body = @d.post_profanity_filter("field1"=>"hello world", "OtherField"=>"hello again")
|
85
|
+
status, body = @d.post_profanity_filter("field1"=>"hello world", "other_field"=>"hello again")
|
104
86
|
assert body.is_a?(Hash)
|
105
87
|
assert_equal 200, status
|
106
88
|
assert_equal "success", body["status"]
|
107
89
|
assert body["filtered"].is_a?(Hash)
|
108
90
|
assert body["filtered"].keys.include?("field1")
|
109
|
-
assert body["filtered"].keys.include?("
|
91
|
+
assert body["filtered"].keys.include?("other_field")
|
110
92
|
end
|
111
93
|
|
112
94
|
def test_handle_post_document_async_callback__string
|
113
|
-
result = { "
|
114
|
-
"
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
95
|
+
result = { "defensio-result" =>
|
96
|
+
{ "api-version" => API_VERSION,
|
97
|
+
"status" => "success",
|
98
|
+
"message" => nil,
|
99
|
+
"signature" => "123456",
|
100
|
+
"allow" => false,
|
101
|
+
"classification" => "malicious",
|
102
|
+
"spaminess" => 0.95,
|
103
|
+
"profanity-match" => true }
|
104
|
+
}
|
105
|
+
|
106
|
+
assert_equal Hash, @d.class.handle_post_document_async_callback(result.to_yaml).class
|
124
107
|
end
|
125
108
|
|
126
109
|
def test_handle_post_document_async_callback__request_object
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
110
|
+
post_data = { "defensio-result" =>
|
111
|
+
{ "api-version" => API_VERSION,
|
112
|
+
"status" => "success",
|
113
|
+
"message" => nil,
|
114
|
+
"signature" => "123456",
|
115
|
+
"allow" => false,
|
116
|
+
"classification" => "malicious",
|
117
|
+
"spaminess" => 0.95,
|
118
|
+
"profanity-match" => true }
|
119
|
+
}
|
135
120
|
|
136
|
-
fake_request_object = OpenStruct.new(:body => StringIO.new(
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
121
|
+
fake_request_object = OpenStruct.new(:body => StringIO.new(post_data.to_yaml))
|
122
|
+
result = @d.class.handle_post_document_async_callback(fake_request_object)
|
123
|
+
assert_equal Hash, result.class
|
124
|
+
assert_equal "success", result["status"]
|
125
|
+
|
126
|
+
fake_request_object = OpenStruct.new(:body => StringIO.new(post_data.to_yaml))
|
127
|
+
result = @d.handle_post_document_async_callback(fake_request_object)
|
128
|
+
assert_equal Hash, result.class
|
129
|
+
assert_equal "success", result["status"]
|
141
130
|
end
|
142
131
|
|
143
132
|
def test_handle_post_document_async_callback__invalid_object_type
|
@@ -147,31 +136,10 @@ class DefensioTest < Test::Unit::TestCase
|
|
147
136
|
|
148
137
|
|
149
138
|
# OTHER TESTS
|
150
|
-
def
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
assert_equal s, @d.send(:http_session) # make sure sessions are reused when KEEP_ALIVE is true
|
155
|
-
else
|
156
|
-
assert_not_equal s, @d.send(:http_session) # make sure sessions are dropped when KEEP_ALIVE is false
|
157
|
-
end
|
158
|
-
|
159
|
-
assert_equal HEADERS, s.headers
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_api_url
|
163
|
-
assert_equal "#{API_HOST}/#{API_VERSION}/users/#{API_KEY}.yaml", @d.send(:api_url)
|
164
|
-
assert_equal "#{API_HOST}/#{API_VERSION}/users/#{API_KEY}/documents.yaml", @d.send(:api_url, "documents")
|
165
|
-
assert_equal "#{API_HOST}/#{API_VERSION}/users/#{API_KEY}/documents/abcdefghijklmnop.yaml", @d.send(:api_url, "documents", "abcdefghijklmnop")
|
166
|
-
end
|
167
|
-
|
168
|
-
def test_hash_to_query_string
|
169
|
-
assert_equal "hello-world=true", @d.send(:hash_to_query_string, {:hello_world => true} )
|
170
|
-
assert_equal "hello-world=this%20has%20spaces%20and%20characters%20($%20?%20&%20%5E%20%C3%A9)%20that%20should%20be%20escaped",
|
171
|
-
@d.send(:hash_to_query_string, {:hello_world => "this has spaces and characters ($ ? & ^ é) that should be escaped"} )
|
172
|
-
assert_equal "value1=true&value2=true&value3=true", @d.send(:hash_to_query_string, { "value1" => true, "value2" => true, :value3 => true})
|
173
|
-
assert_equal "date=2009-01-01", @d.send(:hash_to_query_string, { "date" => Date.new(2009,1,1) })
|
174
|
-
assert_nil @d.send(:hash_to_query_string, nil)
|
139
|
+
def test_api_path
|
140
|
+
assert_equal "/#{API_VERSION}/users/#{@api_key}.yaml", @d.send(:api_path)
|
141
|
+
assert_equal "/#{API_VERSION}/users/#{@api_key}/documents.yaml", @d.send(:api_path, "documents")
|
142
|
+
assert_equal "/#{API_VERSION}/users/#{@api_key}/documents/abcdefghijklmnop.yaml", @d.send(:api_path, "documents", "abcdefghijklmnop")
|
175
143
|
end
|
176
144
|
|
177
145
|
def test_parse_body
|
@@ -181,108 +149,14 @@ class DefensioTest < Test::Unit::TestCase
|
|
181
149
|
|
182
150
|
# HELPERS AND SETUP
|
183
151
|
def setup
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
def base_url
|
192
|
-
"#{API_HOST}/#{API_VERSION}"
|
193
|
-
end
|
194
|
-
|
195
|
-
# MOCKING
|
196
|
-
class FakePatronResponse < Struct.new(:status, :body); end
|
197
|
-
|
198
|
-
def user_body
|
199
|
-
{ "defensio-result" => {"api-version" => API_VERSION, "status" => "success", "message" => nil, "owner-url" => OWNER_URL} }.send("to_#{FORMAT}")
|
200
|
-
end
|
201
|
-
|
202
|
-
def document_body(signature)
|
203
|
-
{ "defensio-result" => {
|
204
|
-
"api-version" => API_VERSION,
|
205
|
-
"status" => "success",
|
206
|
-
"message" => nil,
|
207
|
-
"signature" => signature,
|
208
|
-
"allow" => false,
|
209
|
-
"classification" => "malicious",
|
210
|
-
"spaminess" => 0.95,
|
211
|
-
"profanity-match" => true }
|
212
|
-
}.send("to_#{FORMAT}")
|
213
|
-
end
|
152
|
+
if ENV['DEFENSIO_KEY'].nil?
|
153
|
+
puts "You must set the DEFENSIO_KEY environment variable before running tests. Example:"
|
154
|
+
puts "$ DEFENSIO_KEY=<your api key here> ruby test/defensio_test.rb"
|
155
|
+
puts "Fail. Epic Fail."
|
156
|
+
exit 1
|
157
|
+
end
|
214
158
|
|
215
|
-
|
216
|
-
|
217
|
-
"api-version" => API_VERSION,
|
218
|
-
"status" => "success",
|
219
|
-
"message" => nil,
|
220
|
-
"signature" => signature,
|
221
|
-
"allow" => true,
|
222
|
-
"classification" => "innocent",
|
223
|
-
"spaminess" => 0.95,
|
224
|
-
"profanity-match" => true }
|
225
|
-
}.send("to_#{FORMAT}")
|
226
|
-
end
|
227
|
-
|
228
|
-
def basic_stats_body
|
229
|
-
{ "defensio-result" => {
|
230
|
-
"api-version" => API_VERSION,
|
231
|
-
"status" => "success",
|
232
|
-
"message" => nil,
|
233
|
-
"recent-accuracy" => 0.9975,
|
234
|
-
"legitimate" => { "total" => 100 },
|
235
|
-
"unwanted" => { "total" => 100, "malicious" => 50, "spam" => 100},
|
236
|
-
"false-positives" => 1,
|
237
|
-
"false-negatices" => 2,
|
238
|
-
"learning" => true,
|
239
|
-
"learning-status" => "Details about learning mode" }
|
240
|
-
}.send("to_#{FORMAT}")
|
241
|
-
end
|
242
|
-
|
243
|
-
def extended_stats_body
|
244
|
-
{ "defensio-result" => {
|
245
|
-
"api-version" => API_VERSION,
|
246
|
-
"status" => "success",
|
247
|
-
"message" => nil,
|
248
|
-
"data" => [
|
249
|
-
{ "date" => "2009-09-01",
|
250
|
-
"recent-accuracy" => 0.9975,
|
251
|
-
"legitimate" => 100,
|
252
|
-
"unwanted" => 500,
|
253
|
-
"false-positives" => 1,
|
254
|
-
"false-negatives" => 0 },
|
255
|
-
{
|
256
|
-
"date" => "2009-09-02",
|
257
|
-
"recent-accuracy" => 0.9985,
|
258
|
-
"legitimate" => 50,
|
259
|
-
"unwanted" => 475,
|
260
|
-
"false-positives" => 0,
|
261
|
-
"false-negatives" => 0 },
|
262
|
-
{ "date" => "2009-09-03",
|
263
|
-
"recent-accuracy" => 0.9992,
|
264
|
-
"legitimate" => 100,
|
265
|
-
"unwanted" => 500,
|
266
|
-
"false-positives" => 1,
|
267
|
-
"false-negatives" => 0 }
|
268
|
-
],
|
269
|
-
"chart-urls" => {
|
270
|
-
"recent-accuracy" => "http://domain.com/chart/123456",
|
271
|
-
"total-unwanted" => "http://domain.com/chart/abcdef",
|
272
|
-
"total-legitimate" => "http://domain.com/chart/xyzabc" }
|
273
|
-
}
|
274
|
-
}.send("to_#{FORMAT}")
|
275
|
-
end
|
276
|
-
|
277
|
-
def profanity_filter_body
|
278
|
-
{ "defensio-result" => {
|
279
|
-
"api-version" => API_VERSION,
|
280
|
-
"status" => "success",
|
281
|
-
"message" => nil,
|
282
|
-
"filtered" =>
|
283
|
-
{ "field1" => "hello world",
|
284
|
-
"OtherField" => "hello again" }
|
285
|
-
}
|
286
|
-
}.send("to_#{FORMAT}")
|
159
|
+
@api_key = ENV['DEFENSIO_KEY']
|
160
|
+
@d = Defensio.new(@api_key)
|
287
161
|
end
|
288
162
|
end
|
metadata
CHANGED
@@ -1,26 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: defensio
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
+
- The Defensio Team
|
7
8
|
- Carl Mercier
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
|
12
|
-
date:
|
13
|
+
date: 2010-01-21 00:00:00 -05:00
|
13
14
|
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
17
|
+
name: httparty
|
17
18
|
type: :runtime
|
18
19
|
version_requirement:
|
19
20
|
version_requirements: !ruby/object:Gem::Requirement
|
20
21
|
requirements:
|
21
22
|
- - ">="
|
22
23
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.
|
24
|
+
version: 0.5.0
|
24
25
|
version:
|
25
26
|
description: Official Ruby library for Defensio 2.0
|
26
27
|
email: support@defensio.com
|
@@ -37,6 +38,7 @@ files:
|
|
37
38
|
- README
|
38
39
|
- Rakefile
|
39
40
|
- VERSION
|
41
|
+
- defensio.gemspec
|
40
42
|
- lib/defensio.rb
|
41
43
|
- test/defensio_test.rb
|
42
44
|
has_rdoc: true
|