Commit e43f1aea

mo khan <mo@mokhan.ca>
2013-07-05 02:19:24
install disqus
1 parent 4a58087
Changed files (4)
app/helpers/application_helper.rb
@@ -16,4 +16,12 @@ module ApplicationHelper
       "#{title} - CakeSide"
     end
   end
+
+  def disqus_auth(user = current_user)
+    data = { id: user.id, username: user.username, email: user.email }.to_json
+    message = Base64.encode64(data).gsub("\n", "")
+    timestamp = Time.now.to_i
+    signature = OpenSSL::HMAC.hexdigest('sha1', ENV['DISQUS_SECRET_KEY'], "#{message} #{timestamp}")
+    "#{message} #{signature} #{timestamp}"
+  end
 end
app/views/creations/show.html.erb
@@ -134,3 +134,5 @@ $(function() {
   </div>
 <% end %>
 </div>
+
+<%= render :partial => "disqus", :locals => { :id => "c-#{@creation.id}" } %>
app/views/shared/_disqus.html.erb
@@ -0,0 +1,21 @@
+<div id="disqus_thread"></div>
+<script type="text/javascript">
+var disqus_shortname = 'cakeside';
+var disqus_shortname = '<%= ENV['DISQUS_SHORTNAME'] %>';
+var disqus_identifier = '<%= id %>';
+(function() {
+  var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
+  dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
+  (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
+})();
+</script>
+<script type="text/javascript" charset="utf-8">
+(function(){
+  var disqus_config = function(){
+    this.page.remote_auth_s3 = '<%= disqus_auth %>';
+    this.page.api_key = '<%= ENV['DISQUS_API_KEY'] %>';
+  };
+})();
+</script>
+<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
+<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
spec/helpers/application_helper_spec.rb
@@ -0,0 +1,22 @@
+require "spec_helper"
+
+describe ApplicationHelper do
+  describe :disqus_auth do
+    let(:user) { OpenStruct.new(id: 1, username: 'blah', email: 'test@fairgoods.com') }
+
+    before :each do
+      data = { id: user.id, username: user.username, email: user.email }.to_json
+      message = Base64.encode64(data).gsub("\n", "")
+      timestamp = Time.now.to_i
+      secret = ENV['DISQUS_SECRET_KEY'] = 'secret'
+      signature = OpenSSL::HMAC.hexdigest('sha1', secret, "#{message} #{timestamp}")
+      @expected = "#{message} #{signature} #{timestamp}"
+    end
+
+    it "should generate a single sign on token" do
+      result = helper.disqus_auth(user)
+      result.should_not be_nil
+      result.should == @expected
+    end
+  end
+end