Class: ReplTalk::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/repltalk/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sid = nil) ⇒ Client

Returns a new instance of Client.



8
9
10
# File 'lib/repltalk/client.rb', line 8

def initialize(sid=nil)
	@sid = sid
end

Instance Attribute Details

#sid=(value) ⇒ Object (writeonly)

Sets the attribute sid

Parameters:

  • value

    the value to set the attribute sid to.



6
7
8
# File 'lib/repltalk/client.rb', line 6

def sid=(value)
  @sid = value
end

Instance Method Details

#create_post(board_name, title, content, repl_id: nil, show_hosted: false) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/repltalk/client.rb', line 165

def create_post(board_name, title, content, repl_id: nil, show_hosted: false)
	p = graphql(
		"createPost",
		GQL::Mutations::CREATE_POST,
		input: {
			boardId: get_board(board_name).id,
			title: title,
			body: content,
			replId: repl_id,
			showHosted: show_hosted
		}
	)
	Post.new(self, p["createPost"]["post"])
end

#get_board(name) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/repltalk/client.rb', line 114

def get_board(name)
	b = graphql(
		"boardBySlug",
		GQL::Queries::GET_BOARD,
		slug: name
	)
	return nil if b == nil || b["board"] == nil
	Board.new(b["board"])
end

#get_comment(id) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/repltalk/client.rb', line 84

def get_comment(id)
	c = graphql(
		"comment",
		GQL::Queries::GET_COMMENT,
		id: id
	)
	return nil if c == nil || c["comment"] == nil
	Comment.new(self, c["comment"])
end


139
140
141
142
143
144
145
# File 'lib/repltalk/client.rb', line 139

def get_explore_featured_repls
	r = graphql(
		"ExploreFeaturedRepls",
		GQL::Queries::GET_EXPLORE_FEATURED_REPLS
	)
	r["featuredRepls"].map { |repl| Repl.new(self, repl) }
end

#get_post(id) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/repltalk/client.rb', line 74

def get_post(id)
	p = graphql(
		"post",
		GQL::Queries::GET_POST,
		id: id
	)
	return nil if p == nil || p["post"] == nil
	Post.new(self, p["post"])
end

#get_posts(board: "all", order: "new", count: nil, after: nil, search: nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/repltalk/client.rb', line 124

def get_posts(board: "all", order: "new", count: nil, after: nil, search: nil)
	p = graphql(
		"ReplPostsFeed",
		GQL::Queries::GET_POSTS,
		options: {
			boardSlugs: [board],
			order: order.capitalize,
			count: count,
			after: after,
			searchQuery: search
		}
	)
	p["replPosts"]["items"].map { |post| Post.new(self, post) }
end

#get_repl(url) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/repltalk/client.rb', line 94

def get_repl(url)
	r = graphql(
		"ReplView",
		GQL::Queries::GET_REPL,
		url: url
	)
	return nil if  r == nil || r["repl"] == nil
	Repl.new(self, r["repl"])
end

#get_repl_comment(id) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/repltalk/client.rb', line 104

def get_repl_comment(id)
	c = graphql(
		"ReplViewComment",
		GQL::Queries::GET_REPL_COMMENT,
		id: id
	)
	return nil if c == nil || c["replComment"] == nil
	ReplComment.new(self, c["replComment"])
end

#get_tag(tag) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/repltalk/client.rb', line 156

def get_tag(tag)
	t = graphql(
		"ExploreTrendingRepls",
		GQL::Queries::GET_TAG,
		tag: tag
	)
	Tag.new(self, t["tag"])
end


147
148
149
150
151
152
153
154
# File 'lib/repltalk/client.rb', line 147

def get_trending_tags(count: nil)
	t = graphql(
		"ExploreFeed",
		GQL::Queries::GET_TRENDING_TAGS,
		count: count
	)
	t["trendingTagsFeed"]["initialTags"].map { |tag| Tag.new(self, tag) }
end

#get_user(name) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/repltalk/client.rb', line 43

def get_user(name)
	u = graphql(
		"userByUsername",
		GQL::Queries::GET_USER,
		username: name
	)
	return nil if u == nil || u["user"] == nil
	User.new(self, u["user"])
end

#get_user_by_id(id) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/repltalk/client.rb', line 53

def get_user_by_id(id)
	u = graphql(
		"user",
		GQL::Queries::GET_USER_BY_ID,
		user_id: id
	)
	return nil if u == nil || u["user"] == nil
	User.new(self, u["user"])
end

#graphql(name, query, **variables) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/repltalk/client.rb', line 12

def graphql(name, query, **variables)
	payload = {
		operationName: name,
		query: query,
		variables: variables.to_json
	}
	r  = HTTP
			.cookies(
				"connect.sid": @sid
			)
			.headers(
				referer: "#{$BASE_URL}/@CodingCactus/repltalk",
				"X-Requested-With": "ReplTalk"
			)
			.post(
				"#{$BASE_URL}/graphql",
				form: payload
			)
	begin data = JSON.parse(r)
	rescue
		puts "\e[31mERROR\n#{r}\e[0m"
		return nil
	end
	if data.include?("errors")
		puts "\e[31mERROR\n#{r}\e[0m"
		return nil
	end
	data = data["data"] if data.include?("data")
	data
end

#search_user(username, count: 10) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/repltalk/client.rb', line 63

def search_user(username, count: 10)
	u = graphql(
		"usernameSearch",
		GQL::Queries::USER_SEARCH,
		username: username,
		count: count
	)
	return nil if u["usernameSearch"] == nil
	u["usernameSearch"].map { |user| User.new(self, user) }
end