Skip to content
Browse files

added some rdocs, cleaned up Forum API

  • Loading branch information...
1 parent ba50275 commit e9265bc1ae6859f4d165252946a5486045482529 Matthew Van Horn committed Dec 2, 2008
Showing with 194 additions and 49 deletions.
  1. +2 −1 README.textile
  2. +105 −9 lib/disqus/api.rb
  3. +59 −29 lib/disqus/forum.rb
  4. +24 −8 test/forum_test.rb
  5. +4 −2 test/test_helper.rb
View
3 README.textile
@@ -6,7 +6,7 @@ and has view helpers for Rails and Merb.
h2. Get it
-h3. Stable release:
+h3. Stable release, without API code:
<code>
gem install disqus
@@ -25,6 +25,7 @@ h3. Configure it:
<pre>
<code>
Disqus::defaults[:account] = "my_account"
+ Disqus::defaults[:api_key] = "my_api_key_from_disqus"
</code>
</pre>
View
114 lib/disqus/api.rb
@@ -5,14 +5,47 @@
require 'uri'
module Disqus
-
+ # The Api class implements the Disqus API directly.
+ # It is not really intended to be used directly, but rather to use the domain objects of
+ # Forum, Thread, Post, Author and AnonymousAuthor
+ # Each method in the Api class takes as a single argument a hash of options, and returns a Hash with 3 keys
+ # * 'succeeded' - contains true or false indicating whether the API call succeeded
+ # * 'code' - if the API call did not succeed, this will contain an error code.
+ # * 'message' - contains the object being returned on success, or an error message on failure.
+ #
+ # API Keys
+ # There are two different kinds of API keys.
+ #
+ # First is the User Key. Every Disqus account has a User Key; it is used to perform actions associated with that account.
+ # This can be passed in as an option, or configured as follows
+ # Disqus::defaults[:api_key] = "the_user_api_key"
+ #
+ # The second kind of key is the Forum Key. Every Disqus forum has a Forum Key.
+ # It can be shared among trusted moderators of a forum, and is used to perform actions associated with that forum.
+ # The creator of a forum can get the forum's key through the API.
class Api
ROOT = 'http://disqus.com/api'
class << self
-
-
+
+ # Creates a new post on the thread. Does not check against spam filters or ban list.
+ # This is intended to allow automated importing of comments.
+ #
+ # Returns a Hash containing a representation of the post just created
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:thread_id</tt> - the thread to post to
+ # * <tt>:message</tt> - the content of the post
+ # * <tt>:author_name</tt> - the post creator's name
+ # * <tt>:author_email</tt> - the post creator's email address
+ #
+ # Optional:
+ # * <tt>:parent_post</tt> - the id of the parent post
+ # * <tt>:created_at</tt> - the UTC date this post was created, in the format <tt>%Y-%m-%dT%H:%M</tt> (the current time will be used by default)
+ # * <tt>:author_url</tt> - the author's homepage
+ # * <tt>:ip_address</tt> - the author's IP address
def create_post(opts = {})
opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(post('create_post', :forum_api_key => opts[:forum_api_key],
@@ -26,45 +59,108 @@ def create_post(opts = {})
:ip_address => opts[:ip_address]))
end
+ # Returns an array of hashes representing all forums the user owns. The user is determined by the API key.
+ #
+ # Options:
+ # * <tt>:api_key</tt> - The User's API key (defaults to Disqus::defaults[:api_key])
def get_forum_list(opts = {})
opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(get('get_forum_list', :user_api_key => opts[:api_key]))
end
-
+
+ # Returns A string which is the Forum Key for the given forum.
+ #
+ # Required options hash elements:
+ # * <tt>:forum_id</tt> - the unique id of the forum
+ # Optional:
+ # * <tt>:api_key</tt> - The User's API key (defaults to Disqus::defaults[:api_key])
def get_forum_api_key(opts = {})
opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(get('get_forum_api_key', :user_api_key => opts[:api_key], :forum_id => opts[:forum_id]))
end
+ # Returns: An array of hashes representing all threads belonging to the given forum.
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:forum_id</tt> - the unique id of the forum
def get_thread_list(opts = {})
- opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(get('get_thread_list', :forum_id => opts[:forum_id], :forum_api_key => opts[:forum_api_key]))
end
+ # Returns: A hash having thread_ids as keys and 2-element arrays as values.
+ #
+ # The first array element is the number of visible comments on on the thread; this would be useful for showing users of the site (e.g., "5 Comments").
+ #
+ # The second array element is the total number of comments on the thread.
+ #
+ # These numbers are different because some forums require moderator approval, some messages are flagged as spam, etc.
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:thread_ids</tt> - an array of thread IDs belonging to the given forum.
def get_num_posts(opts = {})
opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(get('get_num_posts', :thread_ids => opts[:thread_ids].join(","), :forum_api_key => opts[:forum_api_key]))
end
-
+
+ # Returns: A hash representing a thread if one was found, otherwise null.
+ #
+ # Only finds threads associated with the given forum.
+ #
+ # Note that there is no one-to-one mapping between threads and URLs:
+ #
+ # a thread will only have an associated URL if it was automatically created by Disqus javascript embedded on that page.
+ # Therefore, we recommend using thread_by_identifier whenever possible, and this method is provided mainly for
+ # handling comments from before your forum was using the API.
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:url</tt> - the URL to check for an associated thread
def get_thread_by_url(opts = {})
- opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(get('get_thread_by_url', :url => opts[:url], :forum_api_key => opts[:forum_api_key]))
end
+ # Returns: An array of hashes representing representing all posts belonging to the given forum.
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:thread_id</tt> - the ID of a thread belonging to the given forum
def get_thread_posts(opts = {})
opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(get('get_thread_posts', :thread_id => opts[:thread_id], :forum_api_key => opts[:forum_api_key]))
end
+ # Create or retrieve a thread by an arbitrary identifying string of your choice. For example, you could use your local database's ID for the thread. This method allows you to decouple thread identifiers from the URLs on which they might be appear. (Disqus would normally use a thread's URL to identify it, which is problematic when URLs do not uniquely identify a resource.) If no thread yet exists for the given identifier (paired with the forum), one will be created.
+ #
+ # Returns: A hash with two keys:
+ # * "thread", which is a hash representing the thread corresponding to the identifier; and
+ # * "created", which indicates whether the thread was created as a result of this method call. If created, it will have the specified title.
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:title</tt> - the title of the thread to possibly be created
+ # * <tt>:identifier</tt> - a string of your choosing
def thread_by_identifier(opts = {})
- opts[:api_key] ||= Disqus::defaults[:api_key]
JSON.parse(post('thread_by_identifier', :forum_api_key => opts[:forum_api_key],
:identifier => opts[:identifier],
:title => opts[:title] ))
end
+ # Sets the provided values on the thread object.
+ #
+ # Returns: An empty success message
+ #
+ # Required options hash elements:
+ # * <tt>:forum_api_key</tt> - the API key for the forum
+ # * <tt>:thread_id</tt> - the ID of a thread belonging to the given forum
+ # Optional:
+ # * <tt>:title</tt> - the title of the thread
+ # * <tt>:slug</tt> - the per-forum-unique string used for identifying this thread in disqus.com URLs relating to this thread. Composed of underscore-separated alphanumeric strings.
+ # * <tt>:url</tt> - the URL this thread is on, if known.
+ # * <tt>:allow_comment</tt> - whether this thread is open to new comments
def update_thread(opts = {})
- opts[:api_key] ||= Disqus::defaults[:api_key]
+ raise opts.inspect
JSON.parse(post('update_thread', :forum_api_key => opts[:forum_api_key],
:thread_id => opts[:thread_id],
:title => opts[:title],
View
88 lib/disqus/forum.rb
@@ -3,10 +3,10 @@ module Disqus
class Forum
attr_reader :id, :shortname, :name, :created_at, :threads
- def initialize(id, shortname, name, created_at)
+ def initialize(id, shortname, name, created_at, include_threads = false)
@id, @shortname, @name, @created_at = id.to_i, shortname, name, Time.parse(created_at.to_s)
@key = nil
- @threads = []
+ @threads = include_threads ? load_threads : []
end
def ==(other_forum)
@@ -16,7 +16,9 @@ def ==(other_forum)
key == other_forum.key
end
- def self.list(opts = {})
+ # returns an array of Forum objects belonging to the user indicated by the API key
+ def self.list(user_api_key = nil)
+ opts = user_api_key ? {:api_key => user_api_key} : {}
response = Disqus::Api::get_forum_list(opts)
if response["succeeded"]
return response["message"].map{|forum| Forum.new(forum["id"], forum["shortname"], forum["name"], forum["created_at"])}
@@ -25,32 +27,33 @@ def self.list(opts = {})
end
end
- def self.find(forum_id, opts = {})
+ # returns a Forum object corresponding to the given forum_id or nil if it was not found
+ def self.find(forum_id, user_api_key = nil)
+ opts = user_api_key ? {:api_key => user_api_key} : {}
list = Forum.list(opts)
if list
list.select{|f| f.id == forum_id}.first
end
end
- def load_key(opts = {})
- response = Disqus::Api::get_forum_api_key(opts.merge(:forum_id => self.id))
- if response["succeeded"]
- return @key = response["message"]
- else
- raise_api_error(response)
- end
+ # returns the Forum API Key for this forum
+ def key(user_api_key = nil)
+ @key ||= load_key(user_api_key)
end
- def key
- @key ||= load_key
+ # returns an Array of threads belonging to this forum
+ def threads(force_update = false)
+ if (@threads.nil? or @threads.empty? or force_update)
+ @threads = Disqus::Thread.list(self)
+ end
+ @threads
end
- def load_threads
- @threads = Disqus::Thread.list(self)
- end
-
- def get_thread_by_url(url, opts = {})
- response = Disqus::Api::get_thread_by_url(opts.merge(:url => url, :forum_api_key => key))
+ # returns a thread associated with the given URL
+ #
+ # a thread will only have an associated URL if it was automatically created by Disqus javascript embedded on that page.
+ def get_thread_by_url(url)
+ response = Disqus::Api::get_thread_by_url(:url => url, :forum_api_key => key)
if response["succeeded"]
t = response["message"]
Thread.new(t["id"], self, t["slug"], t["title"], t["created_at"], t["allow_comments"], t["url"], t["identifier"])
@@ -59,9 +62,16 @@ def get_thread_by_url(url, opts = {})
end
end
- def thread_by_identifier(identifier, title, opts = {})
+ # Create or retrieve a thread by an arbitrary identifying string of your choice.
+ # For example, you could use your local database's ID for the thread.
+ # This method allows you to decouple thread identifiers from the URLs on which they might be appear.
+ # (Disqus would normally use a thread's URL to identify it, which is problematic when URLs do not uniquely identify a resource.)
+ # If no thread yet exists for the given identifier (paired with the forum), one will be created.
+ #
+ # Returns a Thread object representing the thread that was created or retrieved.
+ def thread_by_identifier(identifier, title)
# TODO - should we separate thread retrieval from thread creation? The API to me seems confusing here.
- response = Disqus::Api::thread_by_identifier(opts.merge(:identifier => identifier, :title => title, :forum_api_key => key))
+ response = Disqus::Api::thread_by_identifier(:identifier => identifier, :title => title, :forum_api_key => key)
if response["succeeded"]
t = response["message"]["thread"]
Thread.new(t["id"], self, t["slug"], t["title"], t["created_at"], t["allow_comments"], t["url"], t["identifier"])
@@ -70,20 +80,40 @@ def thread_by_identifier(identifier, title, opts = {})
end
end
- def update_thread(id, title, slug, url, allow_comments, opts = {})
- result = Disqus::Api::update_thread(opts.merge(:forum_api_key => key,
- :thread_id => id,
- :title => title,
- :slug => slug,
- :url => url,
- :allow_comments => allow_comments))
+ # Sets the provided values on the thread object.
+ #
+ # Returns: An empty success message
+ #
+ # Options:
+ # * <tt>:title</tt> - the title of the thread
+ # * <tt>:slug</tt> - the per-forum-unique string used for identifying this thread in disqus.com URLs relating to this thread. Composed of underscore-separated alphanumeric strings.
+ # * <tt>:url</tt> - the URL this thread is on, if known.
+ # * <tt>:allow_comment</tt> - whether this thread is open to new comments
+ def update_thread(thread_id, opts = {})
+ result = Disqus::Api::update_thread(:forum_api_key => key,
+ :thread_id => thread_id,
+ :title => opts[:title],
+ :slug => opts[:slug],
+ :url => opts[:url],
+ :allow_comments => opts[:allow_comments])
return result["succeeded"]
end
- protected
+ private
def raise_api_error(response)
raise "Error: #{response['code']}: #{response['message']}"
end
+
+ def load_key(user_api_key = nil)
+ opts = user_api_key ? {:api_key => user_api_key} : {}
+ response = Disqus::Api::get_forum_api_key(opts.merge(:forum_id => self.id))
+ if response["succeeded"]
+ return @key = response["message"]
+ else
+ raise_api_error(response)
+ end
+ end
+
end
end
View
32 test/forum_test.rb
@@ -5,14 +5,15 @@ class ForumTest < Test::Unit::TestCase
def setup
require 'disqus'
Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
- stub_api_call(:get_forum_api_key)
+ stub_api_call(:get_forum_api_key).returns("FAKE_FORUM_API_KEY")
end
def test_forum_list
mock_api_call(:get_forum_list)
list = Disqus::Forum.list
+ expected = [create_forum]
assert_equal 1, list.size
- assert_equal list, [create_forum]
+ assert_equal expected, list
end
def test_forum_find
@@ -21,16 +22,27 @@ def test_forum_find
assert_equal "disqus-test", forum.shortname
end
- def test_load_key
+ def test_forum_find_bad_id
+ mock_api_call(:get_forum_list)
+ forum = Disqus::Forum.find(666)
+ assert_equal nil, forum
+ end
+
+ def test_forum_find_no_forums
+ Disqus::Api.expects(:get_forum_list).returns({"succeeded"=>true, "code"=>"", "message" => []})
+ forum = Disqus::Forum.find(1234)
+ assert_equal nil, forum
+ end
+
+ def test_key
mock_api_call(:get_forum_api_key)
- forum = create_forum
- assert_equal "FAKE_FORUM_API_KEY", forum.load_key
+ forum = Disqus::Forum.new(1234, "disqus-test", "Disqus Test", "2008-01-03 14:44:07.627492")
+ assert_equal "FAKE_FORUM_API_KEY", forum.key
end
- def test_load_threads
+ def test_threads
forum = create_forum
Disqus::Thread.expects(:list).with(forum).returns([thread = mock()])
- forum.load_threads
assert_equal [thread], forum.threads
end
@@ -50,5 +62,9 @@ def test_thread_by_identifier
assert_equal expected, thread
end
+ def test_update_thread
+ Disqus::Api.expects(:update_thread).with({:thread_id => 1234, :forum_api_key => "FAKE_FORUM_API_KEY", :title => 'Title', :slug => "a_slug", :url => "http://www.example.com", :allow_comments => true}).returns({"succeeded" => true})
+ forum = create_forum
+ forum.update_thread(1234, :title => 'Title', :slug => "a_slug", :url => "http://www.example.com", :allow_comments => true)
+ end
end
-
View
6 test/test_helper.rb
@@ -10,12 +10,14 @@
DISQUS_TEST = YAML.load(File.read(File.dirname(__FILE__) + "/config.yml"))
def create_forum
- Disqus::Forum.new(1234, "disqus-test", "Disqus Test", "2008-01-03 14:44:07.627492")
+ forum = Disqus::Forum.new(1234, "disqus-test", "Disqus Test", "2008-01-03 14:44:07.627492")
+ forum.stubs(:key).returns("FAKE_FORUM_API_KEY")
+ forum
end
def create_thread
mock_forum = mock()
- mock_forum.stubs(:key).returns("FAKE_KEY")
+ mock_forum.stubs(:key).returns("FAKE_FORUM_API_KEY")
Disqus::Thread.new("7651269", mock_forum, "test_thread", "Test thread", "2008-11-28T01:47", true, "FAKE_URL", nil)
end

0 comments on commit e9265bc

Please sign in to comment.
Something went wrong with that request. Please try again.