webpurify 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
19
+
20
+ test.rb
data/README.md ADDED
@@ -0,0 +1,170 @@
1
+ WebPurify RubyGem
2
+ =================
3
+
4
+ This gem allows simple interaction with the WebPurify API using Ruby. For more information about WebPurify and the services it offers, check out http://webpurify.com/.
5
+
6
+
7
+ ### Commands
8
+
9
+ ##### Filters
10
+ * [check](#check)
11
+ * [check_count](#check_count)
12
+ * [replace](#replace)
13
+ * [return](#return)
14
+
15
+ ##### Blacklist
16
+ * [add_to_blacklist](#add_to_blacklist)
17
+ * [remove_from_blacklist](#remove_from_blacklist)
18
+ * [get_blacklist](#get_blacklist)
19
+
20
+ ##### Whitelist
21
+ * [add_to_whitelist](#add_to_whitelist)
22
+ * [remove_from_whitelist](#remove_from_whitelist)
23
+ * [get_whitelist](#get_whitelist)
24
+
25
+
26
+ Install & Initialize
27
+ --------------------
28
+
29
+ `gem install WebPurify`
30
+
31
+ or with bundler
32
+
33
+ `gem WebPurify`
34
+
35
+ `bundle install`
36
+
37
+ Initialize with
38
+
39
+ ```rb
40
+ @wp = WebPurify::Client.new('my_api_key')
41
+ ```
42
+
43
+ or
44
+
45
+ ```rb
46
+ @wp = WebPurify::Client.new({
47
+ api_key: 'my_api_key',
48
+ endpoint: :us, # can be :us, :eu, :ap (defaults to :us)
49
+ enterprise: false, # true for ssl (default false, enterprise key required)
50
+ })
51
+ ```
52
+
53
+ Commands
54
+ --------
55
+
56
+ <a name="check" />
57
+ ### check
58
+
59
+ Check a string of text for profanity. Returns true if profanity found, false if none.
60
+
61
+ ```rb
62
+ puts @wp.check('some profane text')
63
+ ```
64
+
65
+
66
+ <a name="check_count" />
67
+ ### check_count
68
+
69
+ Check a string of text for profanity. Returns number of words if profanity found, 0 if none.
70
+
71
+ ```rb
72
+ puts @wp.check_count('profane text')
73
+ ```
74
+
75
+
76
+ <a name="replace" />
77
+ ### replace
78
+ Check a string of text for profanity. Replaces any found profanity with a provided symbol, and returns the formatted string.
79
+
80
+ ```rb
81
+ puts @wp.replace('profane text', '*')
82
+ ```
83
+
84
+
85
+ <a name="return" />
86
+ ### return
87
+ Check a string of text for profanity. If any found, returns an array of profane words. Else, returns empty array.
88
+
89
+ ```rb
90
+ p @wp.return('profane text')
91
+ ```
92
+
93
+
94
+ ### Options
95
+ All filter commands can take an additional options object, just before the callback. The available options are:
96
+
97
+ ```rb
98
+ options = {
99
+ lang: 'en', # the 2 letter language code for the text you are submitting
100
+ semail: 1, # treat email addresses like profanity
101
+ sphone: 1, # treat phone numbers like profanity
102
+ slink: 1 # treat urls like profanity
103
+ }
104
+
105
+ puts @wp.check('some profane text', options)
106
+ ```
107
+
108
+
109
+ <a name="add_to_blacklist" />
110
+ ### add_to_blacklist
111
+ Add a word to the blacklist.
112
+
113
+ ```rb
114
+ @wp.add_to_blacklist('my_awesome_word')
115
+ ```
116
+
117
+ For Deep search, add optional parameter 1 after word:
118
+
119
+ ```rb
120
+ @wp.add_to_blacklist('my_awesome_word', 1)
121
+ ```
122
+
123
+
124
+ <a name="remove_from_blacklist" />
125
+ ### remove_from_blacklist
126
+ Remove a word from the blacklist.
127
+
128
+ ```rb
129
+ @wp.remove_from_blacklist('my_awesome_word')
130
+ ```
131
+
132
+
133
+ <a name="get_blacklist" />
134
+ ### get_blacklist
135
+ Get the blacklist as an array of words.
136
+
137
+ ```rb
138
+ p @wp.get_blacklist
139
+ ```
140
+
141
+
142
+ <a name="add_to_whitelist" />
143
+ ### add_to_whitelist
144
+ Add a word to the whitelist.
145
+
146
+ ```rb
147
+ @wp.add_to_whitelist('my_awesome_word')
148
+ ```
149
+
150
+
151
+ <a name="remove_from_whitelist" />
152
+ ### remove_from_whitelist
153
+ Remove a word from the whitelist.
154
+
155
+ ```rb
156
+ @wp.remove_from_whitelist('my_awesome_word')
157
+ ```
158
+
159
+
160
+ <a name="get_whitelist" />
161
+ ### get_whitelist
162
+ Get the whitelist as an array of words.
163
+
164
+ ```rb
165
+ p @wp.get_whitelist
166
+ ```
167
+
168
+ ## To-do:
169
+ * Handle error responses
170
+ * Tests
data/lib/web_purify.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative 'web_purify/constants'
2
+ require_relative 'web_purify/request'
3
+ require_relative 'web_purify/client'
4
+
5
+ # WebPurify Gem
6
+ # @author Miles Zimmerman
7
+ #
8
+ # The WebPurify gem provides easy access to the WebPurify API service. For more
9
+ # information regarding WebPurify and the services it offers, check out
10
+ # http://webpurify.com
11
+ module WebPurify
12
+ end
@@ -0,0 +1,39 @@
1
+ require_relative 'methods/filters'
2
+ require_relative 'methods/blacklist'
3
+ require_relative 'methods/whitelist'
4
+
5
+ # WebPurify::Client
6
+ #
7
+ # The WebPurify::Client class maintains state of the request parameters like api_key, endpoint, etc.,
8
+ # and provides easy methods for accessing WebPurify
9
+ class WebPurify::Client
10
+ include WebPurify::Filters
11
+ include WebPurify::Blacklist
12
+ include WebPurify::Whitelist
13
+
14
+ # Initialize the class
15
+ #
16
+ # @param options [String, Hash] Either an API key string, or a hash of options
17
+ def initialize(options)
18
+ if options.is_a? String
19
+ @api_key = options
20
+ @endpoint = :us
21
+ @enterprise = false
22
+ elsif options.is_a? Hash
23
+ @api_key = options[:api_key]
24
+ @endpoint = options[:endpoint] || :us
25
+ @enterprise = options[:enterprise] || false
26
+ end
27
+
28
+ @request_base = {
29
+ :host => WebPurify::Constants.endpoints[@endpoint],
30
+ :path => WebPurify::Constants.rest_path,
31
+ :scheme => WebPurify::Constants.scheme(@enterprise)
32
+ }
33
+
34
+ @query_base = {
35
+ :api_key => @api_key,
36
+ :format => WebPurify::Constants.format
37
+ }
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ module WebPurify
2
+
3
+ # WebPurify::Constants
4
+ #
5
+ # WebPurify::Constants holds all the static variables used to access the API.
6
+ module Constants
7
+
8
+ # The various endpoints to access the API
9
+ #
10
+ # @return [Hash] A hash of the endpoints
11
+ def self.endpoints
12
+ return {
13
+ :us => 'api1.webpurify.com',
14
+ :eu => 'api1-eu.webpurify.com',
15
+ :ap => 'api1-ap.webpurify.com'
16
+ }
17
+ end
18
+
19
+
20
+ # Path appended to the endpoint URI
21
+ #
22
+ # @return [String] The rest path
23
+ def self.rest_path
24
+ return '/services/rest/'
25
+ end
26
+
27
+
28
+ # The response format
29
+ #
30
+ # @return [String] The format
31
+ def self.format
32
+ return 'json'
33
+ end
34
+
35
+
36
+ # Returns either http or https depending on whether enterprise mode
37
+ # is selected.
38
+ #
39
+ # @param enterprise [Boolean] True if enterprise mode, false if not
40
+ # @return [String] The scheme, either http or https
41
+ def self.scheme(enterprise)
42
+ return enterprise ? 'https' : 'http'
43
+ end
44
+
45
+
46
+ # A hash for each API method
47
+ #
48
+ # @return [Hash] The method hash
49
+ def self.methods
50
+ return {
51
+ :check => 'webpurify.live.check',
52
+ :check_count => 'webpurify.live.checkcount',
53
+ :replace => 'webpurify.live.replace',
54
+ :return => 'webpurify.live.return',
55
+ :add_to_blacklist => 'webpurify.live.addtoblacklist',
56
+ :add_to_whitelist => 'webpurify.live.addtowhitelist',
57
+ :remove_from_blacklist => 'webpurify.live.removefromblacklist',
58
+ :remove_from_whitelist => 'webpurify.live.removefromwhitelist',
59
+ :get_blacklist => 'webpurify.live.getblacklist',
60
+ :get_whitelist => 'webpurify.live.getwhitelist'
61
+ }
62
+ end
63
+
64
+ end
65
+ end
@@ -0,0 +1,54 @@
1
+ module WebPurify
2
+
3
+ # WebPurify::Blacklist
4
+ #
5
+ # Handles all methods related to the WebPurify Blacklist
6
+ module Blacklist
7
+
8
+ # Add a word to the blacklist
9
+ #
10
+ # @param word [String] The word to add to the blacklist
11
+ # @param deep_search [Integer] 1 if deep search on word, otherwise 0 for not (default 0)
12
+ # @return [Boolean] True if successful, false if not
13
+ def add_to_blacklist(word, deep_search=0)
14
+ params = {
15
+ :method => WebPurify::Constants.methods[:add_to_blacklist],
16
+ :word => word,
17
+ :ds => deep_search
18
+ }
19
+ parsed = WebPurify::Request.query(@request_base, @query_base, params)
20
+ return parsed[:success]=='1'
21
+ end
22
+
23
+
24
+ # Remove a word from the blacklist
25
+ #
26
+ # @param word [String] The word to remove from the blacklist
27
+ # @return [Boolean] True if successful, false if not
28
+ def remove_from_blacklist(word)
29
+ params = {
30
+ :method => WebPurify::Constants.methods[:remove_from_blacklist],
31
+ :word => word
32
+ }
33
+ parsed = WebPurify::Request.query(@request_base, @query_base, params)
34
+ return parsed[:success]=='1'
35
+ end
36
+
37
+
38
+ # Get the blacklist
39
+ #
40
+ # @return [Array] An array of words in the blacklist
41
+ def get_blacklist
42
+ params = {
43
+ :method => WebPurify::Constants.methods[:get_blacklist]
44
+ }
45
+ parsed = WebPurify::Request.query(@request_base, @query_base, params)
46
+ if parsed[:word]
47
+ return [] << parsed[:word]
48
+ else
49
+ return []
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,70 @@
1
+ module WebPurify
2
+ module Filters
3
+
4
+ # Check for existence of profanity
5
+ #
6
+ # @param text [String] Text to test for profanity
7
+ # @param options [Hash] Options hash, used to set additional parameters
8
+ # @return [Boolean] True if text contains profanity, false if not
9
+ def check(text, options={})
10
+ params = {
11
+ :method => WebPurify::Constants.methods[:check],
12
+ :text => text
13
+ }
14
+ parsed = WebPurify::Request.query(@request_base, @query_base, params.merge(options))
15
+ return parsed[:found]=='1'
16
+ end
17
+
18
+
19
+ # Check for existence of profanity and return number of profane words found
20
+ #
21
+ # @param text [String] Text to test for profanity
22
+ # @param options [Hash] Options hash, used to set additional parameters
23
+ # @return [Integer] The number of profane words found in text
24
+ def check_count(text, options={})
25
+ params = {
26
+ :method => WebPurify::Constants.methods[:check_count],
27
+ :text => text
28
+ }
29
+ parsed = WebPurify::Request.query(@request_base, @query_base, params.merge(options))
30
+ return parsed[:found].to_i
31
+ end
32
+
33
+
34
+ # Replace any matched profanity with provided symbol
35
+ #
36
+ # @param text [String] Text to test for profanity
37
+ # @param symbol [String] The symbol to replace each character of matched profanity
38
+ # @param options [Hash] Options hash, used to set additional parameters
39
+ # @return [String] The original text, replaced with the provided symbol
40
+ def replace(text, symbol, options={})
41
+ params = {
42
+ :method => WebPurify::Constants.methods[:replace],
43
+ :text => text,
44
+ :replacesymbol => symbol
45
+ }
46
+ parsed = WebPurify::Request.query(@request_base, @query_base, params.merge(options))
47
+ return parsed[:text]
48
+ end
49
+
50
+
51
+ # Return an array of matched profanity
52
+ #
53
+ # @param text [String] Text to test for profanity
54
+ # @param options [Hash] Options hash, used to set additional parameters
55
+ # @return [Array] The array of matched profane words
56
+ def return(text, options={})
57
+ params = {
58
+ :method => WebPurify::Constants.methods[:return],
59
+ :text => text
60
+ }
61
+ parsed = WebPurify::Request.query(@request_base, @query_base, params.merge(options))
62
+ if parsed[:expletive]
63
+ return [] << parsed[:expletive]
64
+ else
65
+ return []
66
+ end
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,52 @@
1
+ module WebPurify
2
+
3
+ # WebPurify::Whitelist
4
+ #
5
+ # Handles all methods related to the WebPurify whitelist
6
+ module Whitelist
7
+
8
+ # Add a word to the whitelist
9
+ #
10
+ # @param word [String] The word to add to the whitelist
11
+ # @return [Boolean] True if successful, false if not
12
+ def add_to_whitelist(word)
13
+ params = {
14
+ :method => WebPurify::Constants.methods[:add_to_whitelist],
15
+ :word => word
16
+ }
17
+ parsed = WebPurify::Request.query(@request_base, @query_base, params)
18
+ return parsed[:success]=='1'
19
+ end
20
+
21
+
22
+ # Remove a word from the whitelist
23
+ #
24
+ # @param word [String] The word to remove from the whitelist
25
+ # @return [Boolean] True if successful, false if not
26
+ def remove_from_whitelist(word)
27
+ params = {
28
+ :method => WebPurify::Constants.methods[:remove_from_whitelist],
29
+ :word => word
30
+ }
31
+ parsed = WebPurify::Request.query(@request_base, @query_base, params)
32
+ return parsed[:success]=='1'
33
+ end
34
+
35
+
36
+ # Get the whitelist
37
+ #
38
+ # @return [Array] An array of words in the whitelist
39
+ def get_whitelist
40
+ params = {
41
+ :method => WebPurify::Constants.methods[:get_whitelist]
42
+ }
43
+ parsed = WebPurify::Request.query(@request_base, @query_base, params)
44
+ if parsed[:word]
45
+ return [] << parsed[:word]
46
+ else
47
+ return []
48
+ end
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,62 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require 'json'
5
+
6
+ module WebPurify
7
+
8
+ # WebPurify::Request
9
+ #
10
+ # WebPurify::Request handles the HTTP/HTTPS queries to the API endpoints,
11
+ # converting returned JSON into a usable object.
12
+ module Request
13
+
14
+ WRAPPER = :rsp
15
+
16
+
17
+ # Converts a hash of key/values into a url-ready query string
18
+ #
19
+ # @param hash [Hash] The hash to be converted
20
+ # @return [String] The formatted query string
21
+ def self.to_query(hash)
22
+ parameters = []
23
+ hash.each {|k,v| parameters << "#{k}=#{v}" }
24
+ return URI.encode(parameters.join("&"))
25
+ end
26
+
27
+
28
+ # Executes a query to the API endpoint
29
+ #
30
+ # @param request_base [Hash] The base parameters for the request (comes from WebPurify::Client initialize())
31
+ # @param query_base [Hash] The base parameters for the query (api_key, format)
32
+ # @param params [Hash] The unique query parameters
33
+ # @return [Hash] A hash parsed from the JSON response
34
+ def self.query(request_base, query_base, params)
35
+ q = query_base.merge(params)
36
+ uri_builder = (request_base[:scheme]=='https') ? URI::HTTPS : URI::HTTP
37
+ uri = uri_builder.build(
38
+ :host => request_base[:host],
39
+ :path => request_base[:path],
40
+ :query => WebPurify::Request.to_query(q)
41
+ )
42
+ return JSON.parse(WebPurify::Request.get(uri, request_base[:scheme]), :symbolize_names => true)[WRAPPER]
43
+ end
44
+
45
+
46
+ # Handles making the query according to http or https scheme
47
+ #
48
+ # @param uri [String] The uri to be queried
49
+ # @param scheme [String] The scheme (http, https)
50
+ # @return [String] The JSON request response
51
+ def self.get(uri, scheme)
52
+ req = (scheme=='https') ? Net::HTTPS : Net::HTTP
53
+ begin
54
+ request = req.get(uri)
55
+ rescue Exception => e
56
+ p e
57
+ end
58
+ return request
59
+ end
60
+
61
+ end
62
+ end
@@ -0,0 +1,3 @@
1
+ module WebPurify
2
+ VERSION = "0.5.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'web_purify/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "webpurify"
8
+ s.version = WebPurify::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Miles Zimmerman"]
11
+ s.email = ["miles@zserver.org"]
12
+ s.homepage = "https://github.com/mileszim/webpurify-gem"
13
+ s.summary = %q{A RubyGem for interfacing with the WebPurify API.}
14
+ s.description = %q{A RubyGem for interfacing with the WebPurify API.}
15
+
16
+ s.add_development_dependency "rspec", "~>2.5.0"
17
+
18
+ s.add_dependency "json"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webpurify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Miles Zimmerman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.5.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 2.5.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A RubyGem for interfacing with the WebPurify API.
47
+ email:
48
+ - miles@zserver.org
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - README.md
55
+ - lib/web_purify.rb
56
+ - lib/web_purify/client.rb
57
+ - lib/web_purify/constants.rb
58
+ - lib/web_purify/methods/blacklist.rb
59
+ - lib/web_purify/methods/filters.rb
60
+ - lib/web_purify/methods/whitelist.rb
61
+ - lib/web_purify/request.rb
62
+ - lib/web_purify/version.rb
63
+ - web_purify.gemspec
64
+ homepage: https://github.com/mileszim/webpurify-gem
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: A RubyGem for interfacing with the WebPurify API.
88
+ test_files: []
89
+ has_rdoc: