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

Commit

Permalink
began work on a Forum 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 4e208b8 commit 4d9612e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 20 deletions.
17 changes: 12 additions & 5 deletions disqus.gemspec
@@ -1,26 +1,33 @@
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",
"lib/disqus/widget.rb"
"Rakefile",
]
s.test_files = [
"test/widget_test.rb",
"test/view_helpers_test.rb"
"test/view_helpers_test.rb",
"test/api_test.rb",
"test/forum_test.rb"
]
s.rdoc_options = ["--main", "README.textile", "--inline-source", "--line-numbers"]
s.extra_rdoc_files = ["README.textile"]
Expand Down
35 changes: 32 additions & 3 deletions lib/disqus/forum.rb
@@ -1,9 +1,38 @@
module Disqus

class Forum
attr_reader :id, :shortname, :name
def self.initialize(id, shortname, name)
@id, @shortname, @name = id, shortname, name
attr_reader :id, :shortname, :name, :created_at, :key

def initialize(id, shortname, name, key=nil)
@id, @shortname, @name = id.to_i, shortname, name
end

def ==(other_forum)
id == other_forum.id
shortname == other_forum.shortname
name == other_forum.name
key == other_forum.key
end

def self.list(opts = {})
response = Disqus::Api::get_forum_list(opts)
if response["succeeded"]
list = response["message"].map{|forum| Forum.new(forum["id"], forum["shortname"], forum["name"], forum["created_at"])}
end
end

def self.find(forum_id, opts = {})
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))
@key = response["message"] if response["succeeded"]
end

end
end

3 changes: 2 additions & 1 deletion lib/disqus/thread.rb
Expand Up @@ -4,8 +4,9 @@ class Thread
attr_reader :id, :forum, :slug, :title, :created_at, :allow_comments, :url, :identifier

def self.initialize(id, forum, slug, title, created_at, allow_comments, url, identifier)
@id, @forum, @slug, @title, @created_at, @allow_comments, @url, @identifier = 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
end

end

end
2 changes: 1 addition & 1 deletion test/api_test.rb
Expand Up @@ -26,7 +26,7 @@ def test_get_forum_list

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, :forum_api_key=>"FAKE_KEY")
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

Expand Down
48 changes: 48 additions & 0 deletions test/forum_test.rb
@@ -0,0 +1,48 @@
require 'test/unit'
require 'yaml'
require 'disqus'
require 'disqus/api'
require 'disqus/forum'
require 'mocha'

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

class ForumTest < Test::Unit::TestCase

def setup
Disqus.defaults[:api_key] = DISQUS_TEST["api_key"]
end

def test_forum_list
mock_api_call(:get_forum_list)
list = Disqus::Forum.list
assert_equal 1, list.size
assert_equal list, [create_forum]
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_load_key
mock_api_call(:get_forum_api_key)
forum = create_forum
assert_equal "FAKE_FORUM_API_KEY", forum.load_key
end


private

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

13 changes: 12 additions & 1 deletion test/responses/get_forum_list.json
@@ -1 +1,12 @@
{"message": [{"created_at": "2008-01-03 14:44:07.627492", "shortname": "disqus-test", "id": "1234", "name": "Disqus Test"}], "code": "ok", "succeeded": true}
{
"message": [
{
"created_at": "2008-01-03 14:44:07.627492",
"shortname": "disqus-test",
"id": "1234",
"name": "Disqus Test"
}
],
"code": "ok",
"succeeded": true
}
9 changes: 0 additions & 9 deletions test/responses/get_thread_list.json
Expand Up @@ -10,15 +10,6 @@
"shortname": "disqus-test",
"url":"http://www.example.com/testthread",
"identifier": "this_is_the_thread_identifier"
},
{ "id": "23456",
"forum": "1234",
"slug": "this_is_another_thread",
"title": "This is another thread",
"created_at": "2008-01-03 15:44:07.627492",
"allow_comments": true,
"url":"http://www.example.com/testthreadtwo",
"identifier": "this_is_another_thread_identifier"
}
],
"code": "ok",
Expand Down

0 comments on commit 4d9612e

Please sign in to comment.