repltalk 2.0.3 → 3.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6ea03b8b7ed292b98d72d5d221d74d66aefc4daffea069349115273729b0b3d
4
- data.tar.gz: 650883b757e95b3509d22221855a8c0b868da61149b12efe24b79d19b4436cf7
3
+ metadata.gz: 5147f5ae7c30e13587c24a08f5215c17f0fbc9dd0963fa2c4f8a4e86cf0118eb
4
+ data.tar.gz: a7d97f153947b85650a9642848b3990ec561c243985c02afee915f63f7d5ef38
5
5
  SHA512:
6
- metadata.gz: 825db510c06db9c763e99cd585a4e7e396aa94a04b90e9615efd8ebc661d88d413b978f3f950869fa99e7f1b5bcdee54c388664ad7c066a404cfbe0718d8cb3a
7
- data.tar.gz: 7badfaf3cae0930a7c4927d3cbd32ce8a675e7acd7ef37534e0d0541f783c9a347f661819cf65400e94e89fe6a306616817618e466005490ae0f087931d3baab
6
+ metadata.gz: de64c303910bf6e93f5969107728801d2d00317ce051561d2bd2ac6ccad92ecd49a68d6b35a20f766faead8c0126c843d8fe0d6878c8b9df5b57189808ce2b84
7
+ data.tar.gz: 1d153f24ceccc17deeb89c90648133bfb6a66fe4fc5e495827fd1583ecb5f1534cefcdc5d0ab9873526fe6c191ac14a2e7fab7737a4cc5e3cbdd299db287cd0a
data/lib/repltalk.rb CHANGED
@@ -1,634 +1,8 @@
1
- require "http"
2
- require "json"
3
- require_relative "queries"
1
+ require_relative "repltalk/client"
2
+ require_relative "repltalk/graphql"
3
+ require_relative "repltalk/structures"
4
4
 
5
- $BASE_URL = "https://repl.it"
6
-
7
- class Role
8
- attr_reader :name, :key, :tagline
9
-
10
- def initialize(role)
11
- @name = role["name"]
12
- @key = role["key"]
13
- @tagline = role["tagline"]
14
- end
15
-
16
- def to_s
17
- @name
18
- end
19
- end
20
-
21
-
22
-
23
- class Organization
24
- attr_reader :id, :name, :country, :postal_code, :state, :city, :timestamp
25
-
26
- def initialize(organization)
27
- @id = organization["id"]
28
- @name = organization["name"]
29
- @country = organization["country"]
30
- @postal_code = organization["postalCode"]
31
- @state = organization["state"]
32
- @city = organization["city"]
33
- @timestamp = organization["timeCreated"]
34
- end
35
-
36
- def to_s
37
- @name
38
- end
39
- end
40
-
41
-
42
-
43
- class Subscription
44
- attr_reader :id, :plan_id, :quantity, :timestamp
45
-
46
- def initialize(subscription)
47
- @id = subscription["id"]
48
- @plan_id = subscription["planId"]
49
- @quantity = subscription["quantity"]
50
- @timestamp = subscription["timeCreated"]
51
- end
52
-
53
- def to_s
54
- @plan_id
55
- end
56
- end
57
-
58
-
59
-
60
- class Language
61
- attr_reader :id, :key, :name, :tagline, :icon, :category
62
-
63
- def initialize(lang)
64
- @id = lang["id"]
65
- @key = lang["key"]
66
- @name = lang["displayName"]
67
- @tagline = lang["tagline"]
68
- @icon = lang["icon"]
69
- @category = lang["category"]
70
- end
71
-
72
- def to_s
73
- @id
74
- end
5
+ module ReplTalk
6
+ $BASE_URL = "https://repl.it"
7
+ VERSION = "3.1.0"
75
8
  end
