Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'api', after merging Matt Van Horn's API work.

  • Loading branch information...
commit e96b2e0648762ffa8f85300dcaad0d9896b2a957 2 parents d740883 + e9e0e10
Norman Clarke authored
View
1  .gitignore
@@ -1,3 +1,4 @@
.DS_Store
*.gem
coverage
+test/config.yml
View
121 README.textile
@@ -1,121 +0,0 @@
-h1. Disqus Ruby Gem
-
-The Disqus Gem helps you easily integrate the "Disqus":http://disqus.com
-commenting system into your website. It works for any site programmed in Ruby,
-and has view helpers for Rails and Merb.
-
-At the moment it only supports Disqus Javascript widgets. An implementation of
-the Disqus API is underway (in the "api" repository branch) and will be
-released soon.
-
-h2. Get it
-
-h3. Stable release:
-
-<code>
- gem install disqus
-</code>
-
-h3. Bleeding edge:
-
-<code>
- gem install norman-disqus --source http://gems.github.com
-</code>
-
-h2. Use it:
-
-h3. Configure it:
-
-h4. Generic example:
-
-<pre>
-<code>
- # Somewhere in your code
- Disqus::defaults[:account] = "my_disqus_account"
-</code>
-</pre>
-
-h4. Rails-specific example:
-
-<pre>
-<code>
- # in config/development.rb (or production.rb, test.rb, etc.)
- config.after_initialize do
- Disqus::defaults[:account] = "my_disqus_account"
- end
-</code>
-</pre>
-Note that here the "after initialize" is necessary, otherwise your settings
-won't be set properly.
-
-
-h3. Show the comment threads on a post page:
-
-<pre>
-<code>
- # Loads the commenting system
- disqus_thread
-
- # Or if you're not using Rails/Merb:
- Disqus::Widget::thread
-
- # 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>
- disqus_comment_counts
-
- # Or if you're not using Rails/Merb:
- Disqus::Widget::comment_counts
-</code>
-</pre>
-
-h3. Show the combo widget on a post page:
-
-<pre>
-<code>
- disqus_combo(:color => "blue", :hide_mods => false, :num_items => 20)
-
- # Or for non-Rails/Merb:
- Disqus::Widget::combo(:color => "blue", :hide_mods => false, :num_items => 20)
-</code>
-</pre>
-
-h3. Show the comment count on a permalink:
-
-<pre>
-<code>
- link_to("Permalink", post_path(@post, :anchor => "disqus_thread"))
- ...
- disqus_comment_counts
-
- # Or for non-Rails/Merb:
- Disqus::Widget::comment_counts
-</code>
-</pre>
-
-h2. Hack it:
-
-Github repository: http://github.com/norman/disqus
-
-h2. Complain about it:
-
-"norman@randomba.org":norman@randomba.org
-
-h2. Learn more about Disqus:
-
-"http://disqus.com":http://disqus.com
-
-h2. Thanks to the following contributors:
-
-* "Quin Hoxie":http://github.com/qhoxie - Merb support
-
-h2. Legal Stuff
-
-The Disqus Ruby gem was not created by, nor is officially supported by
-Disqus.com or Big Head Labs, Inc. Use it at your own risk and your own
-responsibility under the terms of the MIT License.
-
-Copyright (c) 2008 "Norman Clarke":norman@randomba.org, released under
-the MIT license
View
38 disqus.gemspec
@@ -1,27 +1,51 @@
Gem::Specification.new do |s|
- s.name = "disqus"
+ s.name = "disqus-api"
s.version = "0.1.1"
s.date = "2008-09-03"
- s.rubyforge_project = 'disqus'
+ s.rubyforge_project = 'disqus-api'
s.summary = "Integrates Disqus commenting system into your Ruby-powered site."
s.email = 'norman@randomba.org'
s.homepage = 'http://randomba.org'
s.description = 'Integrates Disqus into your Ruby-powered site. Works with any Ruby website, and has view helpers for Rails and Merb.'
s.has_rdoc = true
- s.authors = ['Norman Clarke']
+ s.authors = ['Norman Clarke','Matt Van Horn']
s.files = [
"MIT-LICENSE",
"README.textile",
"init.rb",
"lib/disqus.rb",
- "lib/disqus/widget.rb",
+ "lib/disqus/api.rb",
+ "lib/disqus/author.rb",
+ "lib/disqus/forum.rb",
+ "lib/disqus/post.rb",
+ "lib/disqus/thread.rb",
"lib/disqus/view_helpers.rb",
- "Rakefile",
- ]
+ "lib/disqus/widget.rb",
+ "Rakefile"
+ ]
s.test_files = [
+ "test/test_helper.rb",
+ "test/config.yml.sample",
+ "test/api_test.rb",
+ "test/forum_test.rb",
+ "test/merb_test.rb",
+ "test/post_test.rb",
+ "test/rails_test.rb",
+ "test/thread_test.rb",
+ "test/view_helpers_test.rb",
"test/widget_test.rb",
- "test/view_helpers_test.rb"
+ "test/responses/bad_api_key.json",
+ "test/responses/create_post.json",
+ "test/responses/get_forum_api_key.json",
+ "test/responses/get_forum_list.json",
+ "test/responses/get_num_posts.json",
+ "test/responses/get_thread_by_url.json",
+ "test/responses/get_thread_list.json",
+ "test/responses/get_thread_posts.json",
+ "test/responses/thread_by_identifier.json",
+ "test/responses/update_thread.json"
]
s.rdoc_options = ["--main", "README.textile", "--inline-source", "--line-numbers"]
s.extra_rdoc_files = ["README.textile"]
end
+
View
7 init.rb
@@ -1 +1,6 @@
-require 'disqus'
+require 'disqus'
+require 'disqus/api'
+require 'disqus/author'
+require 'disqus/forum'
+require 'disqus/post'
+require 'disqus/thread'
View
1  lib/disqus.rb
@@ -7,6 +7,7 @@
module Disqus
@defaults = {
+ :api_key => "",
:account => "",
:avatar_size => 48,
:color => "grey",
View
203 lib/disqus/api.rb
@@ -0,0 +1,203 @@
+require 'open-uri'
+require 'rubygems'
+require 'json'
+require 'net/http'
+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],
+ :thread_id => opts[:thread_id],
+ :message => opts[:message],
+ :author_name => opts[:author_name],
+ :author_email => opts[:author_email],
+ :parent_post => opts[:parent_post],
+ :created_at => opts[:created_at], #UTC timestring, format: %Y-%m-%dT%H:%M
+ :author_url => opts[:author_url],
+ :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 = {})
+ 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 = {})
+ 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 = {})
+ 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 = {})
+ 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 = {})
+ raise opts.inspect
+ JSON.parse(post('update_thread', :forum_api_key => opts[:forum_api_key],
+ :thread_id => opts[:thread_id],
+ :title => opts[:title],
+ :slug => opts[:slug],
+ :url => opts[:url],
+ :allow_comments => opts[:allow_comments] ))
+ end
+
+
+ private
+
+ def escape(string)
+ URI::encode(string, /[^a-z0-9]/i)
+ end
+
+ def get(*args)
+ open(make_url(*args)) {|u| u.read }
+ end
+
+ def post(*args)
+ url = ROOT + '/' + args.shift
+ post_params = {}
+ args.shift.each { |k, v| post_params[k.to_s]=v.to_s }
+ Net::HTTP.post_form(URI.parse(url),post_params)
+ end
+
+ def make_url(*args)
+ url = ROOT + '/' + args.shift + '/?'
+ args.shift.each { |k, v| url += "#{k}=#{escape(v.to_s)}&" }
+ return url.chomp('&')
+ end
+
+ def validate_opts!(opts)
+ raise ArgumentError.new("You must specify an :api_key") if !opts[:api_key]
+ end
+
+ end
+
+ end
+
+end
View
26 lib/disqus/author.rb
@@ -0,0 +1,26 @@
+module Disqus
+ class BaseAuthor
+ attr_reader :url, :email_hash
+ end
+
+ class Author < BaseAuthor
+ attr_reader :id, :username, :display_name, :has_avatar
+ def initialize(id, username, display_name, url, email_hash, has_avatar)
+ @id, @username, @display_name, @url, @email_hash, @has_avatar = id, username, display_name, url, email_hash, has_avatar
+ end
+
+ # returns the user's <tt>display_name</tt> or <tt>username</tt> if <tt>display_name</tt> is blank.
+ def name
+ @display_name.blank? ? @username : @display_name
+ end
+
+ end
+
+ class AnonymousAuthor < BaseAuthor
+ attr_reader :name
+ def initialize(name, url, email_hash)
+ @name, @url, @email_hash = name, url, email_hash
+ end
+ end
+end
+
View
119 lib/disqus/forum.rb
@@ -0,0 +1,119 @@
+module Disqus
+
+ class Forum
+ attr_reader :id, :shortname, :name, :created_at, :threads
+
+ 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 = include_threads ? load_threads : []
+ end
+
+ def ==(other_forum)
+ id == other_forum.id &&
+ shortname == other_forum.shortname &&
+ name == other_forum.name &&
+ key == other_forum.key
+ end
+
+ # 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"])}
+ else
+ raise_api_error(response)
+ end
+ end
+
+ # 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
+
+ # returns the Forum API Key for this forum
+ def key(user_api_key = nil)
+ @key ||= load_key(user_api_key)
+ end
+
+ # 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
+
+ # 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"])
+ else
+ raise_api_error(response)
+ end
+ 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 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(: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"])
+ else
+ raise_api_error(response)
+ end
+ end
+
+ # 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
+
+ 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
44 lib/disqus/post.rb
@@ -0,0 +1,44 @@
+module Disqus
+
+ class Post
+ attr_reader :id, :forum, :thread, :created_at, :message, :parent_post, :shown, :is_anonymous, :author
+
+ def initialize(id, forum, thread, created_at, message, parent_post, shown, is_anonymous, author)
+ @id, @forum, @thread, @created_at, @message, @parent_post, @shown, @is_anonymous, @author = id.to_i, forum, thread, Time.parse(created_at.to_s), message, parent_post, shown, is_anonymous, author
+ end
+
+ # Returns an array of Post objects representing all posts belonging to the given thread. The arrays is sorted in order of created_at date.
+ def self.list(thread)
+ response = Disqus::Api::get_thread_posts(:thread_id =>thread.id, :forum_api_key => thread.forum.key)
+ if response["succeeded"]
+ posts = response["message"].map do |post|
+ author = nil
+ if post["is_anonymous"]
+ author = AnonymousAuthor.new( post["anonymous_author"]["name"],
+ post["anonymous_author"]["url"],
+ post["anonymous_author"]["email_hash"])
+ else
+ author = Author.new(post["author"]["id"].to_i,
+ post["author"]["username"],
+ post["author"]["display_name"],
+ post["author"]["url"],
+ post["author"]["email_hash"],
+ post["author"]["has_avatar"] )
+ end
+ Post.new( post["id"],
+ thread.forum,
+ thread,
+ post["created_at"],
+ post["message"],
+ post["parent_post"],
+ post["shown"],
+ post["is_anonymous"],
+ author )
+ end
+ posts.sort!{|a,b| a.created_at <=> b.created_at}
+ end
+ end
+ end
+end
+
+
View
68 lib/disqus/thread.rb
@@ -0,0 +1,68 @@
+module Disqus
+
+ class Thread
+ attr_reader :id, :forum, :slug, :title, :created_at, :allow_comments, :url, :identifier, :forum
+
+ def initialize(id, forum, slug, title, created_at, allow_comments, url, identifier)
+ @id, @forum, @slug, @title, @created_at, @allow_comments, @url, @identifier = id.to_i, forum, slug, title, Time.parse(created_at.to_s), allow_comments, url, identifier
+ @posts = []
+ end
+
+ # Threads are equal if all their attributes are equal.
+ def ==(other_thread)
+ id == other_thread.id &&
+ forum == other_thread.forum &&
+ slug == other_thread.slug &&
+ title == other_thread.title &&
+ created_at == other_thread.created_at &&
+ allow_comments == other_thread.allow_comments &&
+ url == other_thread.url &&
+ identifier == other_thread.identifier
+ end
+
+ # returns an array of Thread objects representing the threads belonging to the given Forum.
+ def self.list(forum, opts = {})
+ response = Disqus::Api::get_thread_list(opts.merge(:forum_id =>forum.id, :forum_api_key => forum.key))
+ if response["succeeded"]
+ list = response["message"].map do |thread|
+ Thread.new( thread["id"],
+ forum,
+ thread["slug"],
+ thread["title"],
+ thread["created_at"],
+ thread["allow_comments"],
+ thread["url"],
+ thread["identifier"] )
+ end
+ end
+ end
+
+ # returns an Array of posts belonging to this thread
+ def posts(force_update = false)
+ if (@posts.nil? or @posts.empty? or force_update)
+ @posts = Disqus::Post.list(self)
+ end
+ @posts
+ end
+
+ # Sets the provided values on the thread object.
+ #
+ # options:
+ # * :thread_id
+ # * :title
+ # * :slug
+ # * :url
+ # * :allow_comments
+ def update(opts = {})
+ result = Disqus::Api::update_thread(opts.merge( :forum_api_key => forum.key,
+ :thread_id => id,
+ :title => title,
+ :slug => slug,
+ :url => url,
+ :allow_comments => allow_comments))
+ return result["succeeded"]
+ end
+
+ end
+
+end
View
74 test/api_test.rb
@@ -0,0 +1,74 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class ApiTest < Test::Unit::TestCase
+
+ def setup
+ require 'disqus'
+ Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
+ end
+
+ def test_create_post
+ mock_post_response('create_post.json')
+ the_post = Disqus::Api::create_post()
+ assert_equal "This is a mock post", the_post["message"]["message"]
+ end
+
+ def test_get_forum_list
+ mock_get_response('get_forum_list.json')
+ forum_list = Disqus::Api::get_forum_list
+ assert_equal "Disqus Test", forum_list["message"][0]["name"]
+ end
+
+ def test_get_forum_api_key
+ mock_get_response('get_forum_api_key.json')
+ forum_api_key = Disqus::Api::get_forum_api_key(:forum_id => 1234, :user_api_key=>"FAKE_KEY")
+ assert_equal "FAKE_FORUM_API_KEY", forum_api_key["message"]
+ end
+
+ def test_get_thread_list
+ mock_get_response('get_thread_list.json')
+ thread_list = Disqus::Api::get_thread_list(:forum_api_key=>"FAKE_KEY")
+ assert_equal "this_is_the_thread_identifier", thread_list["message"].first["identifier"]
+ end
+
+ def test_get_num_posts
+ mock_get_response('get_num_posts.json')
+ nums = Disqus::Api::get_num_posts(:thread_ids => [123,456], :forum_api_key=>"FAKE_KEY")
+ assert_equal [10,12], nums["message"][nums["message"].keys.first]
+ end
+
+ def test_get_thread_by_url
+ mock_get_response('get_thread_by_url.json')
+ thread = Disqus::Api::get_thread_by_url(:url => "FAKE_URL", :forum_api_key=>"FAKE_KEY")
+ assert_equal "test_thread", thread["message"]["slug"]
+ end
+
+ def test_get_thread_posts
+ mock_get_response('get_thread_posts.json')
+ thread_posts = Disqus::Api::get_thread_posts(:thread_id =>1234, :forum_api_key => "FAKE_KEY")
+ assert_equal "This is a mock post", thread_posts["message"].first["message"]
+ end
+
+ def test_thread_by_identifier
+ mock_post_response('thread_by_identifier.json')
+ thread = Disqus::Api::thread_by_identifier(:identifier =>'foo_bar', :title => "Foo Bar", :forum_api_key => "FAKE_KEY")
+ assert_equal "Test thread", thread["message"]["thread"]["title"]
+ end
+
+ def test_update_thread
+ mock_post_response('update_thread.json')
+ result = Disqus::Api::thread_by_identifier(:thread_id =>123, :title => "Foo Bar", :forum_api_key => "FAKE_KEY")
+ assert result["succeeded"]
+ end
+
+ private
+
+ def mock_get_response(file)
+ Disqus::Api.expects(:get).returns(File.read(File.dirname(__FILE__) + "/responses/#{file}"))
+ end
+
+ def mock_post_response(file)
+ Disqus::Api.expects(:post).returns(File.read(File.dirname(__FILE__) + "/responses/#{file}"))
+ end
+
+end
View
3  test/config.yml.sample
@@ -0,0 +1,3 @@
+# This can be used to do actual calls to the API rather than use mocks during
+# testing. This can be useful to verify tests against API changes.
+api_key: 'YOUR DISQUS USER API KEY GOES HERE'
View
70 test/forum_test.rb
@@ -0,0 +1,70 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class ForumTest < Test::Unit::TestCase
+
+ def setup
+ require 'disqus'
+ Disqus.defaults[:api_key] = DISQUS_TEST["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 expected, list
+ end
+
+ def test_forum_find
+ mock_api_call(:get_forum_list)
+ forum = Disqus::Forum.find(1234)
+ assert_equal "disqus-test", forum.shortname
+ end
+
+ 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 = 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_threads
+ forum = create_forum
+ Disqus::Thread.expects(:list).with(forum).returns([thread = mock()])
+ assert_equal [thread], forum.threads
+ end
+
+ def test_get_thread_by_url
+ mock_api_call(:get_thread_by_url)
+ forum = create_forum
+ thread = forum.get_thread_by_url("http://www.example.com")
+ expected = Disqus::Thread.new("7651269", forum, "test_thread", "Test thread", "2008-11-28T01:47", true, "FAKE_URL", nil)
+ assert_equal expected, thread
+ end
+
+ def test_thread_by_identifier
+ mock_api_call(:thread_by_identifier)
+ forum = create_forum
+ thread = forum.thread_by_identifier("FAKE_IDENTIFIER", "")
+ expected = Disqus::Thread.new("7651269", forum, "test_thread", "Test thread", "2008-11-28T01:47", true, "FAKE_URL", "FAKE_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
4 test/merb_test.rb
@@ -1,10 +1,10 @@
require 'test/unit'
-
+
module Merb
class Controller
end
end
-
+
class MerbTest < Test::Unit::TestCase
def test_view_helpers_should_be_included
View
18 test/post_test.rb
@@ -0,0 +1,18 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class PostTest < Test::Unit::TestCase
+
+ def setup
+ require 'disqus'
+ Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
+ end
+
+ def test_post_list
+ mock_api_call(:get_thread_posts)
+ list = Disqus::Post.list(create_thread)
+ assert_equal 2, list.size
+ assert_equal list.first.message, "This is a mock post"
+ end
+
+
+end
View
1  test/responses/bad_api_key.json
@@ -0,0 +1 @@
+{"message": "user_api_key BAD could not be converted to UserKey object", "code": "input-invalid", "succeeded": false}
View
23 test/responses/create_post.json
@@ -0,0 +1,23 @@
+{
+ "message": {
+ "id": "12345",
+ "forum": "1234",
+ "thread": "123",
+ "created_at": "2008-01-03 14:44:07.627492",
+ "message": "This is a mock post",
+ "parent_post": null,
+ "shown": true,
+ "is_anonymous": false,
+ "author": {
+ "id":12,
+ "username":"testuser",
+ "display_name":"Test User",
+ "url":"http://www.example.com/testuser",
+ "email_hash":"thisisanMD5hash",
+ "has_avatar": true
+ }
+ },
+ "code": "ok",
+ "succeeded": true
+}
+
View
1  test/responses/get_forum_api_key.json
@@ -0,0 +1 @@
+{"message": "FAKE_FORUM_API_KEY", "code": "ok", "succeeded": true}
View
12 test/responses/get_forum_list.json
@@ -0,0 +1,12 @@
+{
+ "message": [
+ {
+ "created_at": "2008-01-03 14:44:07.627492",
+ "shortname": "disqus-test",
+ "id": "1234",
+ "name": "Disqus Test"
+ }
+ ],
+ "code": "ok",
+ "succeeded": true
+}
View
11 test/responses/get_num_posts.json
@@ -0,0 +1,11 @@
+{
+ "message":{
+ "1001":[10,12],
+ "1002":[15,20],
+ "1003":[20,20],
+ "1004":[10,10],
+ "1005":[5,10]
+ },
+ "succeeded":true,
+ "code":"ok"
+}
View
18 test/responses/get_thread_by_url.json
@@ -0,0 +1,18 @@
+{
+ "message":{
+ "slug": "test_thread",
+ "forum": "60732",
+ "title": "Test thread",
+ "url": "FAKE_URL",
+ "id": "7651269",
+ "identifier": null,
+ "created_at": "2008-11-28T01:47",
+ "allow_comments": true
+ },
+ "succeeded": true,
+ "code": "ok"
+}
+
+
+
+
View
16 test/responses/get_thread_list.json
@@ -0,0 +1,16 @@
+{
+ "message": [
+ {
+ "id": "12345",
+ "forum": "1234",
+ "slug": "this_is_a_thread",
+ "title": "This is a thread",
+ "created_at": "2008-01-03 14:44:07.627492",
+ "allow_comments": true,
+ "url":"http://www.example.com/testthread",
+ "identifier": "this_is_the_thread_identifier"
+ }
+ ],
+ "code": "ok",
+ "succeeded": true
+}
View
37 test/responses/get_thread_posts.json
@@ -0,0 +1,37 @@
+{
+ "message": [{
+ "id": "12345",
+ "forum": "1234",
+ "thread": "123",
+ "created_at": "2008-01-03 14:44:07.627492",
+ "message": "This is a mock post",
+ "parent_post": null,
+ "shown": true,
+ "is_anonymous": false,
+ "author": {
+ "id":12,
+ "username":"testuser",
+ "display_name":"Test User",
+ "url":"http://www.example.com/testuser",
+ "email_hash":"thisisanMD5hash",
+ "has_avatar": true
+ }
+ },{
+ "id": "23456",
+ "forum": "1234",
+ "thread": "123",
+ "created_at": "2008-01-03 14:44:07.627492",
+ "message": "This is another mock post",
+ "parent_post": null,
+ "shown": true,
+ "is_anonymous": true,
+ "anonymous_author": {
+ "id":12,
+ "name":"Anonymous Coward",
+ "email_hash":"thisisanMD5hash"
+ }
+ }],
+ "code": "ok",
+ "succeeded": true
+}
+
View
17 test/responses/thread_by_identifier.json
@@ -0,0 +1,17 @@
+{
+ "message":{
+ "thread":{
+ "slug": "test_thread",
+ "forum": "60732",
+ "title": "Test thread",
+ "url": "FAKE_URL",
+ "id": "7651269",
+ "identifier": "FAKE_IDENTIFIER",
+ "created_at": "2008-11-28T01:47",
+ "allow_comments": true
+ },
+ "created": false
+ },
+ "succeeded": true,
+ "code": "ok"
+}
View
5 test/responses/update_thread.json
@@ -0,0 +1,5 @@
+{
+ "message":{},
+ "succeeded": true,
+ "code": "ok"
+}
View
32 test/test_helper.rb
@@ -0,0 +1,32 @@
+require 'test/unit'
+require 'yaml'
+require 'disqus/api'
+require 'disqus/forum'
+require 'disqus/thread'
+require 'disqus/post'
+require 'disqus/author'
+require 'mocha'
+
+DISQUS_TEST = YAML.load(File.read(File.dirname(__FILE__) + "/config.yml"))
+
+def create_forum
+ 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_FORUM_API_KEY")
+ Disqus::Thread.new("7651269", mock_forum, "test_thread", "Test thread", "2008-11-28T01:47", true, "FAKE_URL", nil)
+end
+
+
+def mock_api_call(method_name)
+ Disqus::Api.expects(method_name.to_sym).returns(JSON.parse(File.read(File.dirname(__FILE__) + "/responses/#{method_name}.json")))
+end
+
+def stub_api_call(method_name)
+ Disqus::Api.stubs(method_name.to_sym).returns(JSON.parse(File.read(File.dirname(__FILE__) + "/responses/#{method_name}.json")))
+end
+
View
28 test/thread_test.rb
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class ThreadTest < Test::Unit::TestCase
+
+ def setup
+ require 'disqus'
+ Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
+ stub_api_call(:get_forum_api_key)
+ end
+
+ def test_thread_list
+ mock_api_call(:get_thread_list)
+ forum = create_forum
+ list = Disqus::Thread.list(forum)
+ assert_equal 1, list.size
+ assert_equal list, [Disqus::Thread.new( 12345,
+ create_forum,
+ "this_is_a_thread",
+ "This is a thread",
+ "2008-01-03 14:44:07.627492",
+ true,
+ "http://www.example.com/testthread",
+ "this_is_the_thread_identifier" )]
+ end
+
+end
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.