Class: Disqus::Forum

Inherits:
Object
  • Object
show all
Defined in:
lib/disqus/forum.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, shortname, name, created_at, include_threads = false) ⇒ Forum

Returns a new instance of Forum.



6
7
8
9
10
# 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

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



4
5
6
# File 'lib/disqus/forum.rb', line 4

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/disqus/forum.rb', line 4

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/disqus/forum.rb', line 4

def name
  @name
end

#shortnameObject (readonly)

Returns the value of attribute shortname.



4
5
6
# File 'lib/disqus/forum.rb', line 4

def shortname
  @shortname
end

#threadsObject (readonly)

Returns the value of attribute threads.



4
5
6
# File 'lib/disqus/forum.rb', line 4

def threads
  @threads
end

Class Method Details

.find(forum_id, user_api_key = nil) ⇒ Object

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



31
32
33
34
35
36
37
# 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) ⇒ Object

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



20
21
22
23
24
25
26
27
28
# 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

Instance Method Details

#==(other_forum) ⇒ Object



12
13
14
15
16
17
# 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) ⇒ Object

Returns an array of threads belonging to this forum.



45
46
47
48
49
50
# 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) ⇒ Object

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.



56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

Returns the forum API Key for this forum.



40
41
42
# 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) ⇒ Object

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.



76
77
78
79
80
81
82
83
84
85
# 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 = {}) ⇒ Object

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



97
98
99
100
101
102
103
104
105
106
107
# 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