class Trefoil::Board
Class representing a board. Used for requesting threads. A list of all ids can be found with `#threads`. An array of all active Threads can be obtained through `#catalog`. A list of archived thread ids can be found with `#archive.`
Attributes
@return [String] The name of the board being referenced.
Public Class Methods
# File lib/trefoil/board.rb, line 12 def initialize(client, name) @client = client @name = name @catalog = [] end
Public Instance Methods
Get info about this board. Keys based on `/boards.json` @param key [Symbol] The key to the desired data. @return [String, Integer, Hash<Symbol => Integer>, nil]
# File lib/trefoil/board.rb, line 21 def [](key) @client.send(:cache_boards) if board_cache.empty? return nil unless board_cache[name] board_cache[name][key] end
Retrive an array of archived thread ids. @return [Array<Integer>]
# File lib/trefoil/board.rb, line 63 def archive @client.get("#{name}/archive.json") end
Whether or not this board is archived @return [true, false]
# File lib/trefoil/board.rb, line 30 def archived? self[:archived] == 1 end
Return a list of all active ops @return [Array<Hash<Symbol => Integer, String>>]
# File lib/trefoil/board.rb, line 69 def catalog return @catalog unless @catalog.empty? update_catalog @catalog end
Link to a board's custom spoiler @param int [Integer, nil] the custom spoiler id to use pics one randomly if left nil. @return [String, nil] The url to a spoiler, or nil if this board does not have custom spoilers.
# File lib/trefoil/board.rb, line 90 def custom_spoiler(int = nil) return nil unless self[:custom_spoilers] spoiler_id = int || rand(self[:custom_spoilers]) + 1 "http://s.4cdn.org/image/spoiler-#{name}#{spoiler_id}.png" end
Whether or not this board is marked as not safe for work @return [true, false]
# File lib/trefoil/board.rb, line 36 def nsfw? self[:ws_board].zero? end
Gets a new thread by id. @param id [Integer] Thread
id.
# File lib/trefoil/board.rb, line 42 def thread(id) Thread.new(client, self, id) end
Get a hash of threads and their last modified time @return [Hash<Integer => Integer>] A hash with thread ids as keys and their
last modified time as the corresponding value.
# File lib/trefoil/board.rb, line 49 def threads thread_list = {} pages = @client.get("#{name}/threads.json") pages.each do |page| page[:threads].each do |thread| thread_list[thread[:no]] = thread[:last_modified] end end thread_list end
Update the catalog with new ops
# File lib/trefoil/board.rb, line 77 def update_catalog @catalog = [] pages = @client.get("#{name}/catalog.json") pages.each do |page| page[:threads].each do |op| @catalog << ThreadStub.new(@client, self, op[:no], op) end end end
Private Instance Methods
Proxy method for accessing the client's board cache @return [Hash<String => Hash<Symbol => String, Integer, Hash<String => Integer>>>]
# File lib/trefoil/board.rb, line 102 def board_cache @client.instance_variable_get(:@board_cache) end