76
-
77
-
78
-
79
- class Board
80
- attr_reader :id, :name, :color, :description
81
-
82
- def initialize(board)
83
- @id = board["id"]
84
- @name = board["name"]
85
- @color = board["color"]
86
- @description = board["description"]
87
- end
88
-
89
- def to_s
90
- @name
91
- end
92
- end
93
-
94
-
95
-
96
- class ReplComment
97
- attr_reader :id, :content, :author, :repl, :replies
98
-
99
- def initialize(client, comment)
100
- @client = client
101
-
102
- @id = comment["id"]
103
- @content = comment["body"]
104
- @author = comment["user"] == nil ? "[deleted user]" : User.new(@client, comment["user"])
105
- @repl = Repl.new(@client, comment["repl"])
106
- @replies = comment["replies"] == nil ? nil : comment["replies"].map { |c| ReplComment.new(@client, c) }
107
- end
108
-
109
- def create_comment(content)
110
- c = @client.graphql(
111
- "ReplViewCreateReplCommentReply",
112
- Mutations.reply_repl_comment,
113
- input: {
114
- replCommentId: @id,
115
- body: content
116
- }
117
- )
118
- ReplComment.new(@client, c["createReplCommentReply"])
119
- end
120
-
121
- def edit(content)
122
- c = @client.graphql(
123
- "ReplViewCommentsUpdateReplComment",
124
- Mutations.edit_repl_comment,
125
- input: {
126
- id: @id,
127
- body: content
128
- }
129
- )
130
- puts c
131
- ReplComment.new(@client, c["updateReplComment"])
132
- end
133
-
134
- def delete
135
- @client.graphql(
136
- "ReplViewCommentsDeleteReplComment",
137
- Mutations.delete_repl_comment,
138
- id: @id
139
- )
140
- nil
141
- end
142
-
143
- def to_s
144
- @content
145
- end
146
- end
147
-
148
-
149
-
150
- class Repl
151
- attr_reader :id, :url, :title, :author, :description, :size, :language, :img_url, :origin_url, :is_private, :is_always_on
152
-
153
- def initialize(client, repl)
154
- @client = client
155
-
156
- @id = repl["id"]
157
- @url = $BASE_URL + repl["url"]
158
- @title = repl["title"]
159
- @author = User.new(@client, repl["user"])
160
- @description = repl["description"]
161
- @size = repl["size"]
162
- @language = Language.new(repl["lang"])
163
- @image_url = repl["imageUrl"]
164
- @origin_url = repl["origin"] == nil ? nil : $BASE_URL + repl["origin"]["url"]
165
-
166
- @is_private = repl["isPrivate"]
167
- @is_always_on = repl["isAlwaysOn"]
168
- end
169
-
170
- def get_forks(count: 100, after: nil)
171
- f = @client.graphql(
172
- "ReplViewForks",
173
- Queries.get_repl_forks,
174
- url: @url,
175
- count: count,
176
- after: after
177
- )
178
- f["repl"]["publicForks"]["items"].map { |repl| Repl.new(@client, repl) }
179
- end
180
-
181
- def get_comments(count: nil, after: nil)
182
- c = @client.graphql(
183
- "ReplViewComments",
184
- Queries.get_repl_comments,
185
- url: @url,
186
- count: count,
187
- after: after
188
- )
189
- c["repl"]["comments"]["items"].map { |comment| ReplComment.new(@client, comment) }
190
- end
191
-
192
- def create_comment(content)
193
- c = @client.graphql(
194
- "ReplViewCreateReplComment",
195
- Mutations.create_repl_comment,
196
- input: {
197
- replId: @id,
198
- body: content
199
- }
200
- )
201
- ReplComment.new(@client, c["createReplComment"])
202
- end
203
-
204
- def to_s
205
- @title
206
- end
207
- end
208
-
209
-
210
-
211
- class Comment
212
- attr_reader :id, :url, :author, :content, :post_id, :is_answer, :vote_count, :timestamp, :can_vote, :has_voted
213
-
214
- def initialize(client, comment)
215
- @client = client
216
-
217
- @id = comment["id"]
218
- @url = $BASE_URL + comment["url"]
219
- @author = comment["user"] == nil ? "[deleted user]" : User.new(@client, comment["user"])
220
- @content = comment["body"]
221
- @post_id = comment["post"]["id"]
222
- @is_answer = comment["isAnswer"]
223
- @vote_count = comment["voteCount"]
224
- @timestamp = comment["timeCreated"]
225
-
226
- @can_vote = comment["canVote"]
227
- @has_voted = comment["hasVoted"]
228
- end
229
-
230
- def get_post
231
- p = @client.graphql(
232
- "post",
233
- Queries.get_post,
234
- id: @post_id
235
- )
236
- Post.new(@client, p["post"])
237
- end
238
-
239
- def get_comments
240
- c = @client.graphql(
241
- "comment",
242
- Queries.get_comments_comments,
243
- id: @id
244
- )
245
- c["comment"]["comments"].map { |comment| Comment.new(@client, comment) }
246
- end
247
-
248
- def get_parent
249
- c = @client.graphql(
250
- "comment",
251
- Queries.get_parent_comment,
252
- id: @id
253
- )
254
- c["comment"]["parentComment"] == nil ? nil : Comment.new(@client, c["comment"]["parentComment"])
255
- end
256
-
257
- def create_comment(content)
258
- c = @client.graphql(
259
- "createComment",
260
- Mutations.create_comment,
261
- input: {
262
- postId: @post_id,
263
- commentId: @id,
264
- body: content
265
- }
266
- )
267
- Comment.new(@client, c["createComment"]["comment"])
268
- end
269
-
270
- def edit(content)
271
- c = @client.graphql(
272
- "updateComment",
273
- Mutations.edit_comment,
274
- input: {
275
- id: @id,
276
- body: content
277
- }
278
- )
279
- Comment.new(@client, c["updateComment"]["comment"])
280
- end
281
-
282
- def delete
283
- @client.graphql(
284
- "deleteComment",
285
- Mutations.delete_comment,
286
- id: @id
287
- )
288
- nil
289
- end
290
-
291
- def report(reason)
292
- @client.graphql(
293
- "createBoardReport",
294
- Mutations.report_comment,
295
- id: @id,
296
- reason: reason
297
- )
298
- nil
299
- end
300
-
301
- def to_s
302
- @content
303
- end
304
- end
305
-
306
-
307
-
308
- class Post
309
- 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
310
-
311
- def initialize(client, post)
312
- @client = client
313
-
314
- @id = post["id"]
315
- @url = $BASE_URL + post["url"]
316
- @title = post["title"]
317
- @content = post["body"]
318
- @preview = post["preview"]
319
- @timestamp = post["timeCreated"]
320
-
321
- @board = Board.new(post["board"])
322
- @repl = post["repl"] == nil ? nil : Repl.new(@client, post["repl"])
323
- @author = post["user"] == nil ? "[deleted user]" : User.new(@client, post["user"])
324
- @answer = post["answer"] == nil ? nil : Comment.new(@client, post["answer"])
325
-
326
- @vote_count = post["voteCount"]
327
- @comment_count = post["commentCount"]
328
-
329
- @can_vote = post["canVote"]
330
- @has_voted = post["hasVoted"]
331
-
332
- @is_answered = post["isAnswered"]
333
- @is_answerable = post["isAnswerable"]
334
-
335
- @is_hidden = post["isHidden"]
336
- @is_pinned = post["isPinned"]
337
- @is_locked = post["isLocked"]
338
- @is_announcement = post["isAnnouncement"]
339
- end
340
-
341
- def get_comments(order: "new", count: nil, after: nil)
342
- c = @client.graphql(
343
- "post",
344
- Queries.get_posts_comments,
345
- postId: @id,
346
- order: order,
347
- count: count,
348
- after: after
349
- )
350
- c["post"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
351
- end
352
-
353
- def get_upvotes(count: nil)
354
- u = @client.graphql(
355
- "post",
356
- Queries.get_posts_upvoters,
357
- id: @id,
358
- count: count
359
- )
360
- u["post"]["votes"]["items"].map { |vote| User.new(@client, vote["user"]) }
361
- end
362
-
363
- def create_comment(content)
364
- c = @client.graphql(
365
- "createComment",
366
- Mutations.create_comment,
367
- input: {
368
- postId: @id,
369
- body: content
370
- }
371
- )
372
- Comment.new(@client, c["createComment"]["comment"])
373
- end
374
-
375
- def edit(title: @title, content: @content, repl_id: @repl.id, show_hosted: false)
376
- p = @client.graphql(
377
- "updatePost",
378
- Mutations.edit_post,
379
- input: {
380
- id: @id,
381
- title: title,
382
- body: content,
383
- replId: repl_id,
384
- showHosted: show_hosted
385
- }
386
- )
387
- Post.new(@client, p["updatePost"]["post"])
388
- end
389
-
390
- def delete
391
- @client.graphql(
392
- "deletePost",
393
- Mutations.delete_post,
394
- id: @id
395
- )
396
- nil
397
- end
398
-
399
- def report(reason)
400
- @client.graphql(
401
- "createBoardReport",
402
- Mutations.report_post,
403
- id: @id,
404
- reason: reason
405
- )
406
- nil
407
- end
408
-
409
- def to_s
410
- @title
411
- end
412
- end
413
-
414
-
415
-
416
- class User
417
- attr_reader :id, :username, :name, :pfp, :bio, :cycles, :is_hacker, :timestamp, :subscription, :roles, :organization, :languages
418
-
419
- def initialize(client, user)
420
- @client = client
421
-
422
- @id = user["id"]
423
- @username = user["username"]
424
- @name = user["fullName"]
425
- @pfp = user["image"]
426
- @bio = user["bio"]
427
- @cycles = user["karma"]
428
- @is_hacker = user["isHacker"]
429
- @timestamp = user["timeCreated"]
430
- @subscription = user["subscription"] == nil ? nil : Subscription.new(user["subscription"])
431
- @roles = user["roles"].map { |role| Role.new(role) }
432
- @organization = user["organization"] == nil ? nil : Organization.new(user["organization"])
433
- @languages = user["languages"].map { |lang| Language.new(lang) }
434
- end
435
-
436
- def get_posts(order: "new", count: nil, after: nil)
437
- p = @client.graphql(
438
- "user",
439
- Queries.get_user_posts,
440
- username: @username,
441
- order: order,
442
- count: count,
443
- after: after
444
- )
445
- p["user"]["posts"]["items"].map { |post| Post.new(@client, post) }
446
- end
447
-
448
- def get_comments(order: "new", count: nil, after: nil)
449
- c = @client.graphql(
450
- "user",
451
- Queries.get_user_comments,
452
- username: @username,
453
- order: order,
454
- count: count,
455
- after: after
456
- )
457
- c["user"]["comments"]["items"].map { |comment| Comment.new(@client, comment) }
458
- end
459
-
460
- def get_repls(count: nil, order: nil, direction: nil, before: nil, after: nil, pinnedReplsFirst: nil, showUnnamed: nil)
461
- r = @client.graphql(
462
- "user",
463
- Queries.get_user_repls,
464
- username: @username,
465
- order: order,
466
- count: count,
467
- direction: direction,
468
- before: before,
469
- after: after,
470
- pinnedReplsFirst: pinnedReplsFirst,
471
- showUnnamed: showUnnamed
472
- )
473
- r["user"]["publicRepls"]["items"].map { |repl| Repl.new(@client, repl) }
474
- end
475
-
476
- def to_s
477
- @username
478
- end
479
- end
480
-
481
-
482
-
483
- class LeaderboardUser < User
484
- attr_reader :cycles_since
485
-
486
- def initialize(client, user)
487
- super(client, user)
488
- @cycles_since = user["karmaSince"]
489
- end
490
- end
491
-
492
-
493
-
494
- class Client
495
- attr_writer :sid
496
-
497
- def initialize(sid=nil)
498
- @sid = sid
499
- end
500
-
501
- def graphql(name, query, **variables)
502
- payload = {
503
- operationName: name,
504
- query: query,
505
- variables: variables.to_json
506
- }
507
- r = HTTP
508
- .cookies(
509
- "connect.sid": @sid
510
- )
511
- .headers(
512
- referer: "#{$BASE_URL}/@CodingCactus/repltalk",
513
- "X-Requested-With": "ReplTalk"
514
- )
515
- .post(
516
- "#{$BASE_URL}/graphql",
517
- form: payload
518
- )
519
- begin data = JSON.parse(r)
520
- rescue
521
- puts "\e[31mERROR\n#{r}\e[0m"
522
- return nil
523
- end
524
- if data.include?("errors")
525
- puts "\e[31mERROR\n#{r}\e[0m"
526
- return nil
527
- end
528
- data = data["data"] if data.include?("data")
529
- data
530
- end
531
-
532
- def get_user(name)
533
- u = graphql(
534
- "userByUsername",
535
- Queries.get_user,
536
- username: name
537
- )
538
- User.new(self, u["user"])
539
- end
540
-
541
- def get_user_by_id(id)
542
- u = graphql(
543
- "user",
544
- Queries.get_user_by_id,
545
- user_id: id
546
- )
547
- User.new(self, u["user"])
548
- end
549
-
550
- def get_post(id)
551
- p = graphql(
552
- "post",
553
- Queries.get_post,
554
- id: id
555
- )
556
- Post.new(self, p["post"])
557
- end
558
-
559
- def get_comment(id)
560
- c = graphql(
561
- "comment",
562
- Queries.get_comment,
563
- id: id
564
- )
565
- Comment.new(self, c["comment"])
566
- end
567
-
568
- def get_repl(url)
569
- r = graphql(
570
- "ReplView",
571
- Queries.get_repl,
572
- url: url
573
- )
574
- Repl.new(self, r["repl"])
575
- end
576
-
577
- def get_repl_comment(id)
578
- c = graphql(
579
- "ReplViewComment",
580
- Queries.get_repl_comment,
581
- id: id
582
- )
583
- ReplComment.new(self, c["replComment"])
584
- end
585
-
586
- def get_board(name)
587
- b = graphql(
588
- "boardBySlug",
589
- Queries.get_board,
590
- slug: name
591
- )
592
- Board.new(b["board"])
593
- end
594
-
595
- def get_leaderboard(count: nil, since: nil, after: nil)
596
- u = graphql(
597
- "LeaderboardQuery",
598
- Queries.get_leaderboard,
599
- count: count,
600
- since: since,
601
- after: after
602
- )
603
- u["leaderboard"]["items"].map { |user| LeaderboardUser.new(self, user) }
604
- end
605
-
606
- def get_posts(board: "all", order: "new", count: nil, after: nil, search: nil, languages: nil)
607
- p = graphql(
608
- "PostsFeed",
609
- Queries.get_posts,
610
- boardSlugs: [board],
611
- order: order,
612
- count: count,
613
- after: after,
614
- searchQuery: search,
615
- languages: languages
616
- )
617
- p["posts"]["items"].map { |post| Post.new(self, post) }
618
- end
619
-
620
- def create_post(board_name, title, content, repl_id: nil, show_hosted: false)
621
- p = graphql(
622
- "createPost",
623
- Mutations.create_post,
624
- input: {
625
- boardId: get_board(board_name).id,
626
- title: title,
627
- body: content,
628
- replId: repl_id,
629
- showHosted: show_hosted
630
- }
631
- )
632
- Post.new(self, p["createPost"]["post"])
633
- end
634
- end