Class: BBS2ch::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/bbs2ch/client.rb

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



6
7
8
# File 'lib/bbs2ch/client.rb', line 6

def initialize
  load_menu
end

Instance Method Details

#boards(regex = nil) ⇒ Object



17
18
19
20
21
22
# File 'lib/bbs2ch/client.rb', line 17

def boards(regex = nil)
  load_menu unless @boards
  return @boards unless regex

  @boards.select { |board| board.name.match(regex) }
end

#categories(regex = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/bbs2ch/client.rb', line 10

def categories(regex = nil)
  load_menu unless @categories
  return @categories unless regex

  @categories.select { |category| category.name.match(regex) }
end

#load_menuObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bbs2ch/client.rb', line 24

def load_menu
  body = Connection.new('http://menu.2ch.net/bbsmenu.html').response_body

  start_pos = body.index('<BR><BR><B>')
  body = body[start_pos..-1].gsub(/<!--.*-->/m, '')

  @categories = []
  @boards = []

  category = nil
  body.each_line do |line|
    line.chomp!

    if line.match('<BR><BR><B>(.+)</B><BR>')
      name = $1
      category = Category.new(name)
    elsif line.match('<A HREF=(.+)/>(.+)</A>')
      name = $2
      url  = $1
      board = Board.new(name, url)
      category.boards << board
      @boards         << board
    elsif line.length == 0
      @categories     << category
    else
      # break
    end
  end

  self
end