nbudin-disqus 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,15 @@
1
+ == 1.0.2 2009-10-05
2
+ 2 bug fixes:
3
+ * The Disqus::API.post method now returns the response body rather than the response object (Nat Budin)
4
+ * Disqus::API.create_post now complies with DISQUS's API requirements (Nat Budin)
5
+
6
+ == 1.0.1 2009-01-19
7
+ 2 minor enhancements:
8
+ * Improve docs (Quin Hoxie)
9
+ * Add developer and container_id options for comment threads. (Quin Hoxie)
10
+
11
+ == 1.0.0 2009-01-05
12
+ * 1 major enhancement:
13
+ * Support for Disqus API added (Matthew Van Horn)
14
+ * 1 minor enhancement:
15
+ * Reorganized gem to use newgem
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,38 @@
1
+ History.txt
2
+ MIT-LICENSE
3
+ Manifest.txt
4
+ README.rdoc
5
+ Rakefile
6
+ config/website.yml
7
+ disqus.gemspec
8
+ init.rb
9
+ lib/disqus.rb
10
+ lib/disqus/api.rb
11
+ lib/disqus/author.rb
12
+ lib/disqus/forum.rb
13
+ lib/disqus/post.rb
14
+ lib/disqus/thread.rb
15
+ lib/disqus/version.rb
16
+ lib/disqus/view_helpers.rb
17
+ lib/disqus/widget.rb
18
+ tasks/rcov.rake
19
+ test/api_test.rb
20
+ test/config.yml.sample
21
+ test/forum_test.rb
22
+ test/merb_test.rb
23
+ test/post_test.rb
24
+ test/rails_test.rb
25
+ test/responses/bad_api_key.json
26
+ test/responses/create_post.json
27
+ test/responses/get_forum_api_key.json
28
+ test/responses/get_forum_list.json
29
+ test/responses/get_num_posts.json
30
+ test/responses/get_thread_by_url.json
31
+ test/responses/get_thread_list.json
32
+ test/responses/get_thread_posts.json
33
+ test/responses/thread_by_identifier.json
34
+ test/responses/update_thread.json
35
+ test/test_helper.rb
36
+ test/thread_test.rb
37
+ test/view_helpers_test.rb
38
+ test/widget_test.rb
data/README.rdoc ADDED
@@ -0,0 +1,143 @@
1
+ == Disqus Ruby Gem
2
+
3
+ The Disqus Gem helps you easily integrate the {Disqus}[http://disqus.com]
4
+ commenting system into your website. It works for any site programmed in Ruby,
5
+ and has view helpers for Rails and Merb.
6
+
7
+ Support for the Disqus Javascript widgets is stable. Disqus API support is
8
+ "beta."
9
+
10
+ === What is Disqus?
11
+
12
+ From the Disqus website:
13
+
14
+ "Disqus, pronounced "discuss", is a service and tool for web comments and
15
+ discussions. The Disqus comment system can be plugged into any website, blog,
16
+ or application. Disqus makes commenting easier and more interactive, while
17
+ connecting websites and commenters across a thriving discussion community."
18
+
19
+ "Disqus is a free service to the general public with absolutely no inline advertisements."
20
+
21
+ === Get it
22
+
23
+ gem install disqus
24
+
25
+ ==== Bleeding Edge
26
+
27
+ gem install norman-disqus --source http://gems.github.com
28
+
29
+ === Use it:
30
+
31
+ ==== Configure it:
32
+
33
+ ===== Generic example:
34
+
35
+ Disqus::defaults[:account] = "my_disqus_account"
36
+ # Optional, only if you're using the API
37
+ Disqus::defaults[:api_key] = "my_disqus_api_key"
38
+
39
+ ===== Rails example:
40
+
41
+ # in config/development.rb (or production.rb, test.rb, etc.)
42
+ config.after_initialize do
43
+ Disqus::defaults[:account] = "my_disqus_account"
44
+ # Optional, only if you're using the API
45
+ Disqus::defaults[:api_key] = "my_disqus_api_key"
46
+ end
47
+
48
+ Note that here the "after initialize" is necessary, otherwise your settings
49
+ won't be set properly.
50
+
51
+ ===== Merb example:
52
+
53
+ # in config/init.rb
54
+ Merb::BootLoader.after_app_loads do
55
+ Disqus::defaults[:account] = "my_disqus_account"
56
+ # Optional, only if you're using the API
57
+ Disqus::defaults[:api_key] = "my_disqus_api_key"
58
+ end
59
+
60
+ Note that here the "after_app_loads" is necessary, otherwise your settings
61
+ won't be set properly.
62
+
63
+ ===== Options
64
+
65
+ :api_key => "" # your api key
66
+ :account => "", # your disqus account
67
+ :developer => false, # allows for threads to work on localhost
68
+ :container_id => 'disqus_thread', # desired thread container
69
+ :avatar_size => 48, # squared pixel size of avatars
70
+ :color => "grey", # theme color
71
+ :default_tab => "popular", # default widget tab
72
+ :hide_avatars => false, # hide or show avatars
73
+ :hide_mods => true, # hide or show moderation
74
+ :num_items => 15, # number of comments to display
75
+ :show_powered_by => true, # show or hide powered by line
76
+ :orientation => "horizontal" # comment orientation
77
+
78
+ ==== Show the comment threads widget on a post page:
79
+
80
+ # Loads the commenting system
81
+ disqus_thread
82
+
83
+ # Or if you're not using Rails/Merb:
84
+ Disqus::Widget::thread
85
+
86
+ # Sets the inner html to the comment count for any links on the page that
87
+ # have the anchor "disqus_thread". For example, "View Comments" below would
88
+ # be replaced by "1 comment" or "23 comments" etc.
89
+ # <a href="http://my.website/article-permalink#disqus_thread">View Comments</a>
90
+ # <a href="http://my.website/different-permalink#disqus_thread">View Comments</a>
91
+ disqus_comment_counts
92
+
93
+ # Or if you're not using Rails/Merb:
94
+ Disqus::Widget::comment_counts
95
+
96
+ ==== Show the combo widget on a post page:
97
+
98
+ disqus_combo(:color => "blue", :hide_mods => false, :num_items => 20)
99
+
100
+ # Or for non-Rails/Merb:
101
+ Disqus::Widget::combo(:color => "blue", :hide_mods => false, :num_items => 20)
102
+
103
+ ==== Show the comment count on a permalink:
104
+
105
+ link_to("Permalink", post_path(@post, :anchor => "disqus_thread"))
106
+ ...
107
+ disqus_comment_counts
108
+
109
+ # Or for non-Rails/Merb:
110
+ Disqus::Widget::comment_counts
111
+
112
+ ==== Work with the Disqus API:
113
+
114
+ See the Disqus::Api class for more info on the Disqus API. You can also read
115
+ the {Disqus developer info here}[http://disqus.com/docs/api/].
116
+
117
+ === Hack it:
118
+
119
+ Github repository:
120
+
121
+ http://github.com/norman/disqus
122
+
123
+ === Submit bug reports:
124
+
125
+ Please use our {Lighthouse}[http://randomba.lighthouseapp.com/projects/16065-disqus/].
126
+
127
+ === Learn more about Disqus:
128
+
129
+ {http://disqus.com}[http://disqus.com]
130
+
131
+ === Thanks to the following contributors:
132
+
133
+ * {Matt Van Horn}[http://github.com/mattvanhorn] - Disqus API
134
+ * {Quin Hoxie}[http://github.com/qhoxie] - Merb support
135
+
136
+ === Legal Stuff
137
+
138
+ The Disqus Ruby gem was not created by, nor is officially supported by
139
+ Disqus.com or Big Head Labs, Inc. Use it at your own risk and your own
140
+ responsibility under the terms of the MIT License.
141
+
142
+ Copyright (c) 2008 {Norman Clarke}[mailto:norman@randomba.org], released under
143
+ the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'hoe'
2
+ require 'newgem'
3
+ require 'lib/disqus/version'
4
+
5
+ $hoe = Hoe.new("disqus", Disqus::Version::STRING) do |p|
6
+ p.rubyforge_name = "nbudin-disqus"
7
+ p.author = ['Norman Clarke', 'Matthew Van Horn']
8
+ p.email = ['norman@randomba.org', 'mattvanhorn@gmail.com']
9
+ p.summary = "Integrates Disqus commenting system into your Ruby-powered site."
10
+ p.description = 'Integrates Disqus into your Ruby-powered site. Works with any Ruby website, and has view helpers for Rails and Merb.'
11
+ p.url = 'http://disqus.rubyforge.org'
12
+ p.test_globs = ['test/**/*_test.rb']
13
+ p.extra_deps << ['json']
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"],
16
+ ['mocha']
17
+ ]
18
+ p.rsync_args = '-av --delete --ignore-errors'
19
+ changes = p.paragraphs_of('History.txt').join("\n\n")
20
+ p.remote_rdoc_dir = ""
21
+ end
22
+
23
+ require 'newgem/tasks'
24
+ Dir['tasks/**/*.rake'].each { |t| load t }
@@ -0,0 +1,2 @@
1
+ host: compay@rubyforge.org
2
+ remote_dir: /var/www/gforge-projects/disqus
data/disqus.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "nbudin-disqus"
5
+ s.version = "1.0.2"
6
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
+ s.authors = ["Norman Clarke", "Matthew Van Horn"]
8
+ s.date = %q{2009-01-19}
9
+ s.description = %q{Integrates Disqus into your Ruby-powered site. Works with any Ruby website, and has view helpers for Rails and Merb.}
10
+ s.email = ["norman@randomba.org", "mattvanhorn@gmail.com"]
11
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.rdoc"]
12
+ s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.rdoc", "Rakefile", "config/website.yml", "disqus.gemspec", "init.rb", "lib/disqus.rb", "lib/disqus/api.rb", "lib/disqus/author.rb", "lib/disqus/forum.rb", "lib/disqus/post.rb", "lib/disqus/thread.rb", "lib/disqus/version.rb", "lib/disqus/view_helpers.rb", "lib/disqus/widget.rb", "tasks/rcov.rake", "test/api_test.rb", "test/config.yml.sample", "test/forum_test.rb", "test/merb_test.rb", "test/post_test.rb", "test/rails_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", "test/test_helper.rb", "test/thread_test.rb", "test/view_helpers_test.rb", "test/widget_test.rb"]
13
+ s.has_rdoc = true
14
+ s.homepage = %q{http://disqus.rubyforge.org}
15
+ s.require_paths = ["lib"]
16
+ s.rubyforge_project = %q{disqus}
17
+ s.rubygems_version = %q{1.3.1}
18
+ s.summary = %q{Integrates Disqus commenting system into your Ruby-powered site.}
19
+ s.test_files = ["test/rails_test.rb", "test/view_helpers_test.rb", "test/thread_test.rb", "test/post_test.rb", "test/api_test.rb", "test/widget_test.rb", "test/forum_test.rb", "test/merb_test.rb"]
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 2
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<json>, [">= 0"])
27
+ s.add_development_dependency(%q<newgem>, [">= 1.2.3"])
28
+ s.add_development_dependency(%q<mocha>, [">= 0"])
29
+ s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
30
+ else
31
+ s.add_dependency(%q<json>, [">= 0"])
32
+ s.add_dependency(%q<newgem>, [">= 1.2.3"])
33
+ s.add_dependency(%q<mocha>, [">= 0"])
34
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
35
+ end
36
+ else
37
+ s.add_dependency(%q<json>, [">= 0"])
38
+ s.add_dependency(%q<newgem>, [">= 1.2.3"])
39
+ s.add_dependency(%q<mocha>, [">= 0"])
40
+ s.add_dependency(%q<hoe>, [">= 1.8.0"])
41
+ end
42
+ end
data/init.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'disqus'
2
+ require 'disqus/api'
3
+ require 'disqus/author'
4
+ require 'disqus/forum'
5
+ require 'disqus/post'
6
+ require 'disqus/thread'
7
+ require 'disqus/version'
data/lib/disqus.rb ADDED
@@ -0,0 +1,72 @@
1
+ require 'disqus/widget'
2
+
3
+ # == From the {Disqus Website}[http://disqus.com]:
4
+
5
+ # "Disqus, pronounced "discuss", is a service and tool for web comments and
6
+ # discussions. The Disqus comment system can be plugged into any website, blog,
7
+ # or application. Disqus makes commenting easier and more interactive, while
8
+ # connecting websites and commenters across a thriving discussion community."
9
+ #
10
+ # "Disqus is a free service to the general public with absolutely no inline
11
+ # advertisements."
12
+
13
+ # The Disqus gem helps you quickly and easily integrate Disqus's Javascript
14
+ # widgets into your Ruby-based website. Adding Disqus to your site literally
15
+ # takes only a few minutes. The Disqus gem also provides a complete
16
+ # implementation of the Disqus API for more complex applications.
17
+
18
+ # To use this code, please first create an account on Disqus[http://disqus.com].
19
+ module Disqus
20
+
21
+ @defaults = {
22
+ :api_key => "",
23
+ :account => "",
24
+ :developer => false,
25
+ :container_id => 'disqus_thread',
26
+ :avatar_size => 48,
27
+ :color => "grey",
28
+ :default_tab => "popular",
29
+ :hide_avatars => false,
30
+ :hide_mods => true,
31
+ :num_items => 15,
32
+ :show_powered_by => true,
33
+ :orientation => "horizontal"
34
+ }
35
+
36
+ # Disqus defaults:
37
+ # :account => "",
38
+ # :avatar_size => 48,
39
+ # :color => "grey",
40
+ # :default_tab => "popular",
41
+ # :hide_avatars => false,
42
+ # :hide_mods => true,
43
+ # :num_items => 15,
44
+ # :show_powered_by => true,
45
+ # :orientation => "horizontal"
46
+ def self.defaults
47
+ @defaults
48
+ end
49
+
50
+ # Load the view helpers if the gem is included in a Rails app.
51
+ def self.enable_rails
52
+ return if ActionView::Base.instance_methods.include? 'disqus_thread'
53
+ require 'disqus/view_helpers'
54
+ ActionView::Base.class_eval { include Disqus::ViewHelpers }
55
+ end
56
+
57
+ # Load the view helpers if the gem is included in a Merb app.
58
+ def self.enable_merb
59
+ return if Merb::Controller.instance_methods.include? 'disqus_thread'
60
+ require 'disqus/view_helpers'
61
+ Merb::Controller.class_eval { include Disqus::ViewHelpers }
62
+ end
63
+
64
+ end
65
+
66
+ if defined?(Rails) and defined?(ActionView)
67
+ Disqus::enable_rails
68
+ end
69
+
70
+ if defined?(Merb)
71
+ Disqus::enable_merb
72
+ end
data/lib/disqus/api.rb ADDED
@@ -0,0 +1,264 @@
1
+ require 'open-uri'
2
+ require 'rubygems'
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'uri'
6
+
7
+ module Disqus
8
+
9
+ # == Disqus API
10
+ #
11
+ # The Api class implements the Disqus API directly. It is not really
12
+ # intended to be used directly, but rather to use the domain objects of
13
+ # Forum, Thread, Post, Author and AnonymousAuthor. For full information on
14
+ # the Disqus API, please see the {Disqus developer info}[http://disqus.com/docs/api/].
15
+ #
16
+ # Each method in the Api class takes as a single argument a hash of options,
17
+ # and returns a Hash with 3 keys:
18
+ #
19
+ # * 'succeeded' - contains true or false indicating whether the API call succeeded
20
+ # * 'code' - if the API call did not succeed, this will contain an error code.
21
+ # * 'message' - contains the object being returned on success, or an error message on failure.
22
+ #
23
+ # === API Keys
24
+ #
25
+ # There are two different kinds of API keys:
26
+ #
27
+ # ==== User Keys
28
+ #
29
+ # Every Disqus account has a User Key; it is used to perform actions
30
+ # associated with that account. This can be passed in as an option, or
31
+ # configured as follows:
32
+ #
33
+ # Disqus::defaults[:api_key] = "the_user_api_key"
34
+ #
35
+ # ==== Forum Keys
36
+ #
37
+ # Every Disqus forum has a Forum Key. It can be shared among trusted
38
+ # moderators of a forum, and is used to perform actions associated with that
39
+ # forum. The creator of a forum can get the forum's key through the API.
40
+ class Api
41
+
42
+ ROOT = 'http://disqus.com/api'
43
+
44
+ class << self
45
+
46
+ # Creates a new post on the thread. Does not check against spam filters or ban list.
47
+ # This is intended to allow automated importing of comments.
48
+ #
49
+ # Returns a Hash containing a representation of the post just created:
50
+ #
51
+ # Required options hash elements:
52
+ #
53
+ # * <tt>:forum_api_key</tt> - the API key for the forum
54
+ # * <tt>:thread_id</tt> - the thread to post to
55
+ # * <tt>:message</tt> - the content of the post
56
+ # * <tt>:author_name</tt> - the post creator's name
57
+ # * <tt>:author_email</tt> - the post creator's email address
58
+ #
59
+ # Optional:
60
+ #
61
+ # * <tt>:parent_post</tt> - the id of the parent post
62
+ # * <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)
63
+ # * <tt>:author_url</tt> - the author's homepage
64
+ # * <tt>:ip_address</tt> - the author's IP address
65
+ def create_post(opts = {})
66
+ opts[:api_key] ||= Disqus::defaults[:api_key]
67
+ post_data = {
68
+ :forum_api_key => opts[:forum_api_key],
69
+ :thread_id => opts[:thread_id],
70
+ :message => opts[:message],
71
+ :author_name => opts[:author_name],
72
+ :author_email => opts[:author_email]
73
+ }
74
+ [:parent_post, :created_at, :author_url, :ip_address].each do |key|
75
+ post_data[key] = opts[key] if opts[key]
76
+ end
77
+ JSON.parse(post('create_post/', post_data))
78
+ end
79
+
80
+ # Returns an array of hashes representing all forums the user owns. The
81
+ # user is determined by the API key.
82
+ #
83
+ # Options:
84
+ #
85
+ # * <tt>:api_key</tt> - The User's API key (defaults to
86
+ # Disqus::defaults[:api_key])
87
+ def get_forum_list(opts = {})
88
+ opts[:api_key] ||= Disqus::defaults[:api_key]
89
+ JSON.parse(get('get_forum_list', :user_api_key => opts[:api_key]))
90
+ end
91
+
92
+ # Returns A string which is the Forum Key for the given forum.
93
+ #
94
+ # Required options hash elements:
95
+ #
96
+ # * <tt>:forum_id</tt> - the unique id of the forum
97
+ #
98
+ # Optional:
99
+ #
100
+ # * <tt>:api_key</tt> - The User's API key (defaults to Disqus::defaults[:api_key])
101
+ def get_forum_api_key(opts = {})
102
+ opts[:api_key] ||= Disqus::defaults[:api_key]
103
+ JSON.parse(get('get_forum_api_key', :user_api_key => opts[:api_key], :forum_id => opts[:forum_id]))
104
+ end
105
+
106
+ # Returns: An array of hashes representing all threads belonging to the
107
+ # given forum.
108
+ #
109
+ # Required options hash elements:
110
+ #
111
+ # * <tt>:forum_api_key</tt> - the API key for the forum
112
+ # * <tt>:forum_id</tt> - the unique id of the forum
113
+ def get_thread_list(opts = {})
114
+ JSON.parse(get('get_thread_list', :forum_id => opts[:forum_id], :forum_api_key => opts[:forum_api_key]))
115
+ end
116
+
117
+ # Returns a hash having thread_ids as keys and 2-element arrays as
118
+ # values.
119
+ #
120
+ # The first array element is the number of visible comments on on the
121
+ # thread; this would be useful for showing users of the site (e.g., "5
122
+ # Comments").
123
+ #
124
+ # The second array element is the total number of comments on the
125
+ # thread.
126
+ #
127
+ # These numbers are different because some forums require moderator
128
+ # approval, some messages are flagged as spam, etc.
129
+ #
130
+ # Required options hash elements:
131
+ #
132
+ # * <tt>:forum_api_key</tt> - the API key for the forum
133
+ # * <tt>:thread_ids</tt> - an array of thread IDs belonging to the given forum.
134
+ def get_num_posts(opts = {})
135
+ opts[:api_key] ||= Disqus::defaults[:api_key]
136
+ JSON.parse(get('get_num_posts', :thread_ids => opts[:thread_ids].join(","), :forum_api_key => opts[:forum_api_key]))
137
+ end
138
+
139
+ # Returns a hash representing a thread if one was found, otherwise null.
140
+ #
141
+ # It only finds threads associated with the given forum.
142
+ #
143
+ # Note that there is no one-to-one mapping between threads and URL's; a
144
+ # thread will only have an associated URL if it was automatically
145
+ # created by Disqus javascript embedded on that page. Therefore, we
146
+ # recommend using thread_by_identifier whenever possible. This method is
147
+ # provided mainly for handling comments from before your forum was using
148
+ # the API.
149
+ #
150
+ # Required options hash elements:
151
+ #
152
+ # * <tt>:forum_api_key</tt> - the API key for the forum
153
+ # * <tt>:url</tt> - the URL to check for an associated thread
154
+ def get_thread_by_url(opts = {})
155
+ JSON.parse(get('get_thread_by_url', :url => opts[:url], :forum_api_key => opts[:forum_api_key]))
156
+ end
157
+
158
+ # Returns an array of hashes representing representing all posts
159
+ # belonging to the given forum.
160
+ #
161
+ # Required options hash elements:
162
+ #
163
+ # * <tt>:forum_api_key</tt> - the API key for the forum
164
+ # * <tt>:thread_id</tt> - the ID of a thread belonging to the given forum
165
+ def get_thread_posts(opts = {})
166
+ JSON.parse(get('get_thread_posts', :thread_id => opts[:thread_id], :forum_api_key => opts[:forum_api_key]))
167
+ end
168
+
169
+ # Create or retrieve a thread by an arbitrary identifying string of your
170
+ # choice. For example, you could use your local database's ID for the
171
+ # thread. This method allows you to decouple thread identifiers from the
172
+ # URL's on which they might be appear. (Disqus would normally use a
173
+ # thread's URL to identify it, which is problematic when URL's do not
174
+ # uniquely identify a resource.) If no thread exists for the given
175
+ # identifier yet (paired with the forum), one will be created.
176
+ #
177
+ # Returns a hash with two keys:
178
+ #
179
+ # * "thread", which is a hash representing the thread corresponding to the identifier; and
180
+ # * "created", which indicates whether the thread was created as a result of this method call. If created, it will have the specified title.
181
+ #
182
+ # Required options hash elements:
183
+ #
184
+ # * <tt>:forum_api_key</tt> - the API key for the forum
185
+ # * <tt>:title</tt> - the title of the thread to possibly be created
186
+ # * <tt>:identifier</tt> - a string of your choosing
187
+ def thread_by_identifier(opts = {})
188
+ JSON.parse(post('thread_by_identifier', :forum_api_key => opts[:forum_api_key],
189
+ :identifier => opts[:identifier],
190
+ :title => opts[:title] ))
191
+ end
192
+
193
+ # Sets the provided values on the thread object.
194
+ #
195
+ # Returns an empty success message.
196
+ #
197
+ # Required options hash elements:
198
+ #
199
+ # * <tt>:forum_api_key</tt> - the API key for the forum
200
+ # * <tt>:thread_id</tt> - the ID of a thread belonging to the given forum
201
+ #
202
+ # Optional:
203
+ #
204
+ # * <tt>:title</tt> - the title of the thread
205
+ # * <tt>:slug</tt> - the per-forum-unique string used for identifying this thread in disqus.com URL's relating to this thread. Composed of underscore-separated alphanumeric strings.
206
+ # * <tt>:url</tt> - the URL this thread is on, if known.
207
+ # * <tt>:allow_comment</tt> - whether this thread is open to new comments
208
+ def update_thread(opts = {})
209
+ raise opts.inspect
210
+ JSON.parse(post('update_thread',
211
+ :forum_api_key => opts[:forum_api_key],
212
+ :thread_id => opts[:thread_id],
213
+ :title => opts[:title],
214
+ :slug => opts[:slug],
215
+ :url => opts[:url],
216
+ :allow_comments => opts[:allow_comments])
217
+ )
218
+ end
219
+
220
+ # Widget to includes a comment form suitable for use with the Disqus
221
+ # API. This is different from the other widgets in that you can specify
222
+ # the thread identifier being commented on.
223
+ def comment_form(forum_shortname, thread_identifier)
224
+ url = 'http://disqus.com/api/reply.js?' +
225
+ "forum_shortname=#{escape(forum_shortname)}&" +
226
+ "thread_identifier=#{escape(thread_identifier)}"
227
+ s = '<div id="dsq-reply">'
228
+ s << '<script type="text/javascript" src="%s"></script>' % url
229
+ s << '</div>'
230
+ return s
231
+ end
232
+
233
+ private
234
+
235
+ def escape(string)
236
+ URI::encode(string, /[^a-z0-9]/i)
237
+ end
238
+
239
+ def get(*args)
240
+ open(make_url(*args)) {|u| u.read }
241
+ end
242
+
243
+ def post(*args)
244
+ url = ROOT + '/' + args.shift
245
+ post_params = {}
246
+ args.shift.each { |k, v| post_params[k.to_s]=v.to_s }
247
+ Net::HTTP.post_form(URI.parse(url),post_params).body
248
+ end
249
+
250
+ def make_url(*args)
251
+ url = ROOT + '/' + args.shift + '/?'
252
+ args.shift.each { |k, v| url += "#{k}=#{escape(v.to_s)}&" }
253
+ return url.chomp('&')
254
+ end
255
+
256
+ def validate_opts!(opts)
257
+ raise ArgumentError.new("You must specify an :api_key") if !opts[:api_key]
258
+ end
259
+
260
+ end
261
+
262
+ end
263
+
264
+ end