cachai 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1077a9878d131805e696c2e1b2105c52dcc6578
4
- data.tar.gz: 572ed93566a73bf464953dc844b7469f95becdfa
3
+ metadata.gz: 3798691a1372fd5f11bbb2a04d67ca5f76c33133
4
+ data.tar.gz: 0ac3cafdb7f2fd8fed54ef8ecddf864f0b261157
5
5
  SHA512:
6
- metadata.gz: bc98cd8492e345a9a3bb1bbd1c983c34b72f746a3c2648977aa60f4ae92684c035e0bbc6e984b5f69b402216c0c9c10afc4e3a124636ef9c07ef7832f1a0cc78
7
- data.tar.gz: 7e8b2dd17bac39e58840db4e91c4574e0b9e325bd8564e26aaf8342fa2f829adba41bdd812b6e7861c46c401b556d33e340943d9df9db97a22fc2c8bb50055b6
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.3'
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 "comments_allowed", :default => 1, :null => false
18
- t.string "path", :null => false
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
- halt(400, "Missing params") if data['protocol'].blank? or data['path'].blank?
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
- halt(400, "No spam allowed") if is_spam?(data, permalink, request)
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
@@ -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 == 400
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 == 400
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
- it 'creates a new response for that post' do
123
- expect do
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
- end.to change(Cachai::Response, :count).by(1)
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.3
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-06-15 00:00:00.000000000 Z
11
+ date: 2016-08-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler