repltalk 2.0.4 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,472 @@
1
+ require "http"
2
+ require "json"
3
+
4
+ module ReplTalk
5
+ class Role
6
+ attr_reader :name, :key, :tagline
7
+
8
+ def initialize(role)
9
+ @name = role["name"]
10
+ @key = role["key"]
11
+ @tagline = role["tagline"]
12
+ end
13
+
14
+ def to_s
15
+ @name
16
+ end
17
+ end
18
+
19
+
20
+
21
+ class Organization
22
+ attr_reader :id, :name, :country, :postal_code, :state, :city, :timestamp
23
+
24
+ def initialize(organization)
25
+ @id = organization["id"]
26
+ @name = organization["name"]
27
+ @country = organization["country"]
28
+ @postal_code = organization["postalCode"]
29
+ @state = organization["state"]
30
+ @city = organization["city"]
31
+ @timestamp = organization["timeCreated"]
32
+ end
33
+
34
+ def to_s
35
+ @name
36
+ end
37
+ end
38
+
39
+
40
+
41
+ class Language
42
+ attr_reader :id, :key, :name, :tagline, :icon, :category
43
+
44
+ def initialize(lang)
45
+ @id = lang["id"]
46
+ @key = lang["key"]
47
+ @name = lang["displayName"]
48
+ @tagline = lang["tagline"]
49
+ @icon = lang["icon"]
50
+ @category = lang["category"]
51
+ end
52
+
53
+ def to_s
54
+ @id
55
+ end
56
+ end
57
+
58
+
59
+
60
+ class Board
61
+ attr_reader :id, :name, :color, :description
62
+
63
+ def initialize(board)
64
+ @id = board["id"]
65
+ @name = board["name"]
66
+ @color = board["color"]
67
+ @description = board["description"]
68
+ end
69
+
70
+ def to_s
71
+ @name
72
+ end
73
+ end
74
+
75
+
76
+
77
+ class ReplComment
78
+ attr_reader :id, :content, :author, :repl, :replies
79
+
80
+ def initialize(client, comment)
81
+ @client = client
82
+
83
+ @id = comment["id"]
84
+ @content = comment["body"]
85
+ @author = comment["user"] == nil ? nil : User.new(@client, comment["user"])
86
+ @repl = comment["repl"] == nil ? nil : Repl.new(@client, comment["repl"])
87
+ @replies = comment["replies"] == nil ? nil : comment["replies"].map { |c| ReplComment.new(@client, c) }
88
+ end
89
+
90
+ def create_comment(content)
91
+ c = @client.graphql(
92
+ "ReplViewCreateReplCommentReply",
93
+ GQL::Mutations::REPLY_REPL_COMMENT,
94
+ input: {
95
+ replCommentId: @id,
96
+ body: content
97
+ }
98
+ )
99
+ ReplComment.new(@client, c["createReplCommentReply"])
100
+ end
101
+
102
+ def edit(content)
103
+ c = @client.graphql(
104
+ "ReplViewCommentsUpdateReplComment",
105
+ GQL::Mutations::EDIT_REPL_COMMENT,
106
+ input: {
107
+ id: @id,
108
+ body: content
109
+ }
110
+ )
111
+ ReplComment.new(@client, c["updateReplComment"])
112
+ end
113
+
114
+ def delete
115
+ @client.graphql(
116
+ "ReplViewCommentsDeleteReplComment",
117
+ GQL::Mutations::DELETE_REPL_COMMENT,
118
+ id: @id
119
+ )
120
+ nil
121
+ end
122
+
123
+ def to_s
124
+ @content
125
+ end
126
+ end
127
+
128
+
129
+
130
+ class Repl
131
+ attr_reader :id, :url, :title, :author, :description, :timestamp, :size, :language, :img_url, :origin_url, :is_private, :is_always_on
132
+
133
+ def initialize(client, repl)
134
+ @client = client
135
+
136
+ @id = repl["id"]
137
+ @url = $BASE_URL + repl["url"]
138
+ @title = repl["title"]
139
+ @author = User.new(@client, repl["user"])
140
+ @description = repl["description"]
141
+ @timestamp = repl["timeCreated"]
142
+ @size = repl["size"]
143
+ @language = Language.new(repl["lang"])
144
+ @image_url = repl["imageUrl"]
145
+ @origin_url = repl["origin"] == nil ? nil : $BASE_URL + repl["origin"]["url"]
146
+
147
+ @is_private = repl["isPrivate"]
148
+ @is_always_on = repl["isAlwaysOn"]
149
+ end
150
+
151
+ def get_forks(count: 100, after: nil)
152
+ f = @client.graphql(
153
+ "ReplViewForks",
154
+ GQL::Queries::GET_REPL_FORKS,
155
+ url: @url,
156
+ count: count,
157
+ after: after
158
+ )
159
+ f["repl"]["publicForks"]["items"].map { |repl| Repl.new(@client, repl) }
160
+ end
161
+
162
+ def get_comments(count: nil, after: nil)
163
+ c = @client.graphql(
164
+ "ReplViewComments",
165
+ GQL::Queries::GET_REPL_COMMENTS,
166
+ url: @url,
167
+ count: count,
168
+ after: after
169
+ )
170
+ c["repl"]["comments"]["items"].map { |comment| ReplComment.new(@client, comment) }
171
+ end
172
+
173
+ def create_comment(content)
174
+ c = @client.graphql(
175
+ "ReplViewCreateReplComment",
176
+ GQL::Mutations::CREATE_REPL_COMMENT,
177
+ input: {
178
+ replId: @id,
179
+ body: content
180
+ }
181
+ )
182
+ ReplComment.new(@client, c["createReplComment"])
183
+ end
184
+
185
+ def to_s
186
+ @title
187
+ end
188
+ end
189
+
190
+
191
+
192
+ class Comment
193
+ attr_reader :id, :url, :author, :content, :post_id, :is_answer, :vote_count, :timestamp, :can_vote, :has_voted
194
+
195
+ def initialize(client, comment)
196
+ @client = client
197
+
198
+ @id = comment["id"]
199
+ @url = $BASE_URL + comment["url"]
200
+ @author = comment["user"] == nil ? "[deleted user]" : User.new(@client, comment["user"])
201
+ @content = comment["body"]
202
+ @post_id = comment["post"]["id"]
203
+ @is_answer = comment["isAnswer"]
204
+ @vote_count = comment["voteCount"]
205
+ @timestamp = comment["timeCreated"]
206
+
207
+ @can_vote = comment["canVote"]
208
+ @has_voted = comment["hasVoted"]
209
+ end
210
+
211
+ def get_post
212
+ p = @client.graphql(
213
+ "post",
214
+ GQL::Queries::GET_POST,
215
+ id: @post_id
216
+ )
217
+ Post.new(@client, p["post"])
218
+ end
219
+
220
+ def get_comments
221
+ c = @client.graphql(
222
+ "comment",
223
+ GQL::Queries::GET_COMMENTS_COMMENTS,
224
+ id: @id
225
+ )
226
+ c["comment"]["comments"].map { |comment| Comment.new(@client, comment) }
227
+ end
228
+
229
+ def get_parent
230
+ c = @client.graphql(
231
+ "comment",
232
+ GQL::Queries::GET_PARENT_COMMENT,
233
+ id: @id
234
+ )
235
+ c["comment"]["parentComment"] == nil ? nil : Comment.new(@client, c["comment"]["parentComment"])
236
+ end
237
+
238
+ def create_comment(content)
239
+ c = @client.graphql(
240
+ "createComment",
241
+ GQL::Mutations::CREATE_COMMENT,
242
+ input: {
243
+ postId: @post_id,
244
+ commentId: @id,
245
+ body: content
246
+ }
247
+ )
248
+ Comment.new(@client, c["createComment"]["comment"])
249
+ end
250
+
251
+ def edit(content)
252
+ c = @client.graphql(
253
+ "updateComment",
254
+ GQL::Mutations::EDIT_COMMENT,
255
+ input: {
256
+ id: @id,
257
+ body: content
258
+ }
259
+ )
260
+ Comment.new(@client, c["updateComment"]["comment"])
261
+ end
262
+
263
+ def delete
264
+ @client.graphql(
265
+ "deleteComment",
266
+ GQL::Mutations::DELETE_COMMENT,
267
+ id: @id
268
+ )
269
+ nil
270
+ end
271
+
272
+ def report(reason)
273
+ @client.graphql(
274
+ "createBoardReport",
275
+ GQL::Mutations::REPORT_COMMENT,
276
+ id: @id,
277
+ reason: reason
278
+ )
279
+ nil
280
+ end
281
+
282
+ def to_s
283
+ @content
284
+ end
285
+ end
286
+
287
+
288
+
289
+ class Post
290
+ attr_reader :id, :url, :repl, :board, :title, :author, :answer, :content, :preview, :timestamp, :vote_count, :comment_count, :can_vote, :has_voted, :is_answered, :is_answerable, :is_hidden, :is_pinned, :is_locked, :is_announcement
291
+
292
+ def initialize(client, post)
293
+ @client = client
294
+
295
+ @id = post["id"]
296
+ @url = $BASE_URL + post["url"]
297
+ @title = post["title"]
298
+ @content = post["body"]
299
+ @preview = post["preview"]
300
+ @timestamp = post["timeCreated"]
301
+
302
+ @board = Board.new(post["board"])
303
+ @repl = post["repl"] == nil ? nil : Repl.new(@client, post["repl"])
304
+ @author = post["user"] == nil ? nil : User.new(@client, post["user"])
305
+ @answer = post["answer"] == nil ? nil : Comment.new(@client, post["answer"])
306
+
307
+ @vote_count = post["voteCount"]
308
+ @comment_count = post["commentCount"]
309
+
310
+ @can_vote = post["canVote"]
311
+ @has_voted = post["hasVoted"]
312
+
313
+ @is_answered = post["isAnswered"]
314
+ @is_answerable = post["isAnswerable"]
315
+
316
+ @is_hidden = post["isHidden"]
317
+ @is_pinned = post["isPinned"]
318
+ @is_locked = post["isLocked"]
319
+ @is_announcement = post["isAnnouncement"]
320
+ end
321
+
322
+ def get_comments(order: "new", count: nil, after: nil)
323
+ c = @client.graphql(
324
+ "post",
325
+ GQL::Queries::GET_POSTS_COMMENTS,
326
+ postId: @id,
327
+ order: order,
328
+ count: count,
329
+ after: after
330
+ )
331
+ c["post"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
332
+ end
333
+
334
+ def get_upvotes(count: nil)
335
+ u = @client.graphql(
336
+ "post",
337
+ GQL::Queries::GET_POSTS_UPVOTERS,
338
+ id: @id,
339
+ count: count
340
+ )
341
+ u["post"]["votes"]["items"].map { |vote| User.new(@client, vote["user"]) }
342
+ end
343
+
344
+ def create_comment(content)
345
+ c = @client.graphql(
346
+ "createComment",
347
+ GQL::Mutations::CREATE_COMMENT,
348
+ input: {
349
+ postId: @id,
350
+ body: content
351
+ }
352
+ )
353
+ Comment.new(@client, c["createComment"]["comment"])
354
+ end
355
+
356
+ def edit(title: @title, content: @content, repl_id: @repl.id, show_hosted: false)
357
+ p = @client.graphql(
358
+ "updatePost",
359
+ GQL::Mutations::EDIT_POST,
360
+ input: {
361
+ id: @id,
362
+ title: title,
363
+ body: content,
364
+ replId: repl_id,
365
+ showHosted: show_hosted
366
+ }
367
+ )
368
+ Post.new(@client, p["updatePost"]["post"])
369
+ end
370
+
371
+ def delete
372
+ @client.graphql(
373
+ "deletePost",
374
+ GQL::Mutations::DELETE_POST,
375
+ id: @id
376
+ )
377
+ nil
378
+ end
379
+
380
+ def report(reason)
381
+ @client.graphql(
382
+ "createBoardReport",
383
+ GQL::Mutations::REPORT_POST,
384
+ id: @id,
385
+ reason: reason
386
+ )
387
+ nil
388
+ end
389
+
390
+ def to_s
391
+ @title
392
+ end
393
+ end
394
+
395
+
396
+
397
+ class User
398
+ attr_reader :id, :username, :name, :pfp, :bio, :cycles, :is_hacker, :timestamp, :subscription, :roles, :organization, :languages
399
+
400
+ def initialize(client, user)
401
+ return nil if user == nil
402
+ @client = client
403
+
404
+ @id = user["id"]
405
+ @username = user["username"]
406
+ @name = user["fullName"]
407
+ @pfp = user["image"]
408
+ @bio = user["bio"]
409
+ @cycles = user["karma"]
410
+ @is_hacker = user["isHacker"]
411
+ @timestamp = user["timeCreated"]
412
+ @roles = user["roles"].map { |role| Role.new(role) }
413
+ @organization = user["organization"] == nil ? nil : Organization.new(user["organization"])
414
+ @languages = user["languages"].map { |lang| Language.new(lang) }
415
+ end
416
+
417
+ def get_posts(order: "new", count: nil, after: nil)
418
+ p = @client.graphql(
419
+ "user",
420
+ GQL::Queries::GET_USER_POSTS,
421
+ username: @username,
422
+ order: order,
423
+ count: count,
424
+ after: after
425
+ )
426
+ p["user"]["posts"]["items"].map { |post| Post.new(@client, post) }
427
+ end
428
+
429
+ def get_comments(order: "new", count: nil, after: nil)
430
+ c = @client.graphql(
431
+ "user",
432
+ GQL::Queries::GET_USER_COMMENTS,
433
+ username: @username,
434
+ order: order,
435
+ count: count,
436
+ after: after
437
+ )
438
+ c["user"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
439
+ end
440
+
441
+ def get_repls(count: nil, order: nil, direction: nil, before: nil, after: nil, pinnedReplsFirst: nil, showUnnamed: nil)
442
+ r = @client.graphql(
443
+ "user",
444
+ GQL::Queries::GET_USER_REPLS,
445
+ username: @username,
446
+ order: order,
447
+ count: count,
448
+ direction: direction,
449
+ before: before,
450
+ after: after,
451
+ pinnedReplsFirst: pinnedReplsFirst,
452
+ showUnnamed: showUnnamed
453
+ )
454
+ r["user"]["publicRepls"]["items"].map { |repl| Repl.new(@client, repl) }
455
+ end
456
+
457
+ def to_s
458
+ @username
459
+ end
460
+ end
461
+
462
+
463
+
464
+ class LeaderboardUser < User
465
+ attr_reader :cycles_since
466
+
467
+ def initialize(client, user)
468
+ super(client, user)
469
+ @cycles_since = user["karmaSince"]
470
+ end
471
+ end
472
+ end