Class Disqus::Forum

  1. lib/disqus/forum.rb
Parent: Object

Attributes

created_at [R]
id [R]
name [R]
shortname [R]
threads [R]

Public class methods

find (forum_id, user_api_key = nil)

Returns a Forum object corresponding to the given forum_id or nil if it was not found.

[show source]
# File lib/disqus/forum.rb, line 31
    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
list (user_api_key = nil)

Returns an array of Forum objects belonging to the user indicated by the API key.

[show source]
# File lib/disqus/forum.rb, line 20
    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
new (id, shortname, name, created_at, include_threads = false)
[show source]
# File lib/disqus/forum.rb, line 6
    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
      @forum_threads = include_threads ? load_threads : []
    end

Public instance methods

== (other_forum)
[show source]
# File lib/disqus/forum.rb, line 12
    def ==(other_forum)
      id        == other_forum.id        &&
      shortname == other_forum.shortname &&
      name      == other_forum.name      &&
      key       == other_forum.key
    end
forum_threads (force_update = false)

Returns an array of threads belonging to this forum.

[show source]
# File lib/disqus/forum.rb, line 45
    def forum_threads(force_update = false)
      if (@forum_threads.nil? or @forum_threads.empty? or force_update)
        @forum_threads = Disqus::Thread.list(self)
      end
      @forum_threads
    end
get_thread_by_url (url)

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.

[show source]
# File lib/disqus/forum.rb, line 56
    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
key (user_api_key = nil)

Returns the forum API Key for this forum.

[show source]
# File lib/disqus/forum.rb, line 40
    def key(user_api_key = nil)
      @key ||= load_key(user_api_key)
    end
thread_by_identifier (identifier, title)

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 URL’s on which they might be appear. (Disqus would normally use a thread’s URL to identify it, which is problematic when URL’s do not uniquely identify a resource.) If no thread exists for the given identifier (paired with the forum) yet, one will be created.

Returns a Thread object representing the thread that was created or retrieved.

[show source]
# File lib/disqus/forum.rb, line 76
    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
update_thread (thread_id, opts = {})

Sets the provided values on the thread object.

Returns an empty success message.

Options:

  • :title - the title of the thread
  • :slug - 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.
  • :url - the URL this thread is on, if known.
  • :allow_comment - whether this thread is open to new comments
[show source]
# File lib/disqus/forum.rb, line 97
    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