disqus snippets

How to use the Disqus API with Ruby

Tagged disqus, api, httparty  Languages ruby
require 'httparty'

# See http://disqus.com/api/docs/
module Disqus
  class Base
    include HTTParty
    format :json
    base_uri 'http://disqus.com/api/3.0'
    debug_output $stderr
  end

  class Forum < Base
    # http://disqus.com/api/docs/forums/create/
    def create(options)
      url = "/forums/create.json"
      self.class.post(url, :body => options.merge({ :api_secret => API_SECRET }))
    end

    def posts(id)
      url = "/forums/listPosts.json?api_secret=#{API_SECRET}&forum=#{id}"
      self.class.get(url)
    end

    def threads(id)
      url = "http://disqus.com/api/3.0/threads/list.json?api_secret=#{API_SECRET}&forum=#{id}"
      self.class.get(url)
    end
  end

  class Post < Base
    # http://disqus.com/api/docs/posts/create/
    def create(message)
      url = "/posts/create.json"
      self.class.post(url, :body => message.merge({ :api_secret => API_SECRET }))
    end
  end
end

# Secret key from http://disqus.com/api/applications/
Disqus::API_SECRET = '...'

How to lazy-load Disqus with jQuery

Tagged jquery, disqus  Languages javascript

This JavaScript snippet will load Disqus when a link or button is clicked:

<script type="text/javascript">
  //window.disqus_developer = 1
  window.disqus_shortname = 'xxx'; 
  window.disqus_identifier = 'xxx';

  $('#show-disqus').click(function() {
    $.ajax({
        type: "GET",
        url: "http://" + disqus_shortname + ".disqus.com/embed.js",
        dataType: "script",
        cache: true
    }) 
  });
</script>
<div id="disqus_thread"></div>