Class: Threads::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/threads/api_client.rb

Overview

Provides methods for making API requests to the Threads API and retrieving user information and threads.

Constant Summary collapse

BASE_URL =
'https://www.threads.net'
API_URL =
"#{BASE_URL}/api/graphql"

Instance Method Summary collapse

Constructor Details

#initialize ⇒ ApiClient

Returns a new instance of ApiClient.



12
13
14
15
16
17
18
19
20
# File 'lib/threads/api_client.rb', line 12

def initialize
  @token = token
  @headers = {
    'Content-Type' => 'application/x-www-form-urlencoded',
    'X-IG-App-ID' => '238260118697367',
    'X-FB-LSD' => @token,
    'Sec-Fetch-Site' => 'same-origin'
  }
end

Instance Method Details

#get_post(post_id) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/threads/api_client.rb', line 58

def get_post(post_id)
  response = Threads::RequestHandler.send_request(
    url: API_URL, headers: @headers,
    data: {
      'lsd' => @token,
      'variables' => { 'postID' => post_id }.to_json,
      'doc_id' => '5587632691339264'
    }
  )

  JSON.parse(response.body)
end

#get_user(username) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/threads/api_client.rb', line 30

def get_user(username)
  id = get_user_id_from_username(username)
  response = Threads::RequestHandler.send_request(
    url: API_URL, headers: @headers,
    data: {
      'lsd' => @token,
      'variables' => { 'userID' => id }.to_json,
      'doc_id' => '23996318473300828'
    }
  )

  JSON.parse(response.body)
end

#get_user_id_from_username(username) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/threads/api_client.rb', line 22

def get_user_id_from_username(username)
  response = Threads::RequestHandler.send_get_request("#{BASE_URL}/@#{username}")
  text = response.gsub("\n", '')

  user_id = text.match(/"props":{"user_id":"(\d+)"},/)
  user_id ? user_id[1] : nil
end

#get_user_replies(username) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/threads/api_client.rb', line 71

def get_user_replies(username)
  id = get_user_id_from_username(username)
  response = Threads::RequestHandler.send_request(
    url: API_URL, headers: @headers,
    data: {
      'lsd' => @token,
      'variables' => { 'userID' => id }.to_json,
      'doc_id' => '6307072669391286'
    }
  )

  JSON.parse(response.body)
end

#get_user_threads(username) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/threads/api_client.rb', line 44

def get_user_threads(username)
  id = get_user_id_from_username(username)
  response = Threads::RequestHandler.send_request(
    url: API_URL, headers: @headers,
    data: {
      'lsd' => @token,
      'variables' => { 'userID' => id }.to_json,
      'doc_id' => '6232751443445612'
    }
  )

  JSON.parse(response.body)
end