Class: TallyGem::Adapters::Xenforo

Inherits:
Object
  • Object
show all
Defined in:
lib/tallygem/adapters/xenforo.rb

Constant Summary collapse

POSTS_PER_PAGE =
25

Instance Method Summary collapse

Constructor Details

#initialize(url, start_num: 1, end_num: nil, last_threadmark: false) ⇒ Xenforo

Returns a new instance of Xenforo.



10
11
12
13
14
15
16
# File 'lib/tallygem/adapters/xenforo.rb', line 10

def initialize(url, start_num: 1, end_num: nil, last_threadmark: false)
  @url             = url
  @start_num       = start_num
  @end_num         = end_num
  @last_threadmark = last_threadmark
  @thread_url      = base_thread_url(@url)
end

Instance Method Details

#postsObject



18
19
20
21
22
23
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
55
56
57
58
59
60
# File 'lib/tallygem/adapters/xenforo.rb', line 18

def posts
  start_page_num = start_page_num()
  first_page     = Nokogiri::XML(open("#{@thread_url}/page-#{start_page_num}"))

  # 'Page x of y' => Int(y)
  end_page_num =
    if @end_num.nil?
      first_page.css('.pageNavHeader').text.split.last.to_i
    else
      page_num(@end_num)
    end

  page_urls = (start_page_num..end_page_num).collect {|page_num| "#{@thread_url}/page-#{page_num}"}

  page_posts = page_urls.collect do |url|
    page = Nokogiri::XML(open(url))
    page.css('.messageList .message').collect do |message|
      # replace imgs with alt-text
      message.css('img').each {|img| img.replace((img['alt']).to_s)}
      # remove quotes
      message.css('.bbCodeQuote').remove
      # remove spoilers
      message.css('.bbCodeSpoilerContainer').remove

      number = message.css('.postNumber').text.delete('#').to_i
      next if number < @start_num || number > (@end_num || end_page_num * POSTS_PER_PAGE)

      id_str = message['id']
      id     = id_str =~ /post-([0-9]+)/ ? $~.captures.first.to_i : id_str
      author = message['data-author']
      text   = message.css('.messageContent article .messageText').text.strip
      TallyGem::Post.new(id, number, author, text)
    end
  end

  # flatten array of page-arrays of posts down to just an array of valid posts
  posts = page_posts.flatten.reject(&:nil?)

  # set the end_num if it's nil
  @end_num ||= posts.last.number

  posts
end

#to_sObject



62
63
64
65
# File 'lib/tallygem/adapters/xenforo.rb', line 62

def to_s
  title = Nokogiri::XML(open(@url)).css('title').text
  "#{title}, Posts: #{@start_num}-#{@end_num}"
end