Skip to content

Commit 8d477d5

Browse files
committed
added YARD documentation for most classes
1 parent b86dfc4 commit 8d477d5

File tree

9 files changed

+320
-7
lines changed

9 files changed

+320
-7
lines changed

lib/didkit/at_handles.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
require_relative 'errors'
22

33
module DIDKit
4+
5+
#
6+
# @private
7+
#
8+
49
module AtHandles
510

611
private

lib/didkit/did.rb

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,52 @@
66
require_relative 'resolver'
77

88
module DIDKit
9+
10+
#
11+
# Represents a DID identifier (account on the ATProto network). This class serves as an entry
12+
# point to various lookup helpers. For convenience it can also be accessed as just `DID` without
13+
# the `DIDKit::` prefix.
14+
#
15+
# @example Resolving a handle
16+
# did = DID.resolve_handle('bsky.app')
17+
#
18+
919
class DID
1020
GENERIC_REGEXP = /\Adid\:\w+\:.+\z/
1121

1222
include Requests
1323

24+
# Resolve a handle into a DID. Looks up the given ATProto domain handle using the DNS TXT method
25+
# and the HTTP .well-known method and returns a DID if one is assigned using either of the methods.
26+
#
27+
# If a DID string or a {DID} object is passed, it simply returns that DID, so you can use this
28+
# method to pass it an input string from the user which can be a DID or handle, without having to
29+
# check which one it is.
30+
#
31+
# @param handle [String, DID] a domain handle (may start with an `@`) or a DID string
32+
# @return [DID, nil] resolved DID if found, nil otherwise
33+
1434
def self.resolve_handle(handle)
1535
Resolver.new.resolve_handle(handle)
1636
end
1737

18-
attr_reader :type, :did, :resolved_by
38+
# @return [Symbol] DID type (`:plc` or `:web`)
39+
attr_reader :type
40+
41+
# @return [String] DID identifier string
42+
attr_reader :did
43+
44+
# @return [Symbol, nil] `:dns` or `:http` if the DID was looked up using one of those methods
45+
attr_reader :resolved_by
46+
47+
alias to_s did
48+
49+
50+
# Create a DID object from a DID string.
51+
#
52+
# @param did [String, DID] DID string or another DID object
53+
# @param resolved_by [Symbol, nil] optionally, how the DID was looked up (`:dns` or `:http`)
54+
# @raise [DIDError] when the DID format or type is invalid
1955

2056
def initialize(did, resolved_by = nil)
2157
if did.is_a?(DID)
@@ -36,20 +72,39 @@ def initialize(did, resolved_by = nil)
3672
@resolved_by = resolved_by
3773
end
3874

39-
alias to_s did
75+
# Returns or looks up the DID document with the DID's identity details from an appropriate source.
76+
# This method caches the document in a local variable if it's called again.
77+
#
78+
# @return [Document] resolved DID document
4079

4180
def document
4281
@document ||= get_document
4382
end
4483

84+
# Looks up the DID document with the DID's identity details from an appropriate source.
85+
# @return [Document] resolved DID document
86+
4587
def get_document
4688
Resolver.new.resolve_did(self)
4789
end
4890

91+
# Returns the first verified handle assigned to this DID.
92+
#
93+
# Looks up the domain handles assigned to this DID in its DID document, checks if they are
94+
# verified (i.e. assigned correctly to this DID using DNS TXT or .well-known) and returns
95+
# the first handle that validates correctly, or nil if none matches.
96+
#
97+
# @return [String, nil] verified handle domain, if found
98+
4999
def get_verified_handle
50100
Resolver.new.get_verified_handle(document)
51101
end
52102

103+
# Fetches the PLC audit log (list of all previous operations) for a did:plc DID.
104+
#
105+
# @return [Array<PLCOperation>] list of PLC operations in the audit log
106+
# @raise [DIDError] when the DID is not a did:plc
107+
53108
def get_audit_log
54109
if @type == :plc
55110
PLCImporter.new.fetch_audit_log(self)
@@ -58,10 +113,23 @@ def get_audit_log
58113
end
59114
end
60115

116+
# Returns the domain portion of a did:web identifier.
117+
#
118+
# @return [String, nil] DID domain if the DID is a did:web, nil for did:plc
119+
61120
def web_domain
62121
did.gsub(/^did\:web\:/, '') if type == :web
63122
end
64123

124+
# Checks the status of the account/repo on its own PDS using the `getRepoStatus` endpoint.
125+
#
126+
# @param request_options [Hash] request options to override
127+
# @option request_options [Integer] :timeout request timeout (default: 15)
128+
# @option request_options [Integer] :max_redirects maximum number of redirects to follow (default: 5)
129+
#
130+
# @return [Symbol, nil] `:active`, or returned inactive status, or `nil` if account is not found
131+
# @raise [APIError] when the response is invalid
132+
65133
def account_status(request_options = {})
66134
doc = self.document
67135
return nil if doc.pds_endpoint.nil?
@@ -91,14 +159,31 @@ def account_status(request_options = {})
91159
end
92160
end
93161

162+
# Checks if the account is seen as active on its own PDS, using the `getRepoStatus` endpoint.
163+
# This is a helper which calls the {#account_status} method and checks if the status is `:active`.
164+
#
165+
# @return [Boolean] true if the returned status is active
166+
# @raise [APIError] when the response is invalid
167+
94168
def account_active?
95169
account_status == :active
96170
end
97171

172+
# Checks if the account exists its own PDS, using the `getRepoStatus` endpoint.
173+
# This is a helper which calls the {#account_status} method and checks if the repo is found at all.
174+
#
175+
# @return [Boolean] true if the returned status is valid, false if repo is not found
176+
# @raise [APIError] when the response is invalid
177+
98178
def account_exists?
99179
account_status != nil
100180
end
101181

182+
# Compares the DID to another DID object or string.
183+
#
184+
# @param other [DID, String] other DID to compare with
185+
# @return [Boolean] true if it's the same DID
186+
102187
def ==(other)
103188
if other.is_a?(String)
104189
self.did == other

lib/didkit/document.rb

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,39 @@
55
require_relative 'services'
66

77
module DIDKit
8+
9+
#
10+
# Parsed DID document from a JSON file loaded from [plc.directory](https://plc.directory) or a did:web domain.
11+
#
12+
# Use {DID#document} or {Resolver#resolve_did} to fetch a DID document and return this object.
13+
#
14+
815
class Document
916
include AtHandles
1017
include Services
1118

12-
attr_reader :json, :did, :handles, :services
19+
# @return [Hash] the complete JSON data of the DID document
20+
attr_reader :json
21+
22+
# @return [DID] the DID that this document describes
23+
attr_reader :did
24+
25+
# Returns a list of handles assigned to this DID in its DID document.
26+
#
27+
# Note: the handles aren't guaranteed to be verified (validated in the other direction).
28+
# Use {#get_verified_handle} to find a handle that is correctly verified.
29+
#
30+
# @return [Array<String>]
31+
attr_reader :handles
32+
33+
# @return [Array<ServiceRecords>] service records like PDS details assigned to the DID
34+
attr_reader :services
35+
36+
# Creates a DID document object.
37+
#
38+
# @param did [DID] DID object
39+
# @param json [Hash] DID document JSON
40+
# @raise [FormatError] when required fields are missing or invalid.
1341

1442
def initialize(did, json)
1543
raise FormatError, "Missing id field" if json['id'].nil?
@@ -23,10 +51,19 @@ def initialize(did, json)
2351
@handles = parse_also_known_as(json['alsoKnownAs'] || [])
2452
end
2553

54+
# Returns the first verified handle assigned to the DID.
55+
#
56+
# Looks up the domain handles assigned to this DID in the DID document, checks if they are
57+
# verified (i.e. assigned correctly to this DID using DNS TXT or .well-known) and returns
58+
# the first handle that validates correctly, or nil if none matches.
59+
#
60+
# @return [String, nil] verified handle domain, if found
61+
2662
def get_verified_handle
2763
Resolver.new.get_verified_handle(self)
2864
end
2965

66+
3067
private
3168

3269
def parse_services(service_data)

lib/didkit/errors.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
module DIDKit
2-
class DIDError < StandardError
3-
end
42

3+
#
4+
# Raised when an HTTP request returns a response with an error status.
5+
#
56
class APIError < StandardError
7+
8+
# @return [Net::HTTPResponse] the returned HTTP response
69
attr_reader :response
710

11+
# @param response [Net::HTTPResponse] the returned HTTP response
812
def initialize(response)
913
@response = response
1014
super("APIError: #{response}")
1115
end
1216

17+
# @return [Integer] HTTP status code
1318
def status
1419
response.code.to_i
1520
end
1621

22+
# @return [String] HTTP response body
1723
def body
1824
response.body
1925
end
2026
end
2127

28+
#
29+
# Raised when a string is not a valid DID or not of the right type.
30+
#
31+
class DIDError < StandardError
32+
end
33+
34+
#
35+
# Raised when the loaded data has some missing or invalid fields.
36+
#
2237
class FormatError < StandardError
2338
end
2439
end

lib/didkit/plc_operation.rb

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,53 @@
66
require_relative 'services'
77

88
module DIDKit
9+
10+
#
11+
# Represents a single operation of changing a specific DID's data in the [plc.directory](https://plc.directory)
12+
# (e.g. changing assigned handles or migrating to a different PDS).
13+
#
14+
915
class PLCOperation
1016
include AtHandles
1117
include Services
1218

13-
attr_reader :json, :did, :cid, :seq, :created_at, :type, :handles, :services
19+
# @return [Hash] the JSON from which the operation is parsed
20+
attr_reader :json
21+
22+
# @return [String] the DID which the operation concerns
23+
attr_reader :did
24+
25+
# @return [String] CID (Content Identifier) of the operation
26+
attr_reader :cid
27+
28+
# Returns a sequential number of the operation (only used in the new export API).
29+
# @return [Integer, nil] sequential number of the operation
30+
attr_reader :seq
31+
32+
# @return [Time] time when the operation was created
33+
attr_reader :created_at
34+
35+
# Returns the `type` field of the operation (usually `"plc_operation"`).
36+
# @return [String] the operation type
37+
attr_reader :type
38+
39+
# Returns a list of handles assigned to the DID in this operation.
40+
#
41+
# Note: the handles aren't guaranteed to be verified (validated in the other direction).
42+
# Use {DID#get_verified_handle} or {Document#get_verified_handle} to find a handle that is
43+
# correctly verified.
44+
#
45+
# @return [Array<String>]
46+
attr_reader :handles
47+
48+
# @return [Array<ServiceRecords>] service records like PDS details assigned to the DID
49+
attr_reader :services
50+
51+
52+
# Creates a PLCOperation object.
53+
#
54+
# @param json [Hash] operation JSON
55+
# @raise [FormatError] when required fields are missing or invalid
1456

1557
def initialize(json)
1658
@json = json

lib/didkit/requests.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
require_relative 'errors'
66

77
module DIDKit
8+
9+
#
10+
# @private
11+
#
12+
813
module Requests
914

1015
private

0 commit comments

Comments
 (0)