Skip to content
This repository was archived by the owner on Dec 5, 2017. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion disqus.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'lib/disqus/version'

spec = Gem::Specification.new do |s|

s.name = "disqus"
Expand All @@ -8,7 +10,6 @@ spec = Gem::Specification.new do |s|
s.summary = "Integrates Disqus commenting system into your Ruby-powered site."
s.description = 'Integrates Disqus into your Ruby-powered site. Works with any Ruby website, and has view helpers for Rails and Merb.'
s.homepage = 'http://github.com/norman/disqus'
s.has_rdoc = true
s.platform = Gem::Platform::RUBY

s.files = Dir["lib/**/*.rb", "lib/**/*.rake", "*.rdoc", "LICENSE", "Rakefile", "test/**/*.*"]
Expand Down
4 changes: 2 additions & 2 deletions lib/disqus.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
%w[api author forum post thread version view_helpers widget].each do |file|
%w[api author forum post thread view_helpers widget].each do |file|
require File.join(File.dirname(__FILE__), "disqus", file)
end

Expand Down Expand Up @@ -49,4 +49,4 @@ def self.defaults
@defaults
end

end
end
4 changes: 2 additions & 2 deletions lib/disqus/view_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Shortcuts to access the widgets as simple functions as opposed to using
# their full qualified names.
%w[combo comment_counts popular_threads recent_comments thread top_commenters].each do |method|
%w[combo comment_counts popular_threads recent_comments thread top_commenters sso_login sso_logout].each do |method|
eval(<<-EOM)
def disqus_#{method}(options = {})
Disqus::Widget.#{method}(options)
end
EOM
end
end
51 changes: 51 additions & 0 deletions lib/disqus/widget.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'base64'
require 'openssl'

module Disqus

# Disqus Widget generator.
Expand All @@ -21,6 +24,9 @@ class Widget
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'

DIGEST = OpenSSL::Digest::Digest.new('sha1')

class << self

# Show the main Disqus thread widget.
Expand All @@ -34,6 +40,7 @@ def thread(opts = {})
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">'
Expand Down Expand Up @@ -166,9 +173,53 @@ def combo(opts = {})
s << '"></script>'
s % [opts[:account], opts[:num_items], opts[:color], opts[:default_tab]]
end


# Single Sign-On | Login
# Options:
# * <tt>:public_key:</tt>
# * <tt>:secret_key:</tt>
# * <tt>:user_id:</tt> any unique user ID number associated with that account within your user database. This will be used to generate a unique username to reference in the Disqus system.
# * <tt>:username:</tt> The displayed name for that account
# * <tt>:email:</tt> The registered email address for that account
# * <tt>:avatar:</tt> A link to that user's avatar (optional)
# * <tt>:url:</tt> A link to the user's website (optional)
def sso_login(opts = {})
opts = Disqus::defaults.merge(opts)
sso :login, opts
end

# Single Sign-On | Logout
# Options:
# * <tt>:public_key:</tt>
# * <tt>:secret_key:</tt>
def sso_logout(opts = {})
opts = Disqus::defaults.merge(opts)
sso :logout, opts
end

private

def sso action, opts
message = (action == :login ? {'id' => opts[:user_id], 'username' => opts[:username], 'email' => opts[:email]} : {})
message = Base64.encode64(message.to_json).gsub("\n", "")
timestamp = Time.now.to_i

signature = OpenSSL::HMAC.hexdigest(DIGEST, opts[:secret_key], "#{message} #{timestamp}")

s = '<script type="text/javascript">'
s << 'var disqus_config = function() {'

if opts[:logout_link]
s << 'this.sso = {'
s << 'logout: "%s"};' % opts[:logout_link]
end

s << 'this.page.remote_auth_s3 = "%s %s %s";' % [message, signature, timestamp]
s << 'this.page.api_key = "%s";}' % opts[:public_key]
s << '</script>'
end

def validate_opts!(opts)
raise ArgumentError.new("You must specify an :account") if !opts[:account]
raise ArgumentError.new("Invalid color") if opts[:color] && !VALID_COLORS.include?(opts[:color])
Expand Down
10 changes: 9 additions & 1 deletion test/widget_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ def test_top_commenters
assert disqus_top_commenters
end

def test_sso_login
assert disqus_sso_login(:public_key => 'app_public', :secret_key => 'app_secret', :user_id => 1, :username => 'Vasya Pupkin', :email => 'vasya@pupkin.ru')
end

def test_sso_logout
assert disqus_sso_logout(:public_key => 'app_public', :secret_key => 'app_secret')
end

def test_invalid_default_tab
assert_raises ArgumentError do
disqus_combo(:default_tab => "test")
Expand All @@ -52,4 +60,4 @@ def test_invalid_avatar_size
end
end

end
end