cachai 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +105 -20
  3. data/cachai.gemspec +1 -1
  4. data/lib/cachai.rb +10 -39
  5. data/lib/models.rb +36 -0
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 60bb4315ef242ac00c5f829339a5e69a36e1691d
4
- data.tar.gz: fd1a0bb8dabbc64bf49f80cd0ddd85727a22abd0
3
+ metadata.gz: e22e87617ddb5c31c0323caa1669a6c6f113e22c
4
+ data.tar.gz: 4600b981f67a035759bf840cdb087e6d47250457
5
5
  SHA512:
6
- metadata.gz: cb9a8a4acb6a5552ec234edca4cc6bb56dc132cdbe1a811677289ca15a0d601b16172392f38b7c3be83ee304e1713644485075b3598d0aa6a5a67ea2344f8eaa
7
- data.tar.gz: 71e1e058a543e67337eaa42681ed690bc50682de0530d5c21fcc09be5f2d0e34ae7abd160be864f839c7199885b0004af4e559b24258e1b3b0c268e461e18d1a
6
+ metadata.gz: f172f5bd47ce928fe0b021c14a37e36ae2f4b93cbe726dbd7f24fa55f60922278574ac0345bed005a7ce27a6541eca6175df45cd71b326c485448c17a8fff1cc
7
+ data.tar.gz: 384a50e683f10c8bcc632de7460789562e046dfcbc2fa2697a0ecb3b96bb232ac29f14f3ef761cbba449c463d1d0868b03893e0c4b85708e7f3045341f17442a
data/README.md CHANGED
@@ -1,26 +1,111 @@
1
- # Commentary
1
+ # Cachai
2
2
 
3
- Add comments to your blog.
3
+ A clean way to add support for comments and pingbacks in your Rack app. Si pos weon.
4
4
 
5
- ## Setup
6
- * Edit `config/production.yml` to add your site. Specify a name and the domain where the site will be hosted. Example configuration will be like this:
5
+ ## Setting up
7
6
 
8
- ```
9
- name: Blog
10
- domain: blog.sdqali.in
11
- ```
7
+ First you need to create a config/database.yml file, as you'd have on a regular Rails app, or if using Sinatra::ActiveRecord.
12
8
 
13
- * Run `RACK_ENV=production setup.rb`
14
- * Start the server with `RACK_ENV=production app.rb`
15
- * Add the following to your HTML pages or templates. `selector` is the CSS selector for the DOM element where comments will be rendered.
9
+ # database.yml
10
+ development:
11
+ adapter: sqlite3
12
+ database: db/development.sqlite3
13
+ pool: 16
16
14
 
17
- ```html
18
- <script type="text/javascript" src="<server>/jquery-1.10.2.min.js"></script>
19
- <script type="text/javascript" src="<server>//commentary.js"></script>
20
- <script type="text/javascript">
21
- $(document).ready(function() {
22
- Commentary.initialize("<server>", "<selector>");
23
- });
24
- </script>
15
+ Now insert the middleware before the app, either in your rackup file or in the app itself. We'll go with method one.
25
16
 
26
- ```
17
+ # rackup.ru
18
+ require 'sinatra'
19
+ require 'cachai'
20
+
21
+ map '/' do
22
+ use Cachai::Middleware, { domain: 'yourdomain.com' }
23
+ run Sinatra::Application
24
+ end
25
+
26
+ Now, the next time the app is loaded, Cachai will set up the necessary tables in the database provided. Basically a `posts` table and a `responses` one.
27
+
28
+ ## Posting new comments
29
+
30
+ Now, to send our first comment to Cachai, let's insert a form within your blog post view.
31
+
32
+ # views/blog/post.erb (or something like it)
33
+
34
+ ...
35
+
36
+ <form action="/comments" method="post" id="comment-form">
37
+ <input type="hidden" name="url" value="http://yourdomain.com/path/to/your/blog/post" />
38
+ <input type="text" name="author_name" />
39
+ <input type="email" name="author_email" />
40
+ <input type="text" name="author_url" />
41
+ <textarea name="content"></textarea>
42
+ </form>
43
+
44
+ Now load your app and try submitting a comment. It should work.
45
+
46
+ ## Posting comment using AJAX
47
+
48
+ Using Javascript, you'd do something like this:
49
+
50
+ // app.js
51
+
52
+ $(function() {
53
+
54
+ $('#comment-form').on('submit', function(e) {
55
+ e.preventDefault();
56
+
57
+ var data = {
58
+ author_name : this.author_name.value,
59
+ author_email : this.author_email.value,
60
+ author_url : this.author_url.value,
61
+ content : this.content.value,
62
+ protocol : window.location.protocol,
63
+ domain : 'yourdomain.com',
64
+ path : encodeURI(window.location.pathname)
65
+ }
66
+
67
+ $.ajax({
68
+ type : 'post',
69
+ data : JSON.stringify(data),
70
+ url : '/comments.json',
71
+ success : function() { alert('Thanks!') },
72
+ error : function() { alert('Oh rats. Try again please.') },
73
+ })
74
+ })
75
+
76
+ })
77
+
78
+ ## Reading comments via AJAX
79
+
80
+ You get them using GET /comments.json.
81
+
82
+ // app.js
83
+
84
+ var domain = 'yourdomain.com',
85
+ path = encodeURI(window.location.pathname),
86
+ url = '/comments.json?callback=?&domain=' + domain + '&path=' + path;
87
+
88
+ $.ajax({
89
+ dataType : 'json',
90
+ url : url,
91
+ success : function(list) { /* render comments */ }),
92
+ error : function(err) { alert('Damn!') }
93
+ });
94
+ }
95
+
96
+ Or if you wish to insert them in the view itself.
97
+
98
+ # views/blog/post.erb
99
+
100
+ <ul id="comments">
101
+ <% Cachai.get_comments_for(@post.url).each do |comment| %>
102
+ <li>
103
+ <strong><%= comment.author_name %></strong>
104
+ <strong><%= simple_format(comment.content) %></strong>
105
+ </li>
106
+ <% end %>
107
+ </ul>
108
+
109
+ # Small print.
110
+
111
+ (c) 2016 Tomas Pollak. MIT Licensed.
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.1.4'
7
+ spec.version = '0.2.0'
8
8
  spec.authors = ["Tomás Pollak"]
9
9
  spec.email = ["tomas@forkhq.com"]
10
10
  spec.description = %q{Middleware for embedabble comments.}
data/lib/cachai.rb CHANGED
@@ -56,7 +56,7 @@ module Cachai
56
56
  check_domain!(params[:domain])
57
57
 
58
58
  # puts "Comments for: #{params[:domain]}#{params[:path]}"
59
- json_list = get_comments(params[:path], params[:nocache])
59
+ json_list = Cachai.get_comments_for(params[:path], params[:nocache])
60
60
 
61
61
  if params[:callback]
62
62
  content_type 'application/javascript'
@@ -68,9 +68,16 @@ module Cachai
68
68
 
69
69
  post '/comments.?:format?' do
70
70
  begin
71
- data = JSON.parse(request.body.read)
72
- check_domain!(data['domain'])
71
+ data = params[:format] == 'json' ? JSON.parse(request.body.read) : params
72
+
73
+ if data[:url] # got single URL with protocol://domain/path
74
+ url = URI.parse(data)
75
+ data['domain'] = url.host
76
+ data['protocol'] = url.schema
77
+ data['path'] = url.path
78
+ end
73
79
 
80
+ check_domain!(data['domain'])
74
81
  halt(400, "Missing params") if data['protocol'].blank? or data['path'].blank?
75
82
 
76
83
  headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
@@ -132,42 +139,6 @@ module Cachai
132
139
  return obj.is_a?(String) ? obj : obj.to_json
133
140
  end
134
141
 
135
- def get_comments(path, nocache = false)
136
- key = Cachai.key_for(path)
137
-
138
- unless !nocache && json_list = Cachai.cache.get(key)
139
- puts "Not cached. Getting from DB: #{path}"
140
-
141
- if post = Post.find_by_path(path)
142
- json_list = get_and_sort_comments_for(post).to_json
143
- else
144
- json_list = '[]'
145
- end
146
-
147
- Cachai.cache.set(key, json_list)
148
- Cachai.cache.expire(key, CACHE_MINUTES)
149
- end
150
-
151
- json_list
152
- end
153
-
154
- def get_and_sort_comments_for(post)
155
- result = []
156
- top_level = post.responses.comment.approved.top_level
157
- nested = post.responses.comment.approved.nested
158
-
159
- top_level.each_with_index do |comment, i|
160
- obj = comment.as_json
161
- children = nested.select do |nested|
162
- nested.parent_id == comment.id
163
- end
164
- obj.merge!(:children => children) if children.any?
165
- result.push(obj)
166
- end
167
-
168
- result
169
- end
170
-
171
142
  def is_spam?(data, link, request)
172
143
  return false unless @akismet
173
144
  # return true if blacklisted?(name, email, content)
data/lib/models.rb CHANGED
@@ -52,6 +52,42 @@ module Cachai
52
52
  "comments:#{@domain}:#{path}"
53
53
  end
54
54
 
55
+ def self.get_comments_for(path, nocache = false)
56
+ key = key_for(path)
57
+
58
+ unless !nocache && json_list = cache.get(key)
59
+ # puts "Not cached. Getting from DB: #{path}"
60
+
61
+ if post = Post.find_by_path(path)
62
+ json_list = get_and_sort_comments_for(post).to_json
63
+ else
64
+ json_list = '[]'
65
+ end
66
+
67
+ cache.set(key, json_list)
68
+ cache.expire(key, CACHE_MINUTES)
69
+ end
70
+
71
+ json_list
72
+ end
73
+
74
+ def self.get_and_sort_comments_for(post)
75
+ result = []
76
+ top_level = post.responses.comment.approved.top_level
77
+ nested = post.responses.comment.approved.nested
78
+
79
+ top_level.each_with_index do |comment, i|
80
+ obj = comment.as_json
81
+ children = nested.select do |nested|
82
+ nested.parent_id == comment.id
83
+ end
84
+ obj.merge!(:children => children) if children.any?
85
+ result.push(obj)
86
+ end
87
+
88
+ result
89
+ end
90
+
55
91
  class Post < ActiveRecord::Base
56
92
  has_many :responses
57
93
 
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.1.4
4
+ version: 0.2.0
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: 2015-09-25 00:00:00.000000000 Z
11
+ date: 2016-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler