Class: Wikidotrb::Module::ForumThreadCollection

Inherits:
Array
  • Object
show all
Defined in:
lib/wikidotrb/module/forum_thread.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(forum:, threads: []) ⇒ ForumThreadCollection

初期化メソッド

Parameters:

  • forum (Forum)

    フォーラムオブジェクト

  • threads (Array<ForumThread>) (defaults to: [])

    スレッドのリスト



16
17
18
19
# File 'lib/wikidotrb/module/forum_thread.rb', line 16

def initialize(forum:, threads: [])
  super(threads)
  @forum = forum
end

Instance Attribute Details

#forum

Returns the value of attribute forum.



11
12
13
# File 'lib/wikidotrb/module/forum_thread.rb', line 11

def forum
  @forum
end

Class Method Details

.acquire_update(forum:, threads:) ⇒ Array<ForumThread>

スレッド情報を取得して更新する

Parameters:

  • forum (Forum)

    フォーラムオブジェクト

  • threads (Array<ForumThread>)

    スレッドのリスト

Returns:

  • (Array<ForumThread>)

    更新されたスレッドのリスト



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
61
62
63
64
65
# File 'lib/wikidotrb/module/forum_thread.rb', line 25

def self.acquire_update(forum:, threads:)
  return threads if threads.empty?

  client = forum.site.client
  responses = forum.site.amc_request(
    bodies: threads.map { |thread| { "t" => thread.id, "moduleName" => "forum/ForumViewThreadModule" } }
  )

  responses.each_with_index do |response, index|
    thread = threads[index]
    html = Nokogiri::HTML(response.body.to_s)
    statistics = html.at_css("div.statistics")
    user = statistics.at_css("span.printuser")
    odate = statistics.at_css("span.odate")
    category_url = html.css("div.forum-breadcrumbs a")[1]["href"]
    category_id = category_url.match(/c-(\d+)/)[1]
    title = html.at_css("div.forum-breadcrumbs").text.strip
    counts = statistics.text.scan(/\n.+\D(\d+)/).last.first.to_i

    thread.title = title.match(/»([ \S]*)$/)[1].strip
    thread.category = thread.forum.category.get(category_id.to_i)
    description_block = html.at_css("div.description-block div.head")
    thread.description = description_block.nil? ? "" : html.at_css("div.description-block").text.strip.match(/[ \S]+$/).to_s

    thread.last = nil if thread.posts_counts != counts
    thread.posts_counts = counts
    thread.created_by = user_parser(client, user)
    thread.created_at = odate_parser(odate)

    pager_no = html.at_css("span.pager-no")
    thread.pagerno = pager_no.nil? ? 1 : pager_no.text.match(/of (\d+)/)[1].to_i

    page_ele = html.at_css("div.description-block>a")
    if page_ele
      thread.page = thread.site.page.get(page_ele["href"][1..])
      thread.page.discuss = thread
    end
  end

  threads
end

Instance Method Details

#update

スレッドを更新する



68
69
70
# File 'lib/wikidotrb/module/forum_thread.rb', line 68

def update
  ForumThreadCollection.acquire_update(forum: @forum, threads: self)
end