yandex_cleanweb 0.0.3 → 0.0.4

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/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - 2.0.0
6
+ notifications:
7
+ email:
8
+ - shatrov@me.com
data/README.md CHANGED
@@ -1,7 +1,11 @@
1
1
  # Yandex Cleanweb
2
2
 
3
+ [![Build Status](https://travis-ci.org/evrone/yandex-cleanweb.png?branch=master)](https://travis-ci.org/evrone/yandex-cleanweb)
4
+
3
5
  Ruby wrapper for [Yandex Cleanweb](http://api.yandex.ru/cleanweb/) spam detector.
4
6
 
7
+ Unfortunatelly, this gem *is not capable with MRI 1.8.7* because of MRI 1.8.7 doesn't have `URI.encode_www_form` method.
8
+
5
9
  ## Installation
6
10
 
7
11
  Add this line to your application's Gemfile:
@@ -32,6 +36,22 @@ YandexCleanweb.spam?(body_html: "some spam <a href='http://spam.com'>spam link</
32
36
  => { id: "request id", links: [ ['http://spam.com', true] ] }
33
37
  ```
34
38
 
39
+ More complex example:
40
+
41
+ ```ruby
42
+
43
+ user_input = "download free porn <a>...</a>"
44
+ if spam_check = YandexCleanweb.spam?(user_input, ip: current_user.ip)
45
+ captcha = YandexCleanweb.get_captcha(spam_check[:id])
46
+
47
+ # now you can show captcha[:url] to user
48
+ # but remember to write captcha[:captcha] to session
49
+
50
+ # to check is captcha enterred by user is valid:
51
+ captcha_valid = YandexCleanweb.valid_captcha?(result[:id], captcha[:captcha], user_captcha)
52
+ end
53
+ ```
54
+
35
55
  If you use Yandex Cleanweb in Rails app, we recommend to set up the api key in `config/initializers/yandex_cleanweb.rb`
36
56
 
37
57
  ## Contributing
data/Rakefile CHANGED
@@ -1 +1,13 @@
1
+ #!/usr/bin/env rake
2
+
1
3
  require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ task :default => :test
7
+
8
+ Rake::TestTask.new do |t|
9
+ t.libs << "lib"
10
+ t.libs << "test"
11
+ t.test_files = FileList["test/**/*_test.rb"]
12
+ t.verbose = true
13
+ end
@@ -1,3 +1,3 @@
1
1
  module YandexCleanweb
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -17,15 +17,17 @@ module YandexCleanweb
17
17
  request_id_tag = doc.xpath('//check-spam-result/id')
18
18
  spam_flag_tag = doc.xpath('//check-spam-result/text')
19
19
 
20
- request_id = request_id_tag[0]
20
+ request_id = request_id_tag[0].content
21
21
  spam_flag = spam_flag_tag[0].attributes["spam-flag"].content
22
22
 
23
23
  if spam_flag == 'yes'
24
- links = doc.xpath('//check-spam-result/links').map { |el|
25
- [attributes["url"], attributes["spam_flag"] == 'yes']
26
- }
24
+ links = doc.xpath('//check-spam-result/links')[0].children
27
25
 
28
- { id: request_id, links: links }
26
+ links.map do |el|
27
+ [el.attributes["url"], el.attributes["spam_flag"] == 'yes']
28
+ end
29
+
30
+ { :id => request_id, :links => links }
29
31
  else
30
32
  false
31
33
  end
@@ -38,10 +40,7 @@ module YandexCleanweb
38
40
  url = doc.xpath('//get-captcha-result/url').text
39
41
  captcha_id = doc.xpath('//get-captcha-result/captcha').text
40
42
 
41
- {
42
- url: url,
43
- captcha: captcha_id
44
- }
43
+ { :url => url, :captcha => captcha_id }
45
44
  end
46
45
 
47
46
  def valid_captcha?(request_id, captcha_id, value)
@@ -55,10 +54,10 @@ module YandexCleanweb
55
54
  def api_check_captcha(request_id, captcha_id, value)
56
55
  check_captcha_url = "#{API_URL}/check-captcha"
57
56
  params = {
58
- key: api_key,
59
- id: request_id,
60
- captcha: captcha_id,
61
- value: value
57
+ :key => api_key,
58
+ :id => request_id,
59
+ :captcha => captcha_id,
60
+ :value => value
62
61
  }
63
62
 
64
63
  uri = URI.parse(check_captcha_url)
@@ -69,7 +68,7 @@ module YandexCleanweb
69
68
 
70
69
  def api_get_captcha(request_id)
71
70
  get_captcha_url = "#{API_URL}/get-captcha"
72
- params = {key: api_key, id: request_id}
71
+ params = { :key => api_key, :id => request_id }
73
72
 
74
73
  uri = URI.parse(get_captcha_url)
75
74
  uri.query = URI.encode_www_form(params)
@@ -77,30 +76,14 @@ module YandexCleanweb
77
76
  Net::HTTP.get(uri)
78
77
  end
79
78
 
80
- def api_check_spam(*options)
81
- cleanweb_options = {}
82
- cleanweb_options["key"] = api_key
79
+ def api_check_spam(options)
80
+ cleanweb_options = { :key => api_key }
83
81
 
84
- if options[0][0].is_a?(String) # fast check
85
- cleanweb_options["body-plain"] = options[0][0]
82
+ if options[0].is_a?(String) # quick check
83
+ cleanweb_options[:body_plain] = options[0]
86
84
  else
87
- options = options[0][0]
88
-
89
- cleanweb_options.merge!({
90
- "body-plain" => options[:body_plain],
91
- "body-html" => options[:body_html],
92
- "body-bbcode" => options[:body_bbcode],
93
-
94
- "subject-html" => options[:subject_html],
95
- "subject-plain" => options[:subject_plain],
96
- "subject-bbcode" => options[:subject_bbcode],
97
-
98
- "ip" => options[:ip],
99
- "email" => options[:email],
100
- "name" => options[:name],
101
- "login" => options[:login],
102
- "realname" => options[:realname]
103
- })
85
+ options = options[0]
86
+ cleanweb_options.merge!(Hash[options.map{ |k,v| [k.to_s.gsub("_","-"), v] }])
104
87
  end
105
88
 
106
89
  check_spam_url = "#{API_URL}/check-spam"
@@ -0,0 +1,7 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ require "minitest/autorun"
4
+ require "minitest/spec"
5
+ require "minitest/pride"
6
+
7
+ require "yandex_cleanweb"
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+ require "test_helper"
3
+
4
+ describe YandexCleanweb do
5
+ before do
6
+ YandexCleanweb.api_key = "cw.1.1.20121227T080449Z.51de1ee126e5ced6.f4f417fb55727520d7e39b00cf5393d4b1ca5e78"
7
+ end
8
+
9
+ describe "#spam?" do
10
+
11
+ describe "simple check" do
12
+ it "works" do
13
+ YandexCleanweb.spam?("фраза").must_equal false
14
+ YandexCleanweb.spam?("недорого увеличение пениса проститутки").must_equal false
15
+ end
16
+ end
17
+
18
+ describe "advanced mode" do
19
+ it "workd" do
20
+ YandexCleanweb.spam?(:body_plain => "my text", :ip => "80.80.40.3").must_equal false
21
+ end
22
+
23
+ it "for some html" do
24
+ result = YandexCleanweb.spam?(:body_html => "some spam <a href='http://spam.com'>spam link</a>")
25
+
26
+ result[:id].wont_be_empty
27
+ result[:links].must_be_empty
28
+ end
29
+
30
+ end
31
+
32
+ end
33
+
34
+ describe "#get_captcha + #valid_captcha?" do
35
+
36
+ it "works for not valid captchas" do
37
+ result = YandexCleanweb.spam?(:body_html => "some spam <a href='http://spam.com'>spam link</a>")
38
+ captcha = YandexCleanweb.get_captcha(result[:id])
39
+
40
+ captcha[:url].wont_be_empty
41
+ captcha[:captcha].wont_be_empty
42
+
43
+ valid = YandexCleanweb.valid_captcha?(result[:id], captcha[:captcha], "1234")
44
+ valid.must_equal false
45
+ end
46
+
47
+ end
48
+ end
@@ -18,4 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_dependency 'nokogiri'
21
+
22
+ gem.add_development_dependency "rake"
23
+ gem.add_development_dependency "minitest", "~> 3.0"
21
24
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yandex_cleanweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-02 00:00:00.000000000 Z
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -27,6 +27,38 @@ dependencies:
27
27
  - - ! '>='
28
28
  - !ruby/object:Gem::Version
29
29
  version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
30
62
  description: Ruby wrapper for Yandex.Cleanweb
31
63
  email:
32
64
  - shatrov@me.com
@@ -35,12 +67,15 @@ extensions: []
35
67
  extra_rdoc_files: []
36
68
  files:
37
69
  - .gitignore
70
+ - .travis.yml
38
71
  - Gemfile
39
72
  - LICENSE.txt
40
73
  - README.md
41
74
  - Rakefile
42
75
  - lib/yandex_cleanweb.rb
43
76
  - lib/yandex_cleanweb/version.rb
77
+ - test/test_helper.rb
78
+ - test/yandex_cleanweb_test.rb
44
79
  - yandex_cleanweb.gemspec
45
80
  homepage: ''
46
81
  licenses: []
@@ -66,4 +101,6 @@ rubygems_version: 1.8.23
66
101
  signing_key:
67
102
  specification_version: 3
68
103
  summary: Ruby wrapper for Yandex.Cleanweb spam detector
69
- test_files: []
104
+ test_files:
105
+ - test/test_helper.rb
106
+ - test/yandex_cleanweb_test.rb