Permalink
Browse files

added api methods to forum and thread, began post obj.

  • Loading branch information...
1 parent 8aa9d33 commit 3de512273cef6ec5901b71c7e4dd39f2067b5121 Matthew Van Horn committed Dec 1, 2008
View
@@ -1,7 +1,7 @@
module Disqus
class Forum
- attr_reader :id, :shortname, :name, :created_at
+ attr_reader :id, :shortname, :name, :created_at, :threads
def initialize(id, shortname, name, created_at)
@id, @shortname, @name = id.to_i, shortname, name
@@ -42,6 +42,33 @@ def key
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))
+ 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"])
+ end
+ end
+
+ def get_thread_by_identifier(identifier, title, opts = {})
+ # 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))
+ 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"])
+ 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))
+ return result["succeeded"]
+ end
+
end
end
-
View
@@ -1,6 +1,35 @@
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.to_i, forum, thread, created_at, message, parent_post, shown, is_anonymous, author
+ end
+
+ def self.list(thread, opts = {})
+ response = Disqus::Api::get_thread_posts(opts.merge(:thread_id =>thread.id, :forum_api_key => thread.forum.key))
+ if response["succeeded"]
+ posts = response["message"].map do |post|
+ Post.new( post["id"],
+ thread.forum,
+ thread,
+ post["created_at"],
+ post["message"],
+ post["parent_post"],
+ post["shown"],
+ post["is_anonymous"],
+ post["is_anonymous"] ? AnonymousAuthor.new( post["anonymous_author"]["name"],
+ post["anonymous_author"]["url"],
+ post["anonymous_author"]["email_hash"]) :
+ 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
+ end
+ end
end
end
View
@@ -1,10 +1,11 @@
module Disqus
class Thread
- attr_reader :id, :forum, :slug, :title, :created_at, :allow_comments, :url, :identifier
+ attr_reader :id, :forum, :slug, :title, :created_at, :allow_comments, :url, :identifier, :forum, :posts
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, created_at, allow_comments, url, identifier
+ @posts = []
end
def ==(other_thread)
@@ -34,6 +35,20 @@ def self.list(forum, opts = {})
end
end
+ def load_posts(opts = {})
+ @posts = Post.list(self, opts)
+ end
+
+ 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
@@ -3,6 +3,7 @@
require 'disqus'
require 'disqus/api'
require 'disqus/forum'
+require 'disqus/thread'
require 'mocha'
DISQUS_TEST = YAML.load(File.read(File.dirname(__FILE__) + "/config.yml"))
@@ -32,13 +33,42 @@ def test_load_key
assert_equal "FAKE_FORUM_API_KEY", forum.load_key
end
+ def test_load_threads
+ forum = create_forum
+ Disqus::Thread.expects(:list).with(forum).returns([thread = mock()])
+ forum.load_threads
+ 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_get_thread_by_identifier
+ mock_api_call(:thread_by_identifier)
+ forum = create_forum
+ thread = forum.get_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
+
+
+
private
- def create_forum
+ def create_forum
Disqus::Forum.new(1234, "disqus-test", "Disqus Test", "2008-01-03 14:44:07.627492")
end
+ def create_thread
+ Disqus::Thread.new("7651269", 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
@@ -6,7 +6,6 @@
"url": "FAKE_URL",
"id": "7651269",
"identifier": null,
- "hidden": false,
"created_at": "2008-11-28T01:47",
"allow_comments": true
},
@@ -6,8 +6,7 @@
"title": "Test thread",
"url": "FAKE_URL",
"id": "7651269",
- "identifier": null,
- "hidden": false,
+ "identifier": "FAKE_IDENTIFIER",
"created_at": "2008-11-28T01:47",
"allow_comments": true
},

0 comments on commit 3de5122

Please sign in to comment.