Class: Disqus::Widget

Inherits:
Object
  • Object
show all
Defined in:
lib/disqus/widget.rb

Overview

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"

Constant Summary collapse

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'
ROOT_PATH + 'popular_threads_widget.js?num_items=%d'
TOP =
ROOT_PATH + 'top_commenters_widget.js?num_items=%d&avatar_size=%d&orientation=%s'

Class Method Summary collapse

Class Method Details

.combo(opts = {}) ⇒ Object

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’.



160
161
162
163
164
165
166
167
168
# File 'lib/disqus/widget.rb', line 160

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 = {}) ⇒ Object

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).



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/disqus/widget.rb', line 59

def comment_counts(opts = {})
  opts = Disqus::defaults.merge(opts)        
  validate_opts!(opts)
  s = <<-WHIMPER
  <script type="text/javascript">
  //<![CDATA[
  (function() {
      var links = document.getElementsByTagName('a');
      var query = '?';
      for(var i = 0; i < links.length; i++) {
        if(links[i].href.indexOf('#disqus_thread') >= 0) {
          query += 'url' + i + '=' + encodeURIComponent(links[i].href) + '&';
        }
      }
      document.write('<' + 'script type="text/javascript" src="#{ROOT_PATH}get_num_replies.js' + query + '"></' + 'script>');
    })();
  //]]>
  </script>
  WHIMPER
  s % opts[:account]
end

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.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/disqus/widget.rb', line 114

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 = {}) ⇒ Object

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.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/disqus/widget.rb', line 136

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 = {}) ⇒ Object

Show the main Disqus thread widget. Options:

  • account: Your Discus account (required).



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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 = {}) ⇒ Object

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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/disqus/widget.rb', line 90

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