Atmosfire 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 83cc1ffc021213e0fc91521cedcb674b095521c8d426ec78736b8729511cc763
4
- data.tar.gz: 48b36faf67694ee803c68c21e30f411e125b1b265edc7eba14bf9c5fc35f83f6
3
+ metadata.gz: a6019bf1753d127a42713023bacb28766618cbb070b4fb0d4f1014e0534c0198
4
+ data.tar.gz: b05c49f4859eb3acf8ee38a89ee325372eb6d830e9796195e54f5db23d402dde
5
5
  SHA512:
6
- metadata.gz: b11ff02dd2a66ae2f6dbc441646e8c004336aa5c3ba1feedf0931e68f951ebb87d4f2e9feaf6931485666ae0e4f49c286d8b7b39119fa6069f5bfe198845768e
7
- data.tar.gz: 5cb83d2bdce1178192334682cca26954744f8f9d7afaa117d5dc86eb39c6c30d9011846d4bea81362f680378091ea20d2e24989a4b68c96b08cc89ddd231d38b
6
+ metadata.gz: 64f31d799ebb6158e05a7aced34b500c8e70fb34cfd25973c3fffae5e968f03d29c063e78380e38d7259b98c73d516a0b63f1d9e4df3dc86e93b420910e446c7
7
+ data.tar.gz: 73a6e7eb8cb76c6ee61ac046c4a19a2c0e9a4c7d3c2a1e6a1031df91a870dfffdfafff63cbfcff85fbebc6f943acb35abac692f18aa21bdaa8e731274926f8f4
@@ -1,4 +1,4 @@
1
- # typed: true
1
+ # typed: false
2
2
 
3
3
  module Atmosfire
4
4
  module RequestUtils
@@ -16,7 +16,10 @@ end
16
16
  module Atmosfire
17
17
  module AtUriParser
18
18
  extend T::Sig
19
- include RequestUtils
19
+
20
+ class << self
21
+ include RequestUtils
22
+ end
20
23
 
21
24
  Rule = Struct.new(:pattern, :transform)
22
25
 
@@ -40,15 +43,29 @@ module Atmosfire
40
43
  Rule.new(pattern, transform)
41
44
  end
42
45
  RuleSets = [
43
- AtUriParser.create_rule(%r{^#{Regexp.escape("https://")}(bsky\.app)/profile/(.+)/post/([\w]+)$}) do |handle, collection, rkey, pds|
46
+ AtUriParser.create_rule(%r{^#{Regexp.escape("https://")}(bsky\.app)/profile/(.+)/post/([\w]+)$}) do |_, handle, rkey, pds|
44
47
  handle.start_with?("did:") ? did = handle : did = resolve_handle(handle, pds)
45
48
  AtUri.new(repo: did, collection: "app.bsky.feed.post", rkey: rkey)
46
49
  end,
47
50
 
51
+ AtUriParser.create_rule(%r{^#{Regexp.escape("https://")}(bsky\.app)/profile/(.+)$}) do |_, handle, pds|
52
+ handle.start_with?("did:") ? did = handle : did = resolve_handle(handle, pds)
53
+ AtUri.new(repo: did)
54
+ end,
55
+
48
56
  AtUriParser.create_rule(%r{^at://(.+)/(.+)/(\w+)$}) do |handle, collection, rkey, pds|
49
57
  handle.start_with?("did:") ? did = handle : did = resolve_handle(handle, pds)
50
58
  AtUri.new(repo: did, collection: collection, rkey: rkey)
51
59
  end,
60
+ AtUriParser.create_rule(%r{^at://(.+)/(.+)$}) do |handle, collection, pds|
61
+ handle.start_with?("did:") ? did = handle : did = resolve_handle(handle, pds)
62
+ AtUri.new(repo: did, collection: collection)
63
+ end,
64
+ AtUriParser.create_rule(%r{^at://(.+)$}) do |handle, pds|
65
+ handle.start_with?("did:") ? did = handle : did = resolve_handle(handle, pds)
66
+ AtUri.new(repo: did)
67
+ end,
68
+
52
69
  ]
53
70
  end
54
71
 
@@ -58,10 +75,20 @@ module Atmosfire
58
75
  const :collection, T.nilable(T.any(Atmosfire::Repo::Collection, String))
59
76
  const :rkey, T.nilable(String)
60
77
 
61
- sig { returns(String) }
78
+ def resolve(pds: "https://bsky.social")
79
+ if @collection.nil?
80
+ Repo.new(@repo.to_s, pds)
81
+ elsif @rkey.nil?
82
+ Repo::Collection.new(Repo.new(@repo.to_s, pds), @collection.to_s, pds)
83
+ else
84
+ Record.from_uri(self, pds)
85
+ end
86
+ end
87
+
88
+ sig { params(pds: String).returns(String) }
62
89
 
63
- def to_s
64
- "at://#{repo}/#{collection}/#{rkey}"
90
+ def to_s(pds: "https://bsky.social")
91
+ self.resolve(pds: pds).to_uri
65
92
  end
66
93
  end
67
94
  end
@@ -1,4 +1,4 @@
1
- # typed: strict
1
+ # typed: true
2
2
  module Atmosfire
3
3
  class Repo
4
4
  class Collection < T::Struct
@@ -11,7 +11,7 @@ module Atmosfire
11
11
  sig { params(limit: Integer).returns(T::Array[Atmosfire::Record]) }
12
12
 
13
13
  def list(limit = 10)
14
- self.repo.pds_endpoint
14
+ self.repo.xrpc
15
15
  .get.com_atproto_repo_listRecords(
16
16
  repo: self.repo.did,
17
17
  collection: self.collection,
@@ -22,10 +22,16 @@ module Atmosfire
22
22
  }
23
23
  end
24
24
 
25
+ def list_all()
26
+ T.must(get_paginated_data(self.repo, :com_atproto_repo_listRecords.to_s, key: "records", params: { repo: self.repo.to_s, collection: self.to_s }, cursor: nil) do |record|
27
+ Atmosfire::Record.from_hash(record)
28
+ end)
29
+ end
30
+
25
31
  sig { returns(String) }
26
32
 
27
33
  def to_uri
28
- "at://#{self.repo.did}/#{self.collection}"
34
+ "at://#{self.repo.did}/#{self.collection}/"
29
35
  end
30
36
 
31
37
  sig { returns(String) }
@@ -36,18 +36,13 @@ module Atmosfire
36
36
 
37
37
  def create(content_hash, session, rkey = nil)
38
38
  return nil if content_hash["$type"].nil?
39
- if rkey.nil?
40
- from_uri(at_uri(session.xrpc.post.com_atproto_repo_createRecord(
41
- repo: session.did,
42
- collection: content_hash["$type"],
43
- record: content_hash,
44
- )["uri"]))
45
- else from_uri(at_uri(session.xrpc.post.com_atproto_repo_createRecord(
39
+ params = {
46
40
  repo: session.did,
47
41
  collection: content_hash["$type"],
48
- rkey: rkey,
49
42
  record: content_hash,
50
- )["uri"])) end
43
+ }
44
+ params[:rkey] = rkey unless rkey.nil?
45
+ from_uri(at_uri(session.xrpc.post.com_atproto_repo_createRecord(params)["uri"]))
51
46
  end
52
47
  end
53
48
 
@@ -68,6 +63,8 @@ module Atmosfire
68
63
  self.class.from_uri(self.uri, session.pds)
69
64
  end
70
65
 
66
+ sig { params(session: Atmosfire::Session).returns(T.nilable(Atmosfire::Record)) }
67
+
71
68
  def put(session)
72
69
  session.xrpc.post.com_atproto_repo_putRecord(
73
70
  repo: session.did,
@@ -86,5 +83,9 @@ module Atmosfire
86
83
  rkey: self.uri.rkey,
87
84
  )
88
85
  end
86
+
87
+ def to_uri
88
+ "at://#{self.uri.repo}/#{self.uri.collection}/#{self.uri.rkey}"
89
+ end
89
90
  end
90
91
  end
@@ -12,12 +12,12 @@ class Atmosfire::Repo
12
12
  # @param authenticate [NilClass, Object] Additional authentication data (default: nil).
13
13
 
14
14
  def initialize(username, pds = "https://bsky.social", open: true, authenticate: nil)
15
- @pds = pds
16
- @pds_endpoint = XRPC::Client.new(pds)
15
+ @pds = T.let pds, String
16
+ @xrpc = T.let(XRPC::Client.new(pds), XRPC::Client)
17
17
  if username.start_with?("did:")
18
- @did = username
18
+ @did = T.let(username, String)
19
19
  else
20
- @did = resolve_handle(username, pds)
20
+ @did = T.let(resolve_handle(username, pds), String)
21
21
  end
22
22
  @record_list = []
23
23
  if open == true
@@ -32,9 +32,11 @@ class Atmosfire::Repo
32
32
  sig { returns(String) }
33
33
 
34
34
  def to_uri
35
- "at://#{@did}"
35
+ "at://#{@did}/"
36
36
  end
37
37
 
38
+ sig { returns(String) }
39
+
38
40
  def to_s
39
41
  @did
40
42
  end
@@ -42,7 +44,7 @@ class Atmosfire::Repo
42
44
  sig { returns(Hash) }
43
45
 
44
46
  def describe_repo
45
- @pds_endpoint.get.com_atproto_repo_describeRepo(repo: @did)
47
+ @xrpc.get.com_atproto_repo_describeRepo(repo: @did)
46
48
  end
47
49
 
48
50
  sig { returns(Hash) }
@@ -57,5 +59,5 @@ class Atmosfire::Repo
57
59
  Collection.new(repo: self, collection: collection)
58
60
  end
59
61
 
60
- attr_reader :did, :record_list, :pds, :pds_endpoint
62
+ attr_reader :did, :record_list, :pds, :xrpc
61
63
  end
@@ -7,6 +7,8 @@ module Atmosfire
7
7
  class UnauthorizedError < HTTPError; end
8
8
 
9
9
  module RequestUtils # Goal is to replace with pure XRPC eventually
10
+ extend T::Sig
11
+
10
12
  def resolve_handle(username, pds = "https://bsky.social")
11
13
  (XRPC::Client.new(pds).get.com_atproto_identity_resolveHandle(handle: username))["did"]
12
14
  end
@@ -66,5 +68,41 @@ module Atmosfire
66
68
  Authorization: "Bearer #{session.refresh_token}",
67
69
  })
68
70
  end
71
+
72
+ sig {
73
+ params(
74
+ session: T.any(Atmosfire::Session, Atmosfire::Repo),
75
+ method: String,
76
+ key: String,
77
+ params: Hash,
78
+ cursor: T.nilable(
79
+ String
80
+ ),
81
+ map_block: T.nilable(Proc),
82
+ )
83
+ .returns(T
84
+ .nilable(
85
+ Array
86
+ ))
87
+ }
88
+
89
+ def get_paginated_data(session, method, key:, params:, cursor: nil, &map_block)
90
+ (send_data = (params.merge({ :limit => 100, :cursor => cursor })))
91
+ response = session.xrpc.get.public_send(method, **send_data)
92
+ data = response.dig(key)
93
+
94
+ if data.nil? || data.empty?
95
+ return []
96
+ end
97
+
98
+ results = block_given? ? data.map(&map_block) : data
99
+ next_cursor = T.let(response.dig("cursor"), T.nilable(String))
100
+
101
+ if next_cursor.nil?
102
+ return results
103
+ else
104
+ return results + get_paginated_data(session, method, key: key, params: params, cursor: next_cursor, &map_block)
105
+ end
106
+ end
69
107
  end
70
108
  end
@@ -45,6 +45,8 @@ module Atmosfire
45
45
  raise UnauthorizedError if response["accessJwt"].nil?
46
46
  @access_token = response["accessJwt"]
47
47
  @refresh_token = response["refreshJwt"]
48
+ @xrpc = XRPC::Client.new(@pds, @access_token)
49
+ @refresher = XRPC::Client.new(@pds, @refresh_token)
48
50
  end
49
51
 
50
52
  sig { returns(T.nilable(Hash)) }
@@ -66,3 +68,21 @@ module Atmosfire
66
68
  end
67
69
  end
68
70
  end
71
+
72
+ module Atmosfire
73
+ class TokenSession < Session
74
+ extend T::Sig
75
+
76
+ sig { params(token: String, pds: String).void }
77
+
78
+ def initialize(token, pds = "https://bsky.social")
79
+ @token = token
80
+ @pds = pds
81
+ open!
82
+ end
83
+
84
+ def open!
85
+ @xrpc = XRPC::Client.new(@pds, @token)
86
+ end
87
+ end
88
+ end
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module Atmosfire
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.4"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Atmosfire
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shreyan Jain
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2023-07-24 00:00:00.000000000 Z
12
+ date: 2023-07-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: xrpc