Class: Wikidotrb::Module::ForumCategory

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site:, id:, forum:, title: nil, description: nil, group: nil, threads_counts: nil, posts_counts: nil, pagerno: nil, last_thread_id: nil, last_post_id: nil) ⇒ ForumCategory

Returns a new instance of ForumCategory.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/wikidotrb/module/forum_category.rb', line 84

def initialize(site:, id:, forum:, title: nil, description: nil, group: nil, threads_counts: nil,
               posts_counts: nil, pagerno: nil, last_thread_id: nil, last_post_id: nil)
  @site = site
  @id = id
  @forum = forum
  @title = title
  @description = description
  @group = group
  @threads_counts = threads_counts
  @posts_counts = posts_counts
  @pagerno = pagerno
  @_last_thread_id = last_thread_id
  @_last_post_id = last_post_id
  @_last = nil
end

Instance Attribute Details

#description

Returns the value of attribute description.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def description
  @description
end

#forum

Returns the value of attribute forum.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def forum
  @forum
end

#group

Returns the value of attribute group.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def group
  @group
end

#id

Returns the value of attribute id.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def id
  @id
end

#last

最後の投稿を取得



111
112
113
# File 'lib/wikidotrb/module/forum_category.rb', line 111

def last
  @last
end

#pagerno

Returns the value of attribute pagerno.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def pagerno
  @pagerno
end

#posts_counts

Returns the value of attribute posts_counts.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def posts_counts
  @posts_counts
end

#site

Returns the value of attribute site.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def site
  @site
end

#threads_counts

Returns the value of attribute threads_counts.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def threads_counts
  @threads_counts
end

#title

Returns the value of attribute title.



81
82
83
# File 'lib/wikidotrb/module/forum_category.rb', line 81

def title
  @title
end

Instance Method Details

#get_url

カテゴリのURLを取得



101
102
103
# File 'lib/wikidotrb/module/forum_category.rb', line 101

def get_url
  "#{@site.get_url}/forum/c-#{@id}"
end

#new_thread(title:, source:, description: "")

新しいスレッドを作成



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/wikidotrb/module/forum_category.rb', line 164

def new_thread(title:, source:, description: "")
  client = @site.client
  client.

  response = @site.amc_request(
    bodies: [
      {
        "category_id" => @id,
        "title" => title,
        "description" => description,
        "source" => source,
        "action" => "ForumAction",
        "event" => "newThread"
      }
    ]
  ).first

  body = JSON.parse(response.body.to_s)

  ForumThread.new(
    site: @site,
    id: body["threadId"].to_i,
    forum: @forum,
    category: self,
    title: title,
    description: description,
    created_by: client.user.get(client.username),
    created_at: DateTime.parse(body["CURRENT_TIMESTAMP"]),
    posts_counts: 1
  )
end

#threadsForumThreadCollection

スレッドのコレクションを取得する

Returns:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/wikidotrb/module/forum_category.rb', line 123

def threads
  client = @site.client
  update
  responses = @site.amc_request(
    bodies: (1..@pagerno).map { |no| { "p" => no, "c" => @id, "moduleName" => "forum/ForumViewCategoryModule" } }
  )

  threads = []

  responses.each do |response|
    html = Nokogiri::HTML(response.body.to_s)
    html.css("table.table tr.head~tr").each do |info|
      title = info.at_css("div.title a")
      thread_id = title["href"].match(/t-(\d+)/)[1].to_i
      description = info.at_css("div.description").text.strip
      user = info.at_css("span.printuser")
      odate = info.at_css("span.odate")
      posts_count = info.at_css("td.posts").text.to_i
      last_id = info.at_css("td.last>a")
      post_id = last_id.nil? ? nil : last_id["href"].match(/post-(\d+)/)[1].to_i

      thread = ForumThread.new(
        site: @site,
        id: thread_id,
        forum: @forum,
        title: title.text.strip,
        description: description,
        created_by: user_parser(client, user),
        created_at: odate_parser(odate),
        posts_counts: posts_count,
        _last_post_id: post_id
      )

      threads << thread
    end
  end

  ForumThreadCollection.new(forum: @forum, threads: threads)
end

#update

カテゴリを更新



106
107
108
# File 'lib/wikidotrb/module/forum_category.rb', line 106

def update
  ForumCategoryCollection.new(forum: @forum, categories: [self]).update.first
end