Class: Wikidotrb::Module::ForumPostCollection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread:, posts: []) ⇒ ForumPostCollection

Initialization method

Parameters:

  • thread (ForumThread)

    Thread object

  • posts (Array<ForumPost>) (defaults to: [])

    List of post objects



15
16
17
18
# File 'lib/wikidotrb/module/forum_post.rb', line 15

def initialize(thread:, posts: [])
  super(posts)
  @thread = thread
end

Instance Attribute Details

#thread

Returns the value of attribute thread.



10
11
12
# File 'lib/wikidotrb/module/forum_post.rb', line 10

def thread
  @thread
end

Class Method Details

.acquire_parent_post(thread:, posts:) ⇒ Array<ForumPost>

Retrieve and set parent post

Parameters:

Returns:

  • (Array<ForumPost>)

    Updated list of posts



31
32
33
34
35
36
# File 'lib/wikidotrb/module/forum_post.rb', line 31

def self.acquire_parent_post(thread:, posts:)
  return posts if posts.empty?

  posts.each { |post| post.parent = thread.get(post.parent_id) }
  posts
end

.acquire_post_info(thread:, posts:) ⇒ Array<ForumPost>

Retrieve and set post information

Parameters:

Returns:

  • (Array<ForumPost>)

    Updated list of posts



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wikidotrb/module/forum_post.rb', line 47

def self.(thread:, posts:)
  return posts if posts.empty?

  responses = thread.site.amc_request(
    bodies: posts.map do |post|
      {
        "postId" => post.id,
        "threadId" => thread.id,
        "moduleName" => "forum/sub/ForumEditPostFormModule"
      }
    end
  )

  responses.each_with_index do |response, index|
    html = Nokogiri::HTML(response.body.to_s)
    title = html.at_css("input#np-title")&.text&.strip
    source = html.at_css("textarea#np-text")&.text&.strip
    posts[index].title = title
    posts[index].source = source
  end

  posts
end

Instance Method Details

#find(target_id) ⇒ ForumPost?

Search for a post by ID

Parameters:

  • target_id (Integer)

    Post ID

Returns:

  • (ForumPost, nil)

    ForumPost object if the post is found, nil otherwise



23
24
25
# File 'lib/wikidotrb/module/forum_post.rb', line 23

def find(target_id)
  find { |post| post.id == target_id }
end

#get_parent_post

Retrieve parent post for revisions



39
40
41
# File 'lib/wikidotrb/module/forum_post.rb', line 39

def get_parent_post
  ForumPostCollection.acquire_parent_post(thread: @thread, posts: self)
end

#get_post_info

Retrieve post information for revisions



72
73
74
# File 'lib/wikidotrb/module/forum_post.rb', line 72

def 
  ForumPostCollection.(thread: @thread, posts: self)
end