Skip to content
This repository has been archived by the owner on Dec 5, 2017. It is now read-only.

Commit

Permalink
began thread object
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Van Horn authored and Matthew Van Horn committed Nov 29, 2008
1 parent 9d89444 commit 8aa9d33
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
15 changes: 12 additions & 3 deletions lib/disqus/forum.rb
@@ -1,10 +1,12 @@
module Disqus

class Forum
attr_reader :id, :shortname, :name, :created_at, :key
attr_reader :id, :shortname, :name, :created_at

def initialize(id, shortname, name, key=nil)
def initialize(id, shortname, name, created_at)
@id, @shortname, @name = id.to_i, shortname, name
@key = nil
@threads = []
end

def ==(other_forum)
Expand Down Expand Up @@ -32,7 +34,14 @@ def load_key(opts = {})
response = Disqus::Api::get_forum_api_key(opts.merge(:forum_id => self.id))
@key = response["message"] if response["succeeded"]
end


def key
@key ||= load_key
end

def load_threads
@threads = Disqus::Thread.list(self)
end
end
end

16 changes: 16 additions & 0 deletions lib/disqus/thread.rb
Expand Up @@ -18,6 +18,22 @@ def ==(other_thread)
identifier == other_thread.identifier
end

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

end

end
3 changes: 1 addition & 2 deletions test/responses/get_thread_list.json
Expand Up @@ -6,8 +6,7 @@
"slug": "this_is_a_thread",
"title": "This is a thread",
"created_at": "2008-01-03 14:44:07.627492",
"allow_comments": true,
"shortname": "disqus-test",
"allow_comments": true,
"url":"http://www.example.com/testthread",
"identifier": "this_is_the_thread_identifier"
}
Expand Down
48 changes: 48 additions & 0 deletions test/thread_test.rb
@@ -0,0 +1,48 @@
require 'test/unit'
require 'yaml'
require 'disqus'
require 'disqus/api'
require 'disqus/forum'
require 'disqus/thread'
require 'mocha'

DISQUS_TEST = YAML.load(File.read(File.dirname(__FILE__) + "/config.yml"))

class ThreadTest < Test::Unit::TestCase

def setup
Disqus.defaults[:api_key] = DISQUS_TEST["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, [create_thread]
end

private

def create_thread
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

def create_forum
Disqus::Forum.new(1234, "disqus-test", "Disqus Test", "2008-01-03 14:44:07.627492")
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


end

0 comments on commit 8aa9d33

Please sign in to comment.