cachai 0.2.3 → 0.2.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.
- checksums.yaml +4 -4
- data/cachai.gemspec +1 -1
- data/db/schema.rb +3 -2
- data/lib/cachai.rb +14 -4
- data/spec/post_comments_spec.rb +34 -6
- data/spec/spec_helper.rb +6 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3798691a1372fd5f11bbb2a04d67ca5f76c33133
|
4
|
+
data.tar.gz: 0ac3cafdb7f2fd8fed54ef8ecddf864f0b261157
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8fc4320584cc9216cc59a347a3f3d43432b2614a7d549375024fe9deb54d784105fc0b476351ae5a7132d8223c24beb76911b341781c32460f2ab775278b9b6
|
7
|
+
data.tar.gz: 5672e5803fd65fa80ce4d7dbb869891d96e39a4f034e31de0fcd5e8332b35fd040c81e414b8456cf9cc40435a53b82566d002a92c56ece03d99d0adcedd39adb
|
data/cachai.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "cachai"
|
7
|
-
spec.version = '0.2.
|
7
|
+
spec.version = '0.2.4'
|
8
8
|
spec.authors = ["Tomás Pollak"]
|
9
9
|
spec.email = ["tomas@forkhq.com"]
|
10
10
|
spec.description = %q{Middleware for embedabble comments.}
|
data/db/schema.rb
CHANGED
@@ -14,8 +14,9 @@
|
|
14
14
|
ActiveRecord::Schema.define(version: 20130901232253) do
|
15
15
|
|
16
16
|
create_table "posts", force: true do |t|
|
17
|
-
t.integer
|
18
|
-
t.string
|
17
|
+
t.integer "comments_allowed", :default => 1, :null => false
|
18
|
+
t.string "path", :null => false
|
19
|
+
t.timestamp "created_at"
|
19
20
|
end
|
20
21
|
|
21
22
|
create_table "responses", force: true do |t|
|
data/lib/cachai.rb
CHANGED
@@ -25,6 +25,7 @@ module Cachai
|
|
25
25
|
def initialize(app, opts = {})
|
26
26
|
domain = opts.delete(:domain) or raise 'Domain required.'
|
27
27
|
redis_host = opts.delete(:redis_host) || 'localhost'
|
28
|
+
@duration = opts.delete(:duration)
|
28
29
|
|
29
30
|
Cachai.boot(domain, redis_host)
|
30
31
|
|
@@ -78,11 +79,20 @@ module Cachai
|
|
78
79
|
end
|
79
80
|
|
80
81
|
check_domain!(data['domain'])
|
81
|
-
|
82
|
+
if data['protocol'].blank? or data['path'].blank?
|
83
|
+
return halt(422, "Missing params.")
|
84
|
+
end
|
85
|
+
|
86
|
+
post = Post.find_or_create_by_path(data['path'])
|
87
|
+
if @duration && post.created_at < @duration.days.ago
|
88
|
+
return halt(400, "Comments closed.")
|
89
|
+
end
|
82
90
|
|
83
|
-
headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
|
84
91
|
permalink = 'http://' + data['domain'] + data['path']
|
85
|
-
|
92
|
+
|
93
|
+
if is_spam?(data, permalink, request)
|
94
|
+
return halt(401, "No spam allowed.")
|
95
|
+
end
|
86
96
|
|
87
97
|
attrs = {
|
88
98
|
:content => data['content'],
|
@@ -93,11 +103,11 @@ module Cachai
|
|
93
103
|
:author_ip => request.ip
|
94
104
|
}
|
95
105
|
|
96
|
-
post = Post.find_or_create_by_path(data['path'])
|
97
106
|
response = Response.create!(attrs.merge(:post_id => post.id))
|
98
107
|
Cachai.clear_cache(data['path'])
|
99
108
|
notify_new_response(response, data['path']) if @recipient
|
100
109
|
|
110
|
+
headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
|
101
111
|
json({ :status => 'ok', :comment => response })
|
102
112
|
|
103
113
|
rescue JSON::ParserError
|
data/spec/post_comments_spec.rb
CHANGED
@@ -25,7 +25,6 @@ describe 'post comment' do
|
|
25
25
|
|
26
26
|
it 'returns 400' do
|
27
27
|
post_comment :domain => nil
|
28
|
-
puts last_response.body
|
29
28
|
last_response.status.should == 400
|
30
29
|
end
|
31
30
|
|
@@ -35,7 +34,7 @@ describe 'post comment' do
|
|
35
34
|
|
36
35
|
it 'returns 400' do
|
37
36
|
post_comment :protocol => nil
|
38
|
-
last_response.status.should ==
|
37
|
+
last_response.status.should == 422
|
39
38
|
end
|
40
39
|
|
41
40
|
end
|
@@ -44,7 +43,7 @@ describe 'post comment' do
|
|
44
43
|
|
45
44
|
it 'returns 400' do
|
46
45
|
post_comment :path => nil
|
47
|
-
last_response.status.should ==
|
46
|
+
last_response.status.should == 422
|
48
47
|
end
|
49
48
|
|
50
49
|
end
|
@@ -113,16 +112,45 @@ describe 'post comment' do
|
|
113
112
|
|
114
113
|
describe 'existing post' do
|
115
114
|
|
115
|
+
before do
|
116
|
+
Cachai::Post.find_by_path('/another/blog/post.html').should be_a(Cachai::Post)
|
117
|
+
end
|
118
|
+
|
116
119
|
it 'does not create a new post entry' do
|
117
120
|
expect do
|
118
121
|
post_comment
|
119
122
|
end.not_to change(Cachai::Post, :count).by(1)
|
120
123
|
end
|
121
124
|
|
122
|
-
|
123
|
-
|
125
|
+
describe 'if within comments duration' do
|
126
|
+
|
127
|
+
it 'creates a new response for that post' do
|
128
|
+
expect do
|
129
|
+
post_comment
|
130
|
+
end.to change(Cachai::Response, :count).by(1)
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'if after comments duration' do
|
136
|
+
|
137
|
+
before do
|
138
|
+
post = Cachai::Post.find_by_path('/another/blog/post.html')
|
139
|
+
post.update_attribute(:created_at, 100.days.ago)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'returns 400' do
|
124
143
|
post_comment
|
125
|
-
|
144
|
+
last_response.status.should == 400
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'does not create a new response for that post' do
|
148
|
+
expect do
|
149
|
+
post_comment
|
150
|
+
end.not_to change(Cachai::Response, :count)
|
151
|
+
end
|
152
|
+
|
153
|
+
|
126
154
|
end
|
127
155
|
|
128
156
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,6 +19,7 @@ class TestApp < Sinatra::Base
|
|
19
19
|
use Cachai::Middleware, {
|
20
20
|
:domain => 'domain.com',
|
21
21
|
:recipient => 'test@example.com',
|
22
|
+
:duration => 30,
|
22
23
|
:sendgrid => {
|
23
24
|
:api_key => 'test',
|
24
25
|
:api_user => 'test'
|
@@ -29,3 +30,8 @@ class TestApp < Sinatra::Base
|
|
29
30
|
'Hello world'
|
30
31
|
end
|
31
32
|
end
|
33
|
+
|
34
|
+
|
35
|
+
RSpec.configure do |config|
|
36
|
+
config.color = true
|
37
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cachai
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tomás Pollak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|