viking 1.0.0
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/.gitignore +17 -0
- data/.rspec +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/lib/core_ext/object.rb +15 -0
- data/lib/core_ext/transformations.rb +35 -0
- data/lib/viking.rb +80 -0
- data/lib/viking/akismet.rb +203 -0
- data/lib/viking/base.rb +74 -0
- data/lib/viking/defensio.rb +384 -0
- data/lib/viking/version.rb +3 -0
- data/spec/core_ext/object_spec.rb +21 -0
- data/spec/core_ext/transformations_spec.rb +41 -0
- data/spec/spec_helper.rb +53 -0
- data/spec/viking/akismet_spec.rb +99 -0
- data/spec/viking/base_spec.rb +47 -0
- data/spec/viking/defensio_spec.rb +147 -0
- data/spec/viking/viking_spec.rb +44 -0
- data/viking.gemspec +21 -0
- metadata +91 -0
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
|
3
|
+
require 'viking'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.mock_with :rspec
|
7
|
+
end
|
8
|
+
|
9
|
+
# See http://blog.jayfields.com/2007/11/ruby-testing-private-methods.html
|
10
|
+
class Class
|
11
|
+
def publicize_methods(instance=nil)
|
12
|
+
saved_private_instance_methods = self.private_instance_methods
|
13
|
+
self.class_eval { public *saved_private_instance_methods }
|
14
|
+
yield(instance)
|
15
|
+
ensure
|
16
|
+
self.class_eval { private *saved_private_instance_methods }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# rSpec Hash additions.
|
22
|
+
#
|
23
|
+
# From
|
24
|
+
# * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
|
25
|
+
# * Neil Rahilly
|
26
|
+
class Hash
|
27
|
+
##
|
28
|
+
# Filter keys out of a Hash.
|
29
|
+
#
|
30
|
+
# { :a => 1, :b => 2, :c => 3 }.except(:a)
|
31
|
+
# => { :b => 2, :c => 3 }
|
32
|
+
def except(*keys)
|
33
|
+
self.reject { |k,v| keys.include?(k || k.to_sym) }
|
34
|
+
end
|
35
|
+
|
36
|
+
##
|
37
|
+
# Override some keys.
|
38
|
+
#
|
39
|
+
# { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
|
40
|
+
# => { :a => 4, :b => 2, :c => 3 }
|
41
|
+
def with(overrides = {})
|
42
|
+
self.merge overrides
|
43
|
+
end
|
44
|
+
|
45
|
+
##
|
46
|
+
# Returns a Hash with only the pairs identified by +keys+.
|
47
|
+
#
|
48
|
+
# { :a => 1, :b => 2, :c => 3 }.only(:a)
|
49
|
+
# => { :a => 1 }
|
50
|
+
def only(*keys)
|
51
|
+
self.reject { |k,v| !keys.include?(k || k.to_sym) }
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Viking::Akismet do
|
4
|
+
|
5
|
+
def valid_options
|
6
|
+
{
|
7
|
+
:blog => 'foo',
|
8
|
+
:api_key => 'bar'
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:akismet) { Viking.connect('akismet', valid_options) }
|
13
|
+
let(:http) { Net::HTTP.new('url') }
|
14
|
+
|
15
|
+
before do
|
16
|
+
Net::HTTP.stub(:new).and_return(http)
|
17
|
+
end
|
18
|
+
|
19
|
+
describe ".new" do
|
20
|
+
|
21
|
+
it "should not have a verified key when initialized" do
|
22
|
+
akismet.send(:verified_key).should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '.url' do
|
28
|
+
|
29
|
+
it 'should return an URL for a request' do
|
30
|
+
Viking::Akismet.url('bar').should == '/1.1/bar'
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#verified?" do
|
36
|
+
|
37
|
+
it "should be verified when all parameters are provided" do
|
38
|
+
http.should_receive(:post).and_return(stub("response", :body => "valid"))
|
39
|
+
|
40
|
+
akismet.should be_verified # #verified? is called twice to make sure #verify_api_key is not called twice
|
41
|
+
akismet.should be_verified
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not be verified if Akismet doesn't validate" do
|
45
|
+
http.should_receive(:post).and_return(stub("response", :body => "invalid"))
|
46
|
+
akismet.should_not be_verified
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should not be verified if its options are invalid" do
|
50
|
+
Viking.connect('akismet', {}).should_not be_verified
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#check_comment" do
|
56
|
+
|
57
|
+
it "should be false if the instance has invalid options" do
|
58
|
+
Viking.connect('akismet', {}).check_comment({}).should be_false
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should be spam when the response body isn't a valid response" do
|
62
|
+
http.should_receive(:post).and_return(stub("response", :body => "invalid"))
|
63
|
+
akismet.check_comment(:user_ip => "127.0.0.1", :user_agent => "Mozilla").should == { :message => "invalid", :spam => true }
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should not be spam when the response body is a valid response" do
|
67
|
+
http.should_receive(:post).and_return(stub("response", :body => "false"))
|
68
|
+
akismet.check_comment(:user_ip => "127.0.0.1", :user_agent => "Mozilla").should == { :message => "false", :spam => false }
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#mark_as_spam" do
|
74
|
+
|
75
|
+
it 'should be false if the instance has invalid options' do
|
76
|
+
Viking.connect('akismet', {}).mark_as_spam({}).should be_false
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'should return the response body' do
|
80
|
+
http.should_receive(:post).and_return(stub('response', :body => "foo"))
|
81
|
+
akismet.mark_as_spam({}).should == { :message => "foo" }
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
describe '#mark_as_ham' do
|
87
|
+
|
88
|
+
it 'should be false if the instance has invalid options' do
|
89
|
+
Viking.connect('akismet', {}).mark_as_ham({}).should be_false
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should return the response body' do
|
93
|
+
http.should_receive(:post).and_return(stub('response', :body => "foo"))
|
94
|
+
akismet.mark_as_ham({}).should == { :message => "foo" }
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Viking::Base do
|
4
|
+
|
5
|
+
def valid_base_options
|
6
|
+
{
|
7
|
+
:api_key => "1234abc",
|
8
|
+
:blog => "wiki.mysite.com"
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:base) { Viking::Base.new({}) }
|
13
|
+
|
14
|
+
describe "#mark_as_spam_or_ham" do
|
15
|
+
|
16
|
+
it "should mark as spam when is_spam is true" do
|
17
|
+
base.should_receive(:mark_as_spam).and_return("I will be spam")
|
18
|
+
base.mark_as_spam_or_ham(true, {}).should == "I will be spam"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should mark as ham when is_spam is false" do
|
22
|
+
base.should_receive(:mark_as_ham).and_return("I will be ham")
|
23
|
+
base.mark_as_spam_or_ham(false, {}).should == "I will be ham"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#invalid_options?" do
|
29
|
+
|
30
|
+
it "should be false if the required options are non-nil" do
|
31
|
+
base.options = valid_base_options
|
32
|
+
base.should_not be_invalid_options
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be true if the options don't include an API key" do
|
36
|
+
base.options = valid_base_options.except(:api_key)
|
37
|
+
base.should be_invalid_options
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should be true if the options don't include a blog address" do
|
41
|
+
base.options = valid_base_options.except(:blog)
|
42
|
+
base.should be_invalid_options
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Viking::Defensio do
|
4
|
+
|
5
|
+
def valid_options
|
6
|
+
{
|
7
|
+
:blog => 'foo',
|
8
|
+
:api_key => 'bar'
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
def defensio_with_bad_options
|
13
|
+
Viking.connect('defensio', {})
|
14
|
+
end
|
15
|
+
|
16
|
+
let(:defensio) { Viking.connect('defensio', valid_options) }
|
17
|
+
let(:http) { Net::HTTP.new('url') }
|
18
|
+
|
19
|
+
before do
|
20
|
+
Net::HTTP.stub!(:new).and_return(http)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '.new' do
|
24
|
+
|
25
|
+
it 'should not have verified options when initialized' do
|
26
|
+
defensio_with_bad_options.send(:verify_options).should == false
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#verified?' do
|
32
|
+
|
33
|
+
it "should be verified when all parameters are provided" do
|
34
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n status: success"))
|
35
|
+
|
36
|
+
defensio.should be_verified # called twice to make sure #validate-key is not called twice
|
37
|
+
defensio.should be_verified
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not be verified if Defensio doesn't validate" do
|
41
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n status: fail"))
|
42
|
+
defensio.should_not be_verified
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not be verified if its options are invalid" do
|
46
|
+
defensio_with_bad_options.should_not be_verified
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#check_article' do
|
52
|
+
|
53
|
+
it 'should be false if its options are invalid' do
|
54
|
+
defensio_with_bad_options.check_article({}).should be_false
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'should check the article with Defensio' do
|
58
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
|
59
|
+
defensio.check_article({})[:foo].should == "bar"
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#check_comment' do
|
65
|
+
|
66
|
+
it 'should be false if its options are invalid' do
|
67
|
+
defensio_with_bad_options.check_comment({}).should be_false
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should raise a NoMethodError if options are provided without an article_date that responds to strftime' do
|
71
|
+
http.should_not_receive(:post)
|
72
|
+
expect { defensio.check_comment(:article_date => nil) }.to raise_error(NoMethodError)
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should check the comment with Defensio' do
|
76
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
|
77
|
+
defensio.check_comment(:article_date => Time.now)[:foo].should == "bar"
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#mark_as_spam' do
|
83
|
+
|
84
|
+
it 'should be false if its options are invalid' do
|
85
|
+
defensio_with_bad_options.mark_as_spam({}).should be_false
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should mark the comments whose signatures are provided as spam' do
|
89
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
|
90
|
+
defensio.mark_as_spam(:signatures => "1,2,3")[:foo].should == "bar"
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#mark_as_ham' do
|
96
|
+
|
97
|
+
it 'should be false if its options are invalid' do
|
98
|
+
defensio_with_bad_options.mark_as_ham({}).should be_false
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'should mark the comments whose signatures are provided as spam' do
|
102
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
|
103
|
+
defensio.mark_as_ham(:signatures => "1,2,3").should == { :foo => "bar" }
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
describe '#stats' do
|
109
|
+
|
110
|
+
it 'should be false if its options are invalid' do
|
111
|
+
defensio_with_bad_options.stats.should be_false
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'should return stats about the blog' do
|
115
|
+
http.should_receive(:post).and_return(stub("response", :body => "defensio-result:\n foo: bar"))
|
116
|
+
defensio.stats.should == { :foo => "bar" }
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
describe '#url' do
|
122
|
+
|
123
|
+
it 'should return an URL for the specified action' do
|
124
|
+
defensio.url('get-stats').should == '/blog/1.2/get-stats/bar.yaml'
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
describe '#process_response_body' do
|
130
|
+
|
131
|
+
it 'should return the defensio-response portion of the YAML response as a Hash' do
|
132
|
+
Viking::Defensio.publicize_methods do
|
133
|
+
v = Viking.connect('defensio', {})
|
134
|
+
v.process_response_body("defensio-result:\n foo: bar").should == { :foo => "bar" }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'should return the entire response with failure as the status if the response is not as expected' do
|
139
|
+
Viking::Defensio.publicize_methods do
|
140
|
+
v = Viking.connect('defensio', {})
|
141
|
+
v.process_response_body("foo:\n bar: baz").should == { :data => { 'foo' => { 'bar' => 'baz' } }, :status => "fail" }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Viking do
|
4
|
+
|
5
|
+
it { should respond_to :logger }
|
6
|
+
it { should respond_to :default_engine }
|
7
|
+
it { should respond_to :connect_options }
|
8
|
+
it { should respond_to :default_instance }
|
9
|
+
|
10
|
+
describe ".connect" do
|
11
|
+
|
12
|
+
it "should load the Defensio engine" do
|
13
|
+
Viking.connect('defensio', {}).should be_a_kind_of(Viking::Defensio)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should load the Akismet engine" do
|
17
|
+
Viking.connect('akismet', {}).should be_a_kind_of(Viking::Akismet)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should be nil if the engine is nil" do
|
21
|
+
Viking.connect(nil, {}).should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be nil if the engine is blank" do
|
25
|
+
Viking.connect('', {}).should be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
describe ".enabled?" do
|
31
|
+
|
32
|
+
it "should not be enabled if a default instance has not be initialized" do
|
33
|
+
Viking.should_not be_enabled
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should be enabled if a default instance has been initialized" do
|
37
|
+
Viking.default_engine = 'defensio'
|
38
|
+
Viking.connect_options = '1234abc'
|
39
|
+
Viking.should be_enabled
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/viking.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'viking/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "viking"
|
8
|
+
gem.version = Viking::VERSION
|
9
|
+
gem.authors = ["Risk Danger Olson", "James Herdman", "Pierre-Louis Gottfrois"]
|
10
|
+
gem.email = ["pl.gottfrois@dimelo.com"]
|
11
|
+
gem.description = %q{Modernized gem version of the Viking plugin}
|
12
|
+
gem.summary = %q{Modernized gem version of the Viking plugin}
|
13
|
+
gem.homepage = "https://github.com/dimelo/viking"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency(%q{rspec}, ['~> 2.12.0'])
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: viking
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Risk Danger Olson
|
9
|
+
- James Herdman
|
10
|
+
- Pierre-Louis Gottfrois
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
date: 2013-03-04 00:00:00.000000000 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
requirement: !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.12.0
|
24
|
+
type: :development
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ~>
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: 2.12.0
|
32
|
+
description: Modernized gem version of the Viking plugin
|
33
|
+
email:
|
34
|
+
- pl.gottfrois@dimelo.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- .gitignore
|
40
|
+
- .rspec
|
41
|
+
- Gemfile
|
42
|
+
- LICENSE.txt
|
43
|
+
- README.md
|
44
|
+
- Rakefile
|
45
|
+
- lib/core_ext/object.rb
|
46
|
+
- lib/core_ext/transformations.rb
|
47
|
+
- lib/viking.rb
|
48
|
+
- lib/viking/akismet.rb
|
49
|
+
- lib/viking/base.rb
|
50
|
+
- lib/viking/defensio.rb
|
51
|
+
- lib/viking/version.rb
|
52
|
+
- spec/core_ext/object_spec.rb
|
53
|
+
- spec/core_ext/transformations_spec.rb
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
- spec/viking/akismet_spec.rb
|
56
|
+
- spec/viking/base_spec.rb
|
57
|
+
- spec/viking/defensio_spec.rb
|
58
|
+
- spec/viking/viking_spec.rb
|
59
|
+
- viking.gemspec
|
60
|
+
homepage: https://github.com/dimelo/viking
|
61
|
+
licenses: []
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ! '>='
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '0'
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
requirements: []
|
79
|
+
rubyforge_project:
|
80
|
+
rubygems_version: 1.8.23
|
81
|
+
signing_key:
|
82
|
+
specification_version: 3
|
83
|
+
summary: Modernized gem version of the Viking plugin
|
84
|
+
test_files:
|
85
|
+
- spec/core_ext/object_spec.rb
|
86
|
+
- spec/core_ext/transformations_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/viking/akismet_spec.rb
|
89
|
+
- spec/viking/base_spec.rb
|
90
|
+
- spec/viking/defensio_spec.rb
|
91
|
+
- spec/viking/viking_spec.rb
|