repltalk 2.1.0 → 3.0.0

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