filter8 0.9
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.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/.rspec +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +42 -0
- data/README.md +47 -0
- data/Rakefile +7 -0
- data/filter8.gemspec +27 -0
- data/lib/filter8.rb +46 -0
- data/lib/filter8/client.rb +45 -0
- data/lib/filter8/exception.rb +5 -0
- data/lib/filter8/request.rb +74 -0
- data/lib/filter8/result.rb +22 -0
- data/spec/lib/filter8/client_spec.rb +75 -0
- data/spec/lib/filter8/request_spec.rb +73 -0
- data/spec/lib/filter8/result_spec.rb +110 -0
- data/spec/lib/filter8_spec.rb +55 -0
- data/spec/spec_helper.rb +14 -0
- metadata +138 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 720eb0a7f14ad24804b6fa997fd683814a5c0560
|
4
|
+
data.tar.gz: ab82cedaa9c4fdf715eb401ff312e6091f65861f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: febb61d5262767ce53f9c650d66856f7f95a5e01261a7a680a563f7425e9708b45c6723335193fb358a67c3464ef1c542591d38227b0938fc13bfe07bc7e57fd
|
7
|
+
data.tar.gz: ac2ac322dd474d1d943ea1f8a0f95a74615a18688d1302f15b01f0a976caeb6a0244013242f2a8836950350a6b5e1e105b098d1357245e82f57d9a64102800ef
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
filter8 (0.1)
|
5
|
+
faraday
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
codeclimate-test-reporter (0.3.0)
|
11
|
+
simplecov (>= 0.7.1, < 1.0.0)
|
12
|
+
diff-lcs (1.2.5)
|
13
|
+
docile (1.1.3)
|
14
|
+
faraday (0.9.0)
|
15
|
+
multipart-post (>= 1.2, < 3)
|
16
|
+
multi_json (1.10.1)
|
17
|
+
multipart-post (2.0.0)
|
18
|
+
rake (10.1.1)
|
19
|
+
rspec (2.14.1)
|
20
|
+
rspec-core (~> 2.14.0)
|
21
|
+
rspec-expectations (~> 2.14.0)
|
22
|
+
rspec-mocks (~> 2.14.0)
|
23
|
+
rspec-core (2.14.8)
|
24
|
+
rspec-expectations (2.14.5)
|
25
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
26
|
+
rspec-mocks (2.14.6)
|
27
|
+
simplecov (0.8.2)
|
28
|
+
docile (~> 1.1.0)
|
29
|
+
multi_json
|
30
|
+
simplecov-html (~> 0.8.0)
|
31
|
+
simplecov-html (0.8.0)
|
32
|
+
timecop (0.6.3)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
codeclimate-test-reporter
|
39
|
+
filter8!
|
40
|
+
rake (~> 10.1.0)
|
41
|
+
rspec (~> 2.14.1)
|
42
|
+
timecop
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# filter8
|
2
|
+
A unofficial ruby wrapper for Filter8 (http://filter8.com). An API to filter lots of stuff from texts, e.g. bad words.
|
3
|
+
|
4
|
+
[](https://codeclimate.com/github/toadle/filter8)
|
5
|
+
[](https://codeclimate.com/github/toadle/filter8)
|
6
|
+
|
7
|
+
# Installation
|
8
|
+
|
9
|
+
|
10
|
+
As you'd expect. Nothing special here:
|
11
|
+
|
12
|
+
```
|
13
|
+
gem install filter8
|
14
|
+
```
|
15
|
+
|
16
|
+
# How to use
|
17
|
+
|
18
|
+
## Configuration
|
19
|
+
|
20
|
+
Before you use Filter8 you need to configure it:
|
21
|
+
```
|
22
|
+
Filter8.configure do |config|
|
23
|
+
config.api_key = "your filter8-api-key"
|
24
|
+
config.api_secret = "your filter8-api-secret"
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Simply send the content, you wish to filter with the filter-method:
|
31
|
+
```
|
32
|
+
Filter8.filter("your content")
|
33
|
+
```
|
34
|
+
|
35
|
+
If you want to change the **default-options** of filter8 (https://filter8.com/documentation), you can pass an optional options-hash with the filter-configuration:
|
36
|
+
```
|
37
|
+
Filter8.filter("your content", blacklist: {enabled: true, locales: [:en, :de, :fr], tags: ["AsciiArt"]})
|
38
|
+
```
|
39
|
+
|
40
|
+
The **email** and **urls** filters are not yet supported.
|
41
|
+
|
42
|
+
## Feedback
|
43
|
+
Feedback and pull-request are always welcome.
|
44
|
+
You can find the author via http://toadle.me
|
45
|
+
|
46
|
+
## License
|
47
|
+
The filter8-gem is released under the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/filter8.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "filter8"
|
3
|
+
s.version = "0.9"
|
4
|
+
s.default_executable = "hola"
|
5
|
+
|
6
|
+
s.authors = ["Tim Adler"]
|
7
|
+
s.date = %q{2014-08-19}
|
8
|
+
s.description = %q{An unofficial ruby wrapper for Filter8 (http://filter8.com). An API to filter lots of stuff from texts, e.g. bad words.}
|
9
|
+
s.summary = %q{An unofficial ruby wrapper for Filter8 (http://filter8.com). An API to filter lots of stuff from texts, e.g. bad words.}
|
10
|
+
s.email = %q{mail (at) toadle (dot) me}
|
11
|
+
s.license = "MIT"
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.homepage = %q{https://github.com/toadle/filter8}
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubygems_version = %q{1.6.2}
|
18
|
+
|
19
|
+
s.add_dependency 'faraday'
|
20
|
+
|
21
|
+
s.add_development_dependency 'rake', '~> 10.1.0'
|
22
|
+
s.add_development_dependency 'rspec', '~> 2.14.1'
|
23
|
+
s.add_development_dependency 'timecop'
|
24
|
+
s.add_development_dependency 'codeclimate-test-reporter'
|
25
|
+
|
26
|
+
end
|
27
|
+
|
data/lib/filter8.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "filter8/exception"
|
2
|
+
require "filter8/client"
|
3
|
+
require "filter8/result"
|
4
|
+
require "filter8/request"
|
5
|
+
|
6
|
+
module Filter8
|
7
|
+
BLACKLIST_FILTER = :blacklist
|
8
|
+
CHARACTERS_FILTER = :characters
|
9
|
+
EMAILS = :emails
|
10
|
+
PHONE_NUMBERS_FILTER = :phoneNumbers
|
11
|
+
URLS_FILTER = :urls
|
12
|
+
WORDS_FILTER = :words
|
13
|
+
|
14
|
+
AVAILABLE_FILTERS = [BLACKLIST_FILTER, CHARACTERS_FILTER, EMAILS, PHONE_NUMBERS_FILTER, URLS_FILTER, WORDS_FILTER]
|
15
|
+
|
16
|
+
FILTER_PARAMS = {
|
17
|
+
BLACKLIST_FILTER => [:enabled, :locale, :tags, :severity],
|
18
|
+
CHARACTERS_FILTER => [:character],
|
19
|
+
PHONE_NUMBERS_FILTER => [:maximumMatchLength, :minimumMatchLength, :separatorPenalty, :spacePenalty, :wordPenalty],
|
20
|
+
WORDS_FILTER => [:word]
|
21
|
+
}
|
22
|
+
|
23
|
+
class Configuration
|
24
|
+
attr_accessor :api_key
|
25
|
+
attr_accessor :api_secret
|
26
|
+
end
|
27
|
+
|
28
|
+
class << self
|
29
|
+
attr_accessor :configuration
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.configure
|
33
|
+
self.configuration ||= Configuration.new
|
34
|
+
yield(configuration)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.filter(content, options = {})
|
38
|
+
raise Exception.new "Configure Filter8-module before using" if(Filter8.configuration.nil?)
|
39
|
+
%i(api_key api_secret).each do |attribute|
|
40
|
+
raise Exception.new "Configure 'attribute' first" if(Filter8.configuration.send(attribute).nil?)
|
41
|
+
end
|
42
|
+
|
43
|
+
client = Filter8::Client.new(api_key: Filter8.configuration.api_key, api_secret: Filter8.configuration.api_secret)
|
44
|
+
client.send_request(Filter8::Request.new(content, options))
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'digest'
|
2
|
+
require 'uri'
|
3
|
+
require 'faraday'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
module Filter8
|
7
|
+
class Client
|
8
|
+
attr_accessor :api_key, :api_secret
|
9
|
+
|
10
|
+
API_URL = 'https://api.filter8.com'
|
11
|
+
API_ENDPOINT = '/content/item.js'
|
12
|
+
|
13
|
+
def initialize(api_key: nil, api_secret: nil, options: {})
|
14
|
+
@api_key = api_key
|
15
|
+
@api_secret = api_secret
|
16
|
+
@options = options
|
17
|
+
end
|
18
|
+
|
19
|
+
def send_request(filter8_request)
|
20
|
+
conn = Faraday.new(:url => API_URL) do |faraday|
|
21
|
+
faraday.request :url_encoded
|
22
|
+
faraday.adapter Faraday.default_adapter
|
23
|
+
end
|
24
|
+
|
25
|
+
conn.basic_auth self.api_key, password
|
26
|
+
response = conn.post "#{API_ENDPOINT}?#{nonce_param}", filter8_request.request_params
|
27
|
+
|
28
|
+
raise Exception.new("Filter8-API error (Status: #{response.status}): #{response.body}") if(response.status != 200)
|
29
|
+
|
30
|
+
JSON.parse response.body
|
31
|
+
end
|
32
|
+
|
33
|
+
def nonce_param
|
34
|
+
"nonce=#{timestamp}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def password
|
38
|
+
Digest::MD5.hexdigest("#{timestamp}#{self.api_secret}")
|
39
|
+
end
|
40
|
+
|
41
|
+
def timestamp
|
42
|
+
@timestamp ||= Time.now.to_i.to_s
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Filter8
|
2
|
+
class Request
|
3
|
+
attr_accessor :content
|
4
|
+
|
5
|
+
def initialize(content, options = {})
|
6
|
+
if content.is_a? Hash
|
7
|
+
@content = content[:content]
|
8
|
+
raise Exception.new("No value for 'content' given") if @content.nil?
|
9
|
+
options = content.reject!{ |k| k == :content }
|
10
|
+
else
|
11
|
+
@content = content
|
12
|
+
end
|
13
|
+
|
14
|
+
options.each do |filter_name, filter_options|
|
15
|
+
validate_filter_options(filter_name, filter_options)
|
16
|
+
|
17
|
+
instance_variable_value = nil
|
18
|
+
if filter_options.is_a? Hash
|
19
|
+
instance_variable_value = { enabled: true }
|
20
|
+
instance_variable_value = instance_variable_value.merge(filter_options)
|
21
|
+
else
|
22
|
+
instance_variable_value = filter_options
|
23
|
+
end
|
24
|
+
instance_variable_set("@#{filter_name}", instance_variable_value)
|
25
|
+
self.class.send(:attr_accessor, filter_name)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def request_params
|
30
|
+
request_params = "content=#{self.content}"
|
31
|
+
|
32
|
+
Filter8::AVAILABLE_FILTERS.each do |filter_name|
|
33
|
+
if self.respond_to?(filter_name) && !self.send(filter_name).nil?
|
34
|
+
request_params = request_params + "&" + filter_options_to_params(filter_name)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
request_params
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def filter_options_to_params(filter_name)
|
44
|
+
params = []
|
45
|
+
|
46
|
+
filter_options = self.send(filter_name)
|
47
|
+
if filter_options.is_a? Hash
|
48
|
+
filter_options.each do |filter_option, filter_option_value|
|
49
|
+
if filter_option_value.respond_to? :each
|
50
|
+
filter_option_value.each do |value|
|
51
|
+
params << "#{filter_name}.#{filter_option}=#{value}"
|
52
|
+
end
|
53
|
+
else
|
54
|
+
params << "#{filter_name}.#{filter_option}=#{filter_option_value}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
params << "#{filter_name}=#{filter_options}"
|
59
|
+
end
|
60
|
+
|
61
|
+
return params.join("&")
|
62
|
+
end
|
63
|
+
|
64
|
+
def validate_filter_options(filter_name, filter_options)
|
65
|
+
raise Exception.new("'#{filter_name}' is not a valid filter8-filter") unless Filter8::AVAILABLE_FILTERS.include?(filter_name)
|
66
|
+
|
67
|
+
if filter_options.respond_to? :each
|
68
|
+
filter_options.each do |filter_option, filter_option_value|
|
69
|
+
raise Exception.new("'#{filter_option}' is not a valid option for filter '#{filter_name}'") unless Filter8::FILTER_PARAMS[filter_name].include?(filter_option)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Filter8
|
2
|
+
class Result
|
3
|
+
attr_accessor :json
|
4
|
+
|
5
|
+
def initialize(json)
|
6
|
+
@json = json
|
7
|
+
end
|
8
|
+
|
9
|
+
def replacement
|
10
|
+
@json["filter"]["replacement"]
|
11
|
+
end
|
12
|
+
|
13
|
+
def matched?
|
14
|
+
@json["filter"]["matched"]
|
15
|
+
end
|
16
|
+
|
17
|
+
def matches
|
18
|
+
@json["filter"]["matches"].map { |match| OpenStruct.new(match) }
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Filter8::Client do
|
4
|
+
|
5
|
+
before do
|
6
|
+
Timecop.freeze(Time.local(2014, 1, 1))
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:client) {Filter8::Client.new(api_key: "12345", api_secret: "abcde")}
|
10
|
+
|
11
|
+
describe "#initialization" do
|
12
|
+
it "will configure a new client with the given parameters" do
|
13
|
+
expect(client.api_key).to eq "12345"
|
14
|
+
expect(client.api_secret).to eq "abcde"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "#send_request" do
|
19
|
+
let(:faraday_response) {double("Faraday::Response", body: { "test" => "response" }.to_json, status: 200)}
|
20
|
+
let(:faraday_connection) {double("Faraday::Connection", basic_auth: nil, post: faraday_response)}
|
21
|
+
let(:filter8_request) {double("Filter8::Request", request_params: { "test" => "data" })}
|
22
|
+
|
23
|
+
before do
|
24
|
+
Faraday.stub(:new).and_return(faraday_connection)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "will correctly configure a new farady-connection to the filter8-server" do
|
28
|
+
expect(Faraday).to receive(:new).with(:url => 'https://api.filter8.com').and_return(faraday_connection)
|
29
|
+
client.send_request(filter8_request)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "will use basic-auth with the api_key and the calculated password" do
|
33
|
+
expect(faraday_connection).to receive(:basic_auth).with(client.api_key, client.password)
|
34
|
+
client.send_request(filter8_request)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "will send the given filter8_requests form-data via post" do
|
38
|
+
expect(faraday_connection).to receive(:post).with("/content/item.js?#{client.nonce_param}", filter8_request.request_params)
|
39
|
+
client.send_request(filter8_request)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "will parse the response as JSON and return the result" do
|
43
|
+
expect(client.send_request(filter8_request)).to eq({ "test" => "response" })
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "#timestamp" do
|
49
|
+
it "will return the current Time as a unix-timestamp" do
|
50
|
+
expect(client.timestamp).to eq Time.local(2014, 1, 1).to_i.to_s
|
51
|
+
end
|
52
|
+
|
53
|
+
it "will return the same value every time it is asked" do
|
54
|
+
Timecop.return
|
55
|
+
|
56
|
+
timestamp = client.timestamp
|
57
|
+
sleep 1.0/10
|
58
|
+
|
59
|
+
expect(client.timestamp).to eq timestamp
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#password" do
|
64
|
+
it "will return a md5-digested combination of nonce and api_secret" do
|
65
|
+
expect(client.password).to eq Digest::MD5.hexdigest("#{client.timestamp}#{client.api_secret}")
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#nonce_param" do
|
70
|
+
it "will return the nonce-param with the clients timestamp" do
|
71
|
+
expect(client.nonce_param).to eq "nonce=#{client.timestamp}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Filter8::Request do
|
4
|
+
|
5
|
+
describe "#initialization" do
|
6
|
+
|
7
|
+
it "will configure a new request with no options" do
|
8
|
+
expect(Filter8::Request.new("fuck")).to be_a Filter8::Request
|
9
|
+
end
|
10
|
+
|
11
|
+
it "will not be initializable without a content" do
|
12
|
+
expect{Filter8::Request.new}.to raise_error
|
13
|
+
end
|
14
|
+
|
15
|
+
it "will translate only options into content and options" do
|
16
|
+
request = Filter8::Request.new(content: "fuck", blacklist: "test")
|
17
|
+
expect(request.content).to eq "fuck"
|
18
|
+
expect(request.blacklist).to eq "test"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "will raise an error when only options and no content is given" do
|
22
|
+
expect{Filter8::Request.new(blacklist: "test")}.to raise_error
|
23
|
+
end
|
24
|
+
|
25
|
+
it "will allow valid filters as options" do
|
26
|
+
request = Filter8::Request.new("fuck", blacklist: "test")
|
27
|
+
expect(request.blacklist).to eq "test"
|
28
|
+
end
|
29
|
+
|
30
|
+
it "will NOT allow invalid filters as options" do
|
31
|
+
expect{Filter8::Request.new("fuck", some_filter: "test")}.to raise_error(Filter8::Exception)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "will allow valid filters with valid options" do
|
35
|
+
request = Filter8::Request.new("fuck", blacklist: {locale: [:en, :de]})
|
36
|
+
|
37
|
+
expect(request.blacklist).to eq({enabled: true, locale: [:en, :de]})
|
38
|
+
end
|
39
|
+
|
40
|
+
it "will NOT allow valid filters with invalid options" do
|
41
|
+
expect{Filter8::Request.new("fuck", blacklist: {myarg: "test"})}.to raise_error(Filter8::Exception)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "will allow filters to be disabled" do
|
45
|
+
request = Filter8::Request.new("fuck", blacklist: {enabled: false})
|
46
|
+
|
47
|
+
expect(request.blacklist).to eq({enabled: false})
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "#request_params" do
|
52
|
+
it "will return the correct parameters, when no options are given" do
|
53
|
+
expect(Filter8::Request.new("fuck").request_params).to eq "content=fuck"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "will return the correct parameters, when one option with a single value is given" do
|
57
|
+
expect(Filter8::Request.new("fuck", blacklist: "test").request_params).to eq "content=fuck&blacklist=test"
|
58
|
+
end
|
59
|
+
|
60
|
+
it "will return the correct parameters, when one option with a hash is given" do
|
61
|
+
expect(Filter8::Request.new("fuck", blacklist: {locale: :en}).request_params).to eq "content=fuck&blacklist.enabled=true&blacklist.locale=en"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "will return the correct parameters, when one option with a multiple value is given" do
|
65
|
+
expect(Filter8::Request.new("fuck", blacklist: {locale: [:en, :de]}).request_params).to eq "content=fuck&blacklist.enabled=true&blacklist.locale=en&blacklist.locale=de"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "will return the correct parameters, when multiple options with a multiple values are given" do
|
69
|
+
expect(Filter8::Request.new("fuck", blacklist: {locale: [:en, :de]}, characters: {character: ["a", "b", "c"]}).request_params).to eq "content=fuck&blacklist.enabled=true&blacklist.locale=en&blacklist.locale=de&characters.enabled=true&characters.character=a&characters.character=b&characters.character=c"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Filter8::Result do
|
4
|
+
|
5
|
+
it "will have a way to access the json-result directly" do
|
6
|
+
result = Filter8::Result.new({"test" => "result"})
|
7
|
+
expect(result.json).to eq({"test" => "result"})
|
8
|
+
end
|
9
|
+
|
10
|
+
context "when initialized with an empty filter8-response" do
|
11
|
+
let(:response_without_results) do
|
12
|
+
{
|
13
|
+
"filter" => {
|
14
|
+
"matched" => false,
|
15
|
+
"matches" => [
|
16
|
+
],
|
17
|
+
"replacement" => "Fuck this motherfucking fuck shit!"
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
let(:result) { Filter8::Result.new(response_without_results) }
|
22
|
+
|
23
|
+
it "will state that there were no matches" do
|
24
|
+
expect(result.matched?).to be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "will return no matches" do
|
28
|
+
expect(result.matches).to eq []
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when initialized with a blacklist filter8-response" do
|
33
|
+
let(:response_with_blacklist_results) do
|
34
|
+
{
|
35
|
+
"filter" => {
|
36
|
+
"matched" => true,
|
37
|
+
"matches" => [
|
38
|
+
{
|
39
|
+
"start" => 0,
|
40
|
+
"length" => 4,
|
41
|
+
"type" => "blacklist",
|
42
|
+
"tags" => [
|
43
|
+
"Vulgarity"
|
44
|
+
],
|
45
|
+
"severity" => "severe",
|
46
|
+
"locale" => "en",
|
47
|
+
"matched" => "fuck",
|
48
|
+
"root" => "fuck",
|
49
|
+
"quality" => 1.0
|
50
|
+
},
|
51
|
+
{
|
52
|
+
"start" => 10,
|
53
|
+
"length" => 13,
|
54
|
+
"type" => "blacklist",
|
55
|
+
"tags" => [
|
56
|
+
"Vulgarity"
|
57
|
+
],
|
58
|
+
"severity" => "severe",
|
59
|
+
"locale" => "en",
|
60
|
+
"matched" => "fucking",
|
61
|
+
"root" => "fuck",
|
62
|
+
"quality" => 1.0
|
63
|
+
},
|
64
|
+
{
|
65
|
+
"start" => 24,
|
66
|
+
"length" => 4,
|
67
|
+
"type" => "blacklist",
|
68
|
+
"tags" => [
|
69
|
+
"Vulgarity"
|
70
|
+
],
|
71
|
+
"severity" => "severe",
|
72
|
+
"locale" => "en",
|
73
|
+
"matched" => "fuck",
|
74
|
+
"root" => "fuck",
|
75
|
+
"quality" => 1.0
|
76
|
+
},
|
77
|
+
{
|
78
|
+
"start" => 29,
|
79
|
+
"length" => 4,
|
80
|
+
"type" => "blacklist",
|
81
|
+
"tags" => [
|
82
|
+
"Vulgarity"
|
83
|
+
],
|
84
|
+
"severity" => "severe",
|
85
|
+
"locale" => "en",
|
86
|
+
"matched" => "shit",
|
87
|
+
"root" => "shit",
|
88
|
+
"quality" => 1.0
|
89
|
+
}
|
90
|
+
],
|
91
|
+
"replacement" => "**** this ************* **** ****!"
|
92
|
+
}
|
93
|
+
}
|
94
|
+
end
|
95
|
+
let(:result) { Filter8::Result.new(response_with_blacklist_results) }
|
96
|
+
|
97
|
+
it "will return the correct replacement" do
|
98
|
+
expect(result.replacement).to eq "**** this ************* **** ****!"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "will state that there were matches" do
|
102
|
+
expect(result.matched?).to be_true
|
103
|
+
end
|
104
|
+
|
105
|
+
it "will return all matches" do
|
106
|
+
expect(result.matches).to eq(response_with_blacklist_results["filter"]["matches"].map { |match| OpenStruct.new(match) })
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Filter8 do
|
4
|
+
|
5
|
+
describe "#configure" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
Filter8.configure do |config|
|
9
|
+
config.api_key = "12345"
|
10
|
+
config.api_secret = "abcde"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
after do
|
15
|
+
Filter8.configuration = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "will correctly retain it's configuration" do
|
19
|
+
expect(Filter8.configuration.api_key).to eq "12345"
|
20
|
+
expect(Filter8.configuration.api_secret).to eq "abcde"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#filter" do
|
25
|
+
context "when not configured first" do
|
26
|
+
it "will raise an error and asks to configure first" do
|
27
|
+
expect{Filter8.filter("test", blacklist: "test")}.to raise_error(Filter8::Exception)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
context "when correct configured first" do
|
31
|
+
let(:request) { double() }
|
32
|
+
let(:client) { double() }
|
33
|
+
let(:result) { double() }
|
34
|
+
|
35
|
+
before do
|
36
|
+
Filter8.configure do |config|
|
37
|
+
config.api_key = "12345"
|
38
|
+
config.api_secret = "abcde"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
after do
|
43
|
+
Filter8.configuration = nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "will use the module-config to configure a client and send a request to filter8 with the given parameters. Returns the result" do
|
47
|
+
expect(Filter8::Client).to receive(:new).with(api_key: "12345", api_secret: "abcde").and_return(client)
|
48
|
+
expect(Filter8::Request).to receive(:new).with("test", blacklist: "test").and_return(request)
|
49
|
+
expect(client).to receive(:send_request).with(request).and_return(result)
|
50
|
+
expect(Filter8.filter("test", blacklist: "test")).to eq result
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filter8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.9'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tim Adler
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 10.1.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 10.1.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.14.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.14.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: timecop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: codeclimate-test-reporter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: An unofficial ruby wrapper for Filter8 (http://filter8.com). An API to
|
84
|
+
filter lots of stuff from texts, e.g. bad words.
|
85
|
+
email: mail (at) toadle (dot) me
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- filter8.gemspec
|
97
|
+
- lib/filter8.rb
|
98
|
+
- lib/filter8/client.rb
|
99
|
+
- lib/filter8/exception.rb
|
100
|
+
- lib/filter8/request.rb
|
101
|
+
- lib/filter8/result.rb
|
102
|
+
- spec/lib/filter8/client_spec.rb
|
103
|
+
- spec/lib/filter8/request_spec.rb
|
104
|
+
- spec/lib/filter8/result_spec.rb
|
105
|
+
- spec/lib/filter8_spec.rb
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
homepage: https://github.com/toadle/filter8
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.2.2
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: An unofficial ruby wrapper for Filter8 (http://filter8.com). An API to filter
|
131
|
+
lots of stuff from texts, e.g. bad words.
|
132
|
+
test_files:
|
133
|
+
- spec/lib/filter8/client_spec.rb
|
134
|
+
- spec/lib/filter8/request_spec.rb
|
135
|
+
- spec/lib/filter8/result_spec.rb
|
136
|
+
- spec/lib/filter8_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
has_rdoc:
|