Class Disqus::Widget

  1. lib/disqus/widget.rb
Parent: Object

Disqus Widget generator.

All of the methods accept various options, and “account” is required for all of them. You can avoid having to pass in the account each time by setting it in the defaults like this:

Disqus::defaults[:account] = "my_account"

Constants

VALID_COLORS = ['blue', 'grey', 'green', 'red', 'orange']
VALID_NUM_ITEMS = 5..20
VALID_DEFAULT_TABS = ['people', 'recent', 'popular']
VALID_AVATAR_SIZES = [24, 32, 48, 92, 128]
VALID_ORIENTATIONS = ['horizontal', 'vertical']
ROOT_PATH = 'http://disqus.com/forums/%s/'
THREAD = ROOT_PATH + 'embed.js'
COMBO = ROOT_PATH + 'combination_widget.js?num_items=%d&color=%s&default_tab=%s'
RECENT = ROOT_PATH + 'recent_comments_widget.js?num_items=%d&avatar_size=%d'
POPULAR = ROOT_PATH + 'popular_threads_widget.js?num_items=%d'
TOP = ROOT_PATH + 'top_commenters_widget.js?num_items=%d&avatar_size=%d&orientation=%s'

Public class methods

combo (opts = {})

Show the Disqus combo widget. This is a three-tabbed box with links popular threads, top posters, and recent threads. Options:

  • :account: Your Discus account (required).
  • :num_items: How many items to show.
  • :hide_mods: Don’t show moderators.
  • :default_tab: Should be ‘people’, ‘recent’, or ‘popular’.
[show source]
# File lib/disqus/widget.rb, line 161
      def combo(opts = {})
        opts = Disqus::defaults.merge(opts)
        validate_opts!(opts)
        s = '<script type="text/javascript" src="'
        s << COMBO
        s << '&hide_mods=1' if opts[:hide_mods]
        s << '"></script>' 
        s % [opts[:account], opts[:num_items], opts[:color], opts[:default_tab]]
      end
comment_counts (opts = {})

Loads Javascript to show the number of comments for the page.

The Javascript sets the inner html to the comment count for any links on the page that have the anchor “disqus_thread“. For example, “View Comments” below would be replaced by “1 comment” or “23 comments” etc.

<a href="http://my.website/article-permalink#disqus_thread">View Comments</a>
<a href="http://my.website/different-permalink#disqus_thread">View Comments</a>

Options:

  • account: Your Discus account (required).
[show source]
# File lib/disqus/widget.rb, line 59
      def comment_counts(opts = {})
        opts = Disqus::defaults.merge(opts)        
        validate_opts!(opts)
        s = "<script type=\"text/javascript\">\n//<[CDATA[\n(function() {\nvar links = document.getElementsByTagName('a');\nvar query = '?';\nfor(var i = 0; i < links.length; i++) {\nif(links[i].href.indexOf('#disqus_thread') >= 0) {\nquery += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';\n}\n}\ndocument.write('<' + 'script type=\"text/javascript\" src=\"\#{ROOT_PATH}get_num_replies.js' + query + '\"></' + 'script>');\n})();\n//]]>\n</script>\n"
        s % opts[:account]
      end
popular_threads (opts = {})

Show the popular threads Disqus widget. Options:

  • account: Your Discus account (required).
  • header: HTML snipper with header (default h2) tag and text.
  • num_items:: How many items to show.
  • hide_mods: Don’t show moderators.
[show source]
# File lib/disqus/widget.rb, line 115
      def popular_threads(opts = {})
        opts = Disqus::defaults.merge(opts)
        opts[:header] ||= '<h2 class="dsq-widget-title">Popular Threads</h2>'
        validate_opts!(opts)
        s = '<div id="dsq-popthreads" class="dsq-widget">'
        s << opts[:header]
        s << '<script type="text/javascript" src="'
        s << POPULAR
        s << '&hide_mods=1' if opts[:hide_mods]
        s << '"></script>'
        s << '</div>'
        s << '<a href="http://disqus.com">Powered by Disqus</a>' if opts[:show_powered_by]
        s % [opts[:account], opts[:num_items]]
      end
recent_comments (opts = {})

Show the recent comments Disqus widget. Options:

  • account: Your Discus account (required).
  • header: HTML snipper with header (default h2) tag and text.
  • num_items:: How many items to show.
  • hide_avatars: Don’t show avatars.
  • avatar_size: Avatar size.
[show source]
# File lib/disqus/widget.rb, line 137
      def recent_comments(opts = {})
        opts = Disqus::defaults.merge(opts)
        opts[:header] ||= '<h2 class="dsq-widget-title">Recent Comments</h2>'
        validate_opts!(opts)
        s = '<div id="dsq-recentcomments" class="dsq-widget">'
        s << opts[:header]
        s << '<script type="text/javascript" src="'
        s << RECENT 
        s << '&hide_avatars=1' if opts[:hide_avatars]
        s << '"></script>'
        s << '</div>'
        if opts[:show_powered_by]
          s << '<a href="http://disqus.com">Powered by Disqus</a>'
        end
        s % [opts[:account], opts[:num_items], opts[:avatar_size]]
      end
thread (opts = {})

Show the main Disqus thread widget. Options:

  • account: Your Discus account (required).
[show source]
# File lib/disqus/widget.rb, line 29
      def thread(opts = {})
        opts = Disqus::defaults.merge(opts)
        opts[:view_thread_text] ||= "View the discussion thread"
        validate_opts!(opts)
        s = ''
        if opts[:developer]
          s << '<script type="text/javascript">var disqus_developer = 1;</script>'
        end
        s << '<div id="disqus_thread"></div>'
        s << '<script type="text/javascript" src="' + THREAD + '"></script>'
        s << '<noscript><a href="http://%s.disqus.com/?url=ref">'
        s << opts[:view_thread_text]
        s << '</a></noscript>'
        if opts[:show_powered_by]
          s << '<a href="http://disqus.com" class="dsq-brlink">blog comments '
          s << 'powered by <span class="logo-disqus">Disqus</span></a>'
        end
        s % [opts[:account], opts[:account]]
      end
top_commenters (opts = {})

Show the top commenters Disqus thread widget. Options:

  • account: Your Discus account (required).
  • header: HTML snipper with header (default h2) tag and text.
  • show_powered_by: Show or hide the powered by Disqus text.
  • num_items:: How many items to show.
  • hide_mods: Don’t show moderators.
  • hide_avatars: Don’t show avatars.
  • avatar_size: Avatar size.
[show source]
# File lib/disqus/widget.rb, line 91
      def top_commenters(opts = {})
        opts = Disqus::defaults.merge(opts)
        opts[:header] ||= '<h2 class="dsq-widget-title">Top Commenters</h2>'
        validate_opts!(opts)        
        s = '<div id="dsq-topcommenters" class="dsq-widget">'
        s << opts[:header]
        s << '<script type="text/javascript" src="'
        s << TOP
        s << '&hide_avatars=1' if opts[:hide_avatars]
        s << '&hide_mods=1' if opts[:hide_mods]
        s << '"></script>'
        s << '</div>'
        if opts[:show_powered_by]
          s << '<a href="http://disqus.com">Powered by Disqus</a>'
        end
        s % [opts[:account], opts[:num_items], opts[:avatar_size], opts[:orientation]]
      end