Class: Trefoil::Board

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#nameString (readonly)

Returns The name of the board being referenced.

Returns:

  • (String)

    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`

Parameters:

  • key (Symbol)

    The key to the desired data.

Returns:

  • (String, Integer, Hash<Symbol => Integer>, nil)


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

#archiveArray<Integer>

Retrive an array of archived thread ids.

Returns:

  • (Array<Integer>)


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

Returns:

  • (true, false)


30
31
32
# File 'lib/trefoil/board.rb', line 30

def archived?
  self[:archived] == 1
end

#catalogArray<Hash<Symbol => Integer, String>>

Return a list of all active ops

Returns:

  • (Array<Hash<Symbol => Integer, String>>)


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

Parameters:

  • int (Integer, nil) (defaults to: nil)

    the custom spoiler id to use pics one randomly if left nil.

Returns:

  • (String, nil)

    The url to a spoiler, or nil if this board does not have custom spoilers.



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

Returns:

  • (true, false)


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.

Parameters:

  • id (Integer)

    Thread id.



42
43
44
# File 'lib/trefoil/board.rb', line 42

def thread(id)
  Thread.new(client, self, id)
end

#threadsHash<Integer => Integer>

Get a hash of threads and their last modified time

Returns:

  • (Hash<Integer => Integer>)

    A hash with thread ids as keys and their last modified time as the corresponding value.



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_catalogObject

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