at_protocol 0.0.4 → 0.0.4.2

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: 88028815424d12616955d54125fb23ad627812290c2c52a8d491bb108ebd54fa
4
- data.tar.gz: d68b5af3850c75174ef573809f2d87a4012c707d82079601cdd3351da5c317ed
3
+ metadata.gz: bac3005062f2494c16a3fcabc3af9f45daf3b127b81624872ab71603de1f9fbc
4
+ data.tar.gz: 28088b1b416163feab91d06e42245279216673cf2ae1d077eebc78bfb79a0b1c
5
5
  SHA512:
6
- metadata.gz: 37cdd260697f6461ed9215f4d06b932988a292a5fc53afde8d555a1dc04c5355819026742528997d5c2d476a9618ddf396a972f794c73cab11784e6fff39c520
7
- data.tar.gz: 5cd9f10e044b3edfdc37ce71abd01ae53fd66bfe4fa9669157d0a2f507e5d6190f3ed4b89490fb012dc9868ac2c7e2722fa35967428934cf1efec697c6def618
6
+ metadata.gz: eeb00d26cead39126432d2be52540da02f5d9734eebe9c5c4735dd4e4e556ae88a0886fe8455c98a72969b7728e2213ad0f957317a1d89852cdbcb687e7d44f7
7
+ data.tar.gz: deb75bae3fe96f9eb2831f0e61ce48f23aa9030b5fe1714123cb0c7293e7378ac3a13cef0850f3b653e5467376b19fbf07de5a9d50e21d2a775527ce16b507ba
@@ -13,6 +13,10 @@ module ATProto
13
13
 
14
14
  module_function :at_uri
15
15
  end
16
+
17
+ def self.AtUri(str)
18
+ RequestUtils.at_uri(str)
19
+ end
16
20
  end
17
21
 
18
22
  module ATProto
@@ -9,22 +9,10 @@ module ATProto
9
9
  const(:repo, ATProto::Repo)
10
10
  const(:collection, String)
11
11
 
12
- sig { params(limit: Integer).returns(T::Array[ATProto::Record]) }
13
-
14
- def list(limit = 10)
15
- self.repo.xrpc
16
- .get.com_atproto_repo_listRecords(
17
- repo: self.repo.did,
18
- collection: self.collection,
19
- limit: limit,
20
- )["records"]
21
- .map { |record|
22
- ATProto::Record.from_hash(record)
23
- }
24
- end
12
+ sig { params(count: T.nilable(Integer)).returns(T::Array[ATProto::Record]) }
25
13
 
26
- def list_all()
27
- 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|
14
+ def list(count = nil)
15
+ 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 }, count: count, cursor: nil) do |record|
28
16
  ATProto::Record.from_hash(record)
29
17
  end)
30
18
  end
@@ -0,0 +1,22 @@
1
+ # typed: true
2
+ module ATProto
3
+ class Record
4
+ class StrongRef < T::Struct
5
+ include RequestUtils
6
+ extend T::Sig
7
+ const :uri, ATProto::AtUri
8
+ prop :cid, Skyfall::CID
9
+
10
+ def to_json
11
+ {
12
+ "uri" => uri.to_s,
13
+ "cid" => cid.to_s,
14
+ }
15
+ end
16
+
17
+ def inspect
18
+ self.to_json.to_json
19
+ end
20
+ end
21
+ end
22
+ end
@@ -46,7 +46,7 @@ module ATProto
46
46
  record: content_hash,
47
47
  }
48
48
  params[:rkey] = rkey unless rkey.nil?
49
- from_uri(at_uri(session.xrpc.post.com_atproto_repo_createRecord(params)["uri"]))
49
+ from_uri(at_uri(session.xrpc.post.com_atproto_repo_createRecord(**params)["uri"]))
50
50
  end
51
51
  end
52
52
  dynamic_attr_reader(:to_uri) { "at://#{self.uri.repo}/#{self.uri.collection}/#{self.uri.rkey}" }
@@ -72,19 +72,19 @@ module ATProto
72
72
 
73
73
  def to_write(type = :delete)
74
74
  ATProto::Writes::Write.new(
75
- {
75
+ **({
76
76
  action: Writes::Write::Action.deserialize(type),
77
77
  value: (self.content if type == :update),
78
78
  collection: self.uri.collection,
79
79
  rkey: self.uri.rkey,
80
- }.compact
80
+ }.compact),
81
81
  )
82
82
  end
83
83
 
84
84
  sig { params(other: ATProto::Record).returns(T::Boolean) }
85
85
 
86
86
  def ==(other)
87
- self.cid.to_s == other.cid.to_s
87
+ self.cid.to_s == other.cid.to_s && self.uri.to_s == other.uri.to_s
88
88
  end
89
89
  end
90
90
  end
@@ -78,6 +78,9 @@ module ATProto
78
78
  cursor: T.nilable(
79
79
  String
80
80
  ),
81
+ count: T.nilable(
82
+ Integer
83
+ ),
81
84
  map_block: T.nilable(Proc),
82
85
  )
83
86
  .returns(T
@@ -86,7 +89,7 @@ module ATProto
86
89
  ))
87
90
  }
88
91
 
89
- def get_paginated_data(session, method, key:, params:, cursor: nil, &map_block)
92
+ def get_paginated_data(session, method, key:, params:, cursor: nil, count: nil, &map_block)
90
93
  params.merge({ limit: 100, cursor: cursor }).then do |send_data|
91
94
  session.xrpc.get.public_send(method, **send_data).then do |response|
92
95
  response.dig(key).then do |data|
@@ -94,26 +97,19 @@ module ATProto
94
97
  return []
95
98
  end
96
99
 
97
- if block_given?
98
- data.map(&map_block).then do |results|
99
- response.dig("cursor").then do |next_cursor|
100
- if next_cursor.nil?
101
- return results
102
- else
103
- get_paginated_data(session, method, key: key, params: params, cursor: next_cursor, &map_block).then do |next_results|
104
- return results + next_results
105
- end
106
- end
107
- end
100
+ results = if block_given?
101
+ data.map(&map_block)
102
+ else
103
+ data
108
104
  end
109
- else
110
- response.dig("cursor").then do |next_cursor|
111
- if next_cursor.nil?
112
- return data
113
- else
114
- get_paginated_data(session, method, key: key, params: params, cursor: next_cursor, &map_block).then do |next_results|
115
- return data + next_results
116
- end
105
+
106
+ response.dig("cursor").then do |next_cursor|
107
+ if next_cursor.nil? || (count && results.length >= count)
108
+ return results[0..count]
109
+ else
110
+ remaining_count = count ? count - results.length : nil
111
+ get_paginated_data(session, method, key: key, params: params, cursor: next_cursor, count: remaining_count, &map_block).then do |next_results|
112
+ return (results + next_results)
117
113
  end
118
114
  end
119
115
  end
@@ -123,3 +119,26 @@ module ATProto
123
119
  end
124
120
  end
125
121
  end
122
+
123
+ module ATProto
124
+ module RequestUtils
125
+ class XRPCResponseCode < T::Enum
126
+ enums do
127
+ Unknown = new(1)
128
+ InvalidResponse = new(2)
129
+ Success = new(200)
130
+ InvalidRequest = new(400)
131
+ AuthRequired = new(401)
132
+ Forbidden = new(403)
133
+ XRPCNotSupported = new(404)
134
+ PayloadTooLarge = new(413)
135
+ RateLimitExceeded = new(429)
136
+ InternalServerError = new(500)
137
+ MethodNotImplemented = new(501)
138
+ UpstreamFailure = new(502)
139
+ NotEnoughResouces = new(503)
140
+ UpstreamTimeout = new(504)
141
+ end
142
+ end
143
+ end
144
+ end
@@ -66,6 +66,10 @@ module ATProto
66
66
  raise UnauthorizedError
67
67
  end
68
68
  end
69
+
70
+ def inspect
71
+ "#<ATProto::Session(did: #{did}, access_token: #{access_token})>"
72
+ end
69
73
  end
70
74
  end
71
75
 
@@ -1,4 +1,4 @@
1
1
  # typed: strict
2
2
  module ATProto
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.4.2"
4
4
  end
@@ -1,5 +1,7 @@
1
1
  # typed: true
2
2
  module ATProto
3
+ extend T::Sig
4
+
3
5
  class Writes < T::Struct
4
6
  class Write < T::Struct
5
7
  class Action < T::Enum
@@ -101,14 +103,13 @@ module ATProto
101
103
 
102
104
  class << self
103
105
  extend T::Sig
104
- sig { params(block: Proc).returns(T::Array[Write]) }
105
-
106
- def generate(&block)
107
- Collector.new.instance_eval(&block)
108
- end
109
106
  end
110
107
  end
111
108
 
109
+ def self.Writes(session, &block)
110
+ Writes.new(writes: Writes::Collector.new.instance_eval(&block), session: session, repo: Repo.new(session.did))
111
+ end
112
+
112
113
  class Writes
113
114
  class Collector
114
115
  include RequestUtils
data/lib/at_protocol.rb CHANGED
@@ -15,13 +15,21 @@ class Class
15
15
  end
16
16
  end
17
17
 
18
- require :"skyfall/cid".to_s
18
+ module ATProto
19
+ class << self
20
+ def method_missing(method_name, *args, &block)
21
+ if const_defined?(method_name)
22
+ Object.const_get(method_name).new(*args, &block)
23
+ else
24
+ super
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ require "skyfall/cid"
19
31
  require "xrpc"
20
- require "at_protocol/requests"
21
- require "at_protocol/session"
22
- require "at_protocol/repo"
23
- require "at_protocol/collection"
24
- require "at_protocol/at_uri"
25
- require "at_protocol/writes"
26
- require "at_protocol/record"
27
- require "at_protocol/helpers/strongref"
32
+
33
+ %w(requests session repo collection at_uri writes record record/strongref).each do |name|
34
+ require "at_protocol/#{name}"
35
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: at_protocol
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.4.2
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-08-16 00:00:00.000000000 Z
12
+ date: 2023-09-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: xrpc
@@ -36,6 +36,7 @@ files:
36
36
  - "./lib/at_protocol/at_uri.rb"
37
37
  - "./lib/at_protocol/collection.rb"
38
38
  - "./lib/at_protocol/record.rb"
39
+ - "./lib/at_protocol/record/strongref.rb"
39
40
  - "./lib/at_protocol/repo.rb"
40
41
  - "./lib/at_protocol/requests.rb"
41
42
  - "./lib/at_protocol/session.rb"