Module: Bskyrb::RequestUtils

Included in:
RecordManager, Session
Defined in:
lib/bskyrb/session.rb

Instance Method Summary collapse

Instance Method Details

#at_post_link(pds, url) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bskyrb/session.rb', line 58

def at_post_link(pds, url)
  url = url.to_s

  # Check if the URL is already in AT format
  if url.start_with?("at://")
    # Validate the username and post ID in the URL
    parts = url.split("/")
    if parts.length == 5 && parts[3] == "app.bsky.feed.post"
      username = parts[2]
      post_id = parts[4]
      if post_id
        return url
      end
    end

    # If the URL is not valid, raise an error
    raise "The provided URL #{url} is not a valid AT URL"
  end

  # Validate the format of the regular URL and extract the username and post ID
  regex = /https:\/\/[a-zA-Z0-9.-]+\/profile\/[a-zA-Z0-9.-]+\/post\/[a-zA-Z0-9.-]+/
  raise "The provided URL #{url} does not match the expected schema" unless regex.match?(url)
  username = url.split("/")[-3]
  post_id = url.split("/")[-1]

  # Validate the username and post ID in the AT URL
  did = resolve_handle(pds, username)["did"]

  # Construct the AT URL
  "at://#{did}/app.bsky.feed.post/#{post_id}"
end

#create_record_uri(pds) ⇒ Object



24
25
26
# File 'lib/bskyrb/session.rb', line 24

def create_record_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.createRecord"
end

#default_authenticated_headers(session) ⇒ Object



52
53
54
55
56
# File 'lib/bskyrb/session.rb', line 52

def default_authenticated_headers(session)
  default_headers.merge({
    Authorization: "Bearer #{session.access_token}",
  })
end

#default_headersObject



20
21
22
# File 'lib/bskyrb/session.rb', line 20

def default_headers
  { "Content-Type" => "application/json" }
end

#delete_record_uri(pds) ⇒ Object



28
29
30
# File 'lib/bskyrb/session.rb', line 28

def delete_record_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.deleteRecord"
end

#get_author_feed_uri(pds, query) ⇒ Object



40
41
42
# File 'lib/bskyrb/session.rb', line 40

def get_author_feed_uri(pds, query)
  "#{pds}/xrpc/app.bsky.feed.getAuthorFeed#{query_obj_to_query_params(query)}"
end


48
49
50
# File 'lib/bskyrb/session.rb', line 48

def get_popular_uri(pds, query)
  "#{pds}/xrpc/app.bsky.unspecced.getPopular#{query_obj_to_query_params(query)}"
end

#get_post_thread_uri(pds, query) ⇒ Object



36
37
38
# File 'lib/bskyrb/session.rb', line 36

def get_post_thread_uri(pds, query)
  "#{pds}/xrpc/app.bsky.feed.getPostThread#{query_obj_to_query_params(query)}"
end

#get_timeline_uri(pds, query) ⇒ Object



44
45
46
# File 'lib/bskyrb/session.rb', line 44

def get_timeline_uri(pds, query)
  "#{pds}/xrpc/app.bsky.feed.getTimeline#{query_obj_to_query_params(query)}"
end

#query_obj_to_query_params(q) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/bskyrb/session.rb', line 12

def query_obj_to_query_params(q)
  out = "?"
  q.to_h.each do |key, value|
    out += "#{key}=#{value}&" unless value.nil? || (value.class.method_defined?(:empty?) && value.empty?)
  end
  out.slice(0...-1)
end

#resolve_handle(pds, username) ⇒ Object



7
8
9
10
# File 'lib/bskyrb/session.rb', line 7

def resolve_handle(pds, username)
  resolveHandle = XRPC::Endpoint.new(pds, "com.atproto.identity.resolveHandle", :handle)
  resolveHandle.get(handle: username)
end

#upload_blob_uri(pds) ⇒ Object



32
33
34
# File 'lib/bskyrb/session.rb', line 32

def upload_blob_uri(pds)
  "#{pds}/xrpc/com.atproto.repo.uploadBlob"
end