cleanweb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
  /.ruby-version
19
19
  /.ruby-gemset
20
+ /Guardfile
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ # script: rspec spec
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Cleanweb
2
2
 
3
- TODO: Write a gem description
3
+ [![Build Status](https://travis-ci.org/arrowcircle/cleanweb.png?branch=master)](https://travis-ci.org/arrowcircle/cleanweb)
4
+ [![Code Climate](https://codeclimate.com/github/arrowcircle/cleanweb.png)](https://codeclimate.com/github/arrowcircle/cleanweb)
5
+
6
+ Cleanweb is Ruby gem Yandex Cleanweb service.
4
7
 
5
8
  ## Installation
6
9
 
@@ -8,17 +11,29 @@ Add this line to your application's Gemfile:
8
11
 
9
12
  gem 'cleanweb'
10
13
 
11
- And then execute:
14
+ Get key here:
12
15
 
13
- $ bundle
16
+ http://api.yandex.ru/key/form.xml?service=cw
14
17
 
15
- Or install it yourself as:
18
+ Set key to Cleanweb class:
16
19
 
17
- $ gem install cleanweb
20
+ Cleanweb.api_key = "YOUR KEY HERE"
18
21
 
19
22
  ## Usage
20
23
 
21
- TODO: Write usage instructions here
24
+ To check if content spam or not, create a cleanweb instance and check it for spam:
25
+
26
+ cw = Cleanweb.new({:subject = > "My Test Subject", :body => "My Test Body"})
27
+ cw.spam?
28
+ => false
29
+
30
+ You can add more info to initial params. Here is the list:
31
+
32
+ * `ip` - user IP address,
33
+ * `email` - user email,
34
+ * `name` - user footer signature,
35
+ * `login` - user login,
36
+ * `realname` - user real name
22
37
 
23
38
  ## Contributing
24
39
 
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ task :default => [:spec]
3
+ desc 'run Rspec specs'
4
+ task :spec do
5
+ sh 'rspec spec'
6
+ end
data/cleanweb.gemspec CHANGED
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'cleanweb/version'
4
+ require 'cleanweb'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "cleanweb"
8
- spec.version = Cleanweb::VERSION
8
+ spec.version = Cleanweb::Version::VERSION
9
9
  spec.authors = ["Oleg Bovykin"]
10
10
  spec.email = ["oleg.bovykin@gmail.com"]
11
11
  spec.description = %q{Gem for Yandex Cleanweb intergation}
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
23
  spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "guard-rspec"
24
25
  end
data/lib/cleanweb.rb CHANGED
@@ -1,5 +1,54 @@
1
- require "cleanweb/version"
1
+ # coding: utf-8
2
+
3
+ require 'net/http'
4
+ require 'rexml/document'
5
+ require 'open-uri'
6
+
7
+ class Cleanweb
8
+ require 'cleanweb/version'
9
+ require 'cleanweb/spam'
10
+
11
+ include Version
12
+ include Spam
13
+
14
+ class KeyError < StandardError; end
15
+ class NoSubjectError < StandardError; end
16
+ class NoBodyError < StandardError; end
17
+
18
+ SPAM_URL = "http://cleanweb-api.yandex.ru/1.0/check-spam"
19
+
20
+ class << self
21
+ attr_accessor 'api_key'
22
+ end
23
+ @@api_key = nil
24
+
25
+ def self.api_key= key
26
+ @@api_key = key
27
+ end
28
+
29
+ def initialize params
30
+ check_key
31
+ @params = params
32
+ check_params
33
+ end
34
+
35
+ def check_key
36
+ raise KeyError if @@api_key.nil?
37
+ end
38
+
39
+ def check_params
40
+ check_subject
41
+ check_body
42
+ end
43
+
44
+ def check_subject
45
+ @subject = @params[:subject]
46
+ raise NoSubjectError if @subject.nil?
47
+ end
48
+
49
+ def check_body
50
+ @body = @params[:body]
51
+ raise NoBodyError if @body.nil?
52
+ end
2
53
 
3
- module Cleanweb
4
- # Your code goes here...
5
54
  end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+
3
+ module Cleanweb::Spam
4
+ def parse_xml
5
+ xml = REXML::Document.new(ask_yandex)
6
+ res = []
7
+ begin
8
+ xml.root.elements.each("text") {|e| res << e.attributes["spam-flag"]}
9
+ xml.root.elements.each("links") do |links|
10
+ links.elements.each("link") {|e| res << e.attributes["spam-flag"]}
11
+ end
12
+ rescue
13
+ end
14
+ res.uniq
15
+ end
16
+
17
+ def spam?
18
+ results = parse_xml
19
+ if results == ["no"] && !results.empty?
20
+ false
21
+ else
22
+ true
23
+ end
24
+ end
25
+
26
+ def spam_params
27
+ res = {key: @@api_key}
28
+ @params.slice([:ip, :email, :name, :login, :realname]).each do |k, v|
29
+ res.merge!(k: v) if @params[k].present?
30
+ end
31
+ res["subject-plain"] = @subject
32
+ res["body-plain"] = @body
33
+ res
34
+ end
35
+
36
+ def ask_yandex
37
+ uri = URI.parse(SPAM_URL)
38
+ Net::HTTP.post_form(uri, spam_params).body
39
+ end
40
+ end
@@ -1,3 +1,5 @@
1
- module Cleanweb
2
- VERSION = "0.0.1"
3
- end
1
+ # coding: utf-8
2
+
3
+ module Cleanweb::Version
4
+ VERSION = "0.0.2"
5
+ end
@@ -0,0 +1,8 @@
1
+ <check-spam-result>
2
+ <id>123456789abcd</id>
3
+ <text spam-flag="yes" />
4
+ <links>
5
+ <link href="http://some-url.ru" spam-flag="no" />
6
+ <link href="http://some-url.ru" spam-flag="no" />
7
+ </links>
8
+ </check-spam-result>
@@ -0,0 +1,8 @@
1
+ <check-spam-result>
2
+ <id>123456789abcd</id>
3
+ <text spam-flag="no" />
4
+ <links>
5
+ <link href="http://some-url.ru" spam-flag="yes" />
6
+ <link href="http://some-url.ru" spam-flag="no" />
7
+ </links>
8
+ </check-spam-result>
@@ -0,0 +1,6 @@
1
+ <check-spam-result>
2
+ <id>123456789abcd</id>
3
+ <text spam-flag="no" />
4
+ <links>
5
+ </links>
6
+ </check-spam-result>
@@ -0,0 +1,6 @@
1
+ <check-spam-result>
2
+ <id>123456789abcd</id>
3
+ <text spam-flag="yes" />
4
+ <links>
5
+ </links>
6
+ </check-spam-result>
data/spec/mocks/ok.xml ADDED
@@ -0,0 +1,8 @@
1
+ <check-spam-result>
2
+ <id>123456789abcd</id>
3
+ <text spam-flag="no" />
4
+ <links>
5
+ <link href="http://some-url.ru" spam-flag="no" />
6
+ <link href="http://some-url.ru" spam-flag="no" />
7
+ </links>
8
+ </check-spam-result>
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Cleanweb do
6
+ context "initialize" do
7
+ let(:cleanweb_no_key) { Cleanweb.api_key = nil; Cleanweb.new "text"}
8
+ let(:cleanweb_no_subject) { Cleanweb.new no_subject}
9
+ let(:cleanweb_no_body) { Cleanweb.new no_body}
10
+
11
+ let(:no_subject) { {subject: nil, body: "body"} }
12
+ let(:no_body) { {subject: "subject", body: nil} }
13
+
14
+ it "проверяет наличие ключа" do
15
+ lambda { cleanweb_no_key }.should raise_error(Cleanweb::KeyError)
16
+ Cleanweb.api_key = "123"
17
+ end
18
+
19
+ it "проверяет и задает входные параметры" do
20
+ lambda { cleanweb_no_subject }.should raise_error(Cleanweb::NoSubjectError)
21
+ lambda { cleanweb_no_body }.should raise_error(Cleanweb::NoBodyError)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Cleanweb::Spam do
6
+ context "spam?" do
7
+ let(:params) { {subject: "subject", body: "body"} }
8
+ let(:cleanweb) { Cleanweb.new params }
9
+ let(:ok_response) { File.read(File.join("spec", "mocks", "ok.xml")) }
10
+ let(:links_response) { File.read(File.join("spec", "mocks", "links.xml")) }
11
+ let(:no_links_response) { File.read(File.join("spec", "mocks", "no_links.xml")) }
12
+ let(:no_links_body_response) { File.read(File.join("spec", "mocks", "no_links_body.xml")) }
13
+ let(:body_response) { File.read(File.join("spec", "mocks", "body.xml")) }
14
+
15
+ it "возвращает false если контент не спам" do
16
+ cleanweb.should_receive(:ask_yandex).and_return(ok_response)
17
+ expect(cleanweb.spam?).to eq false
18
+ end
19
+
20
+ it "возвращает true если контент спам, а ссылки ок" do
21
+ cleanweb.should_receive(:ask_yandex).and_return(body_response)
22
+ expect(cleanweb.spam?).to eq true
23
+ end
24
+
25
+ it "возвращает true если контент ок, а ссылки спам" do
26
+ cleanweb.should_receive(:ask_yandex).and_return(links_response)
27
+ expect(cleanweb.spam?).to eq true
28
+ end
29
+
30
+ it "возвращает false если контент ок, а ссылкок нет" do
31
+ cleanweb.should_receive(:ask_yandex).and_return(no_links_response)
32
+ expect(cleanweb.spam?).to eq false
33
+ end
34
+
35
+ it "возвращает true если контент спам, а ссылкок нет" do
36
+ cleanweb.should_receive(:ask_yandex).and_return(no_links_body_response)
37
+ expect(cleanweb.spam?).to eq true
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'cleanweb'
3
+
4
+ RSpec.configure do |c|
5
+ c.mock_with :rspec
6
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cleanweb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-05-07 00:00:00.000000000 Z
12
+ date: 2013-05-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -59,6 +59,22 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
62
78
  description: Gem for Yandex Cleanweb intergation
63
79
  email:
64
80
  - oleg.bovykin@gmail.com
@@ -67,13 +83,24 @@ extensions: []
67
83
  extra_rdoc_files: []
68
84
  files:
69
85
  - .gitignore
86
+ - .rspec
87
+ - .travis.yml
70
88
  - Gemfile
71
89
  - LICENSE.txt
72
90
  - README.md
73
91
  - Rakefile
74
92
  - cleanweb.gemspec
75
93
  - lib/cleanweb.rb
94
+ - lib/cleanweb/spam.rb
76
95
  - lib/cleanweb/version.rb
96
+ - spec/mocks/body.xml
97
+ - spec/mocks/links.xml
98
+ - spec/mocks/no_links.xml
99
+ - spec/mocks/no_links_body.xml
100
+ - spec/mocks/ok.xml
101
+ - spec/models/cleanweb_spec.rb
102
+ - spec/models/spam_spec.rb
103
+ - spec/spec_helper.rb
77
104
  homepage: https://github.com/arrowcircle/cleanweb
78
105
  licenses:
79
106
  - MIT
@@ -99,4 +126,12 @@ rubygems_version: 1.8.25
99
126
  signing_key:
100
127
  specification_version: 3
101
128
  summary: Yandex Cleanweb
102
- test_files: []
129
+ test_files:
130
+ - spec/mocks/body.xml
131
+ - spec/mocks/links.xml
132
+ - spec/mocks/no_links.xml
133
+ - spec/mocks/no_links_body.xml
134
+ - spec/mocks/ok.xml
135
+ - spec/models/cleanweb_spec.rb
136
+ - spec/models/spam_spec.rb
137
+ - spec/spec_helper.rb