Class: Trefoil::Board
- Inherits:
-
Object
- Object
- Trefoil::Board
- Defined in:
- lib/trefoil/board.rb
Overview
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.`
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
The name of the board being referenced.
Instance Method Summary collapse
-
#[](key) ⇒ String, ...
Get info about this board.
-
#archive ⇒ Array<Integer>
Retrive an array of archived thread ids.
-
#archived? ⇒ true, false
Whether or not this board is archived.
-
#catalog ⇒ Array<Hash<Symbol => Integer, String>>
Return a list of all active ops.
-
#custom_spoiler(int = nil) ⇒ String?
Link to a board’s custom spoiler.
-
#initialize(client, name) ⇒ Board
constructor
A new instance of Board.
-
#nsfw? ⇒ true, false
Whether or not this board is marked as not safe for work.
-
#thread(id) ⇒ Object
Gets a new thread by id.
-
#threads ⇒ Hash<Integer => Integer>
Get a hash of threads and their last modified time.
-
#update_catalog ⇒ Object
Update the catalog with new ops.
Constructor Details
#initialize(client, name) ⇒ Board
Returns a new instance of Board.
12 13 14 15 16 |
# File 'lib/trefoil/board.rb', line 12 def initialize(client, name) @client = client @name = name @catalog = [] end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns The name of the board being referenced.
10 11 12 |
# File 'lib/trefoil/board.rb', line 10 def name @name end |
Instance Method Details
#[](key) ⇒ String, ...
Get info about this board. Keys based on ‘/boards.json`
21 22 23 24 25 26 |
# 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 |
#archive ⇒ Array<Integer>
Retrive an array of archived thread ids.
63 64 65 |
# File 'lib/trefoil/board.rb', line 63 def archive @client.get("#{name}/archive.json") end |
#archived? ⇒ true, false
Whether or not this board is archived
30 31 32 |
# File 'lib/trefoil/board.rb', line 30 def archived? self[:archived] == 1 end |
#catalog ⇒ Array<Hash<Symbol => Integer, String>>
Return a list of all active ops
69 70 71 72 73 74 |
# File 'lib/trefoil/board.rb', line 69 def catalog return @catalog unless @catalog.empty? update_catalog @catalog end |
#custom_spoiler(int = nil) ⇒ String?
Link to a board’s custom spoiler
90 91 92 93 94 95 96 |
# 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 |
#nsfw? ⇒ true, false
Whether or not this board is marked as not safe for work
36 37 38 |
# File 'lib/trefoil/board.rb', line 36 def nsfw? self[:ws_board].zero? end |
#thread(id) ⇒ Object
Gets a new thread by id.
42 43 44 |
# File 'lib/trefoil/board.rb', line 42 def thread(id) Thread.new(client, self, id) end |
#threads ⇒ Hash<Integer => Integer>
Get a hash of threads and their last modified time
49 50 51 52 53 54 55 56 57 58 59 |
# 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_catalog ⇒ Object
Update the catalog with new ops
77 78 79 80 81 82 83 84 85 |
# 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 << op end end end |