cachai 0.2.7 → 0.2.8

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: 12ab8f63663887eb16edbe178e1289d8c92d8e79
4
- data.tar.gz: 8840bce666685bf34f3eaf4388d81f1a2c97f93c
3
+ metadata.gz: d538da3df5551924790c3a94e17161d09dc20dd4
4
+ data.tar.gz: a9ee1fe10e59f1a52a17d7c63bfb77f6a7d59d99
5
5
  SHA512:
6
- metadata.gz: e0097506b4e9d93e7e12e2733b588b2562ba6f1caf809bbd7e72b7c28ba6566dcb0ecfbb18f93da0cc6a9858a9988c6c7af78ff3d1a182cb8391ce4b97b597da
7
- data.tar.gz: 4f4f39f81b688598f50452d9da0315f395189eb40205144e94f6f44a3792c5efa37f8525566ad1938f109b6c2d69ad8d4630bf904f79ca22a801af01390c88a7
6
+ metadata.gz: 199c5691844b8dc21449da8315c9a2a76911394aaa05462dde71e94572b22f9f7dc6cb771f18ffa2871dab2713ea5925f8081d853b65b8a08d9136201b2c5de2
7
+ data.tar.gz: 79d440f2f003ddb363dd7ffb8816e8e3f8c73292caf755677c0ff82f23bda7922dbc97b46c19610e3499681ebba8a126962fbc5d490e4d2fc7d1e3cbc7a9a870
@@ -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'
7
+ spec.version = '0.2.8'
8
8
  spec.authors = ["Tomás Pollak"]
9
9
  spec.email = ["tomas@forkhq.com"]
10
10
  spec.description = %q{Middleware for embedabble comments.}
@@ -106,9 +106,12 @@ module Cachai
106
106
  }
107
107
 
108
108
  response = Response.create!(attrs.merge(:post_id => post.id))
109
-
110
109
  Cachai.clear_cache(data['path'])
111
- notify_new_response(response, data['path']) if @recipient
110
+
111
+ if response.approved?
112
+ notify_new_response_to_admin(response, data['path']) if @recipient
113
+ # notify_new_response_to_parent(response, data['path']) if response.parent
114
+ end
112
115
 
113
116
  headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
114
117
  json({ :status => 'ok', :comment => response })
@@ -181,15 +184,25 @@ module Cachai
181
184
  false
182
185
  end
183
186
 
184
- def notify_new_response(response, path)
187
+ def notify_new_response_to_admin(response, path)
188
+ subject = "Nuevo comentario de #{response.author_name} at #{path}"
189
+ send_email(content: response.content, to: @recipient, path: path, subject: subject)
190
+ end
191
+
192
+ def notify_new_response_to_parent(response, path)
193
+ subject = "Respuesta de #{response.author_name} a tu comentario en #{path}"
194
+ send_email(content: response.content, to: response.parent.author_email, path: path, subject: subject)
195
+ end
196
+
197
+ def send_email(data)
185
198
  RestClient.post "https://api:#{@mailgun_api_key}"\
186
199
  "@api.mailgun.net/v3/#{@mailgun_domain}/messages",
187
- :from => 'comments@' + Cachai.domain,
188
- :to => @recipient,
189
- :subject => "New comment from #{response.author_name} at #{path}",
190
- :text => "#{response.content}\n\n--\nhttp://#{Cachai.domain}#{path}"
191
- rescue => e
192
- puts "MAIL ERROR: #{e.message}"
200
+ :from => data[:from] || 'comments@' + Cachai.domain,
201
+ :to => data[:to],
202
+ :subject => data[:subject],
203
+ :text => "#{data[:content]}\n\n--\nhttp://#{Cachai.domain}#{data[:path]}"
204
+ # rescue => e
205
+ # puts "MAIL ERROR: #{e.message}"
193
206
  end
194
207
 
195
208
  end
@@ -133,6 +133,10 @@ module Cachai
133
133
  scope :top_level, lambda { where(:parent_id => 0) }
134
134
  scope :nested, lambda { where("parent_id != 0") }
135
135
 
136
+ def parent
137
+ @parent ||= Response.find(parent_id) rescue nil
138
+ end
139
+
136
140
  def as_json(options = {})
137
141
  {
138
142
  :id => id,
@@ -1,4 +1,5 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'rest-client'
2
3
 
3
4
  describe 'post comment' do
4
5
  include Rack::Test::Methods
@@ -90,6 +91,10 @@ describe 'post comment' do
90
91
 
91
92
  describe 'valid data' do
92
93
 
94
+ before do
95
+ allow(RestClient).to receive(:post).and_return(true)
96
+ end
97
+
93
98
  describe 'unexisting post' do
94
99
 
95
100
  before do
@@ -120,6 +125,16 @@ describe 'post comment' do
120
125
  expect(Cachai::Response.last.approved).to eql(1)
121
126
  end
122
127
 
128
+ it 'sends emails' do
129
+ expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", {
130
+ :from =>"comments@domain.com",
131
+ :to =>"test@example.com",
132
+ :subject =>"Nuevo comentario de Some author at /another/blog/post.html",
133
+ :text =>"New comment\n\n--\nhttp://domain.com/another/blog/post.html"
134
+ }).and_return(true)
135
+ post_comment
136
+ end
137
+
123
138
  end
124
139
 
125
140
  describe 'if blocked' do
@@ -135,6 +150,11 @@ describe 'post comment' do
135
150
  expect(Cachai::Response.last.approved).to eql(0)
136
151
  end
137
152
 
153
+ it 'does not send emails' do
154
+ expect(RestClient).not_to receive(:post)
155
+ post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'})
156
+ end
157
+
138
158
  end
139
159
 
140
160
  end
@@ -166,6 +186,50 @@ describe 'post comment' do
166
186
  expect(Cachai::Response.last.approved).to eql(1)
167
187
  end
168
188
 
189
+ it 'sends admin emails' do
190
+ expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", {
191
+ :from =>"comments@domain.com",
192
+ :to =>"test@example.com",
193
+ :subject =>"Nuevo comentario de Some author at /another/blog/post.html",
194
+ :text =>"New comment\n\n--\nhttp://domain.com/another/blog/post.html"
195
+ }).and_return(true)
196
+
197
+ post_comment
198
+ end
199
+
200
+ describe 'if comment is response to parent' do
201
+
202
+ before do
203
+ post = Cachai::Post.find_by_path('/another/blog/post.html')
204
+ @parent = post.responses.create!({
205
+ :content => 'Content',
206
+ :author_name => 'Some guy',
207
+ :author_email => 'some@email.com',
208
+ :author_ip => '22.33.44.55'
209
+ })
210
+ end
211
+
212
+ it 'sends email both to admin and to parent comment guy' do
213
+
214
+ expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", {
215
+ :from => "comments@domain.com",
216
+ :to => "test@example.com",
217
+ :subject => "Nuevo comentario de Some author at /another/blog/post.html",
218
+ :text => "New comment\n\n--\nhttp://domain.com/another/blog/post.html"
219
+ }).and_return(true)
220
+
221
+ expect(RestClient).to receive(:post).with("https://api:key-aoidjoaijdoaijdoaijsd@api.mailgun.net/v3/foobartest1234.mailgun.org/messages", {
222
+ :from => "comments@domain.com",
223
+ :to => "some@email.com",
224
+ :subject => "Respuesta de Some author a tu comentario en /another/blog/post.html",
225
+ :text => "New comment\n\n--\nhttp://domain.com/another/blog/post.html"
226
+ }).and_return(true)
227
+
228
+ post_comment(parent_id: @parent.id)
229
+ end
230
+
231
+ end
232
+
169
233
  end
170
234
 
171
235
  describe 'if blocked' do
@@ -181,6 +245,11 @@ describe 'post comment' do
181
245
  expect(Cachai::Response.last.approved).to eql(0)
182
246
  end
183
247
 
248
+ it 'does not send emails' do
249
+ expect(RestClient).not_to receive(:post)
250
+ post_comment({}, {'REMOTE_ADDR' => '12.12.12.12'})
251
+ end
252
+
184
253
  end
185
254
 
186
255
  end
@@ -20,9 +20,9 @@ class TestApp < Sinatra::Base
20
20
  :domain => 'domain.com',
21
21
  :recipient => 'test@example.com',
22
22
  :duration => 30,
23
- :sendgrid => {
24
- :api_key => 'test',
25
- :api_user => 'test'
23
+ :mailgun => {
24
+ domain: 'foobartest1234.mailgun.org',
25
+ api_key: 'key-aoidjoaijdoaijdoaijsd'
26
26
  },
27
27
  :blocked_ips => [ "12.12.12.12" ]
28
28
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cachai
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomás Pollak
@@ -198,4 +198,3 @@ test_files:
198
198
  - spec/post_comments_spec.rb
199
199
  - spec/spec_helper.rb
200
200
  - test/app_test.rb
201
- has_rdoc: