Class: ATProto::Writes::Write

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/at_protocol/writes.rb

Defined Under Namespace

Classes: Action

Instance Method Summary collapse

Instance Method Details

#endpoint_nameObject

If you want to use with individual actions instead of applyWrites:



34
35
36
37
38
39
40
41
42
43
# File 'lib/at_protocol/writes.rb', line 34

def endpoint_name
  case self.action
  when Action::Create
    "com_atproto_repo_createRecord"
  when Action::Update
    "com_atproto_repo_putRecord"
  when Action::Delete
    "com_atproto_repo_deleteRecord"
  end
end

#to_hObject



23
24
25
26
27
28
29
30
31
# File 'lib/at_protocol/writes.rb', line 23

def to_h
  {
    :"$type" => "com.atproto.repo.applyWrites##{self.action.serialize}",
    action: self.action.serialize,
    value: self.value || nil,
    collection: self.collection,
    rkey: self.rkey || nil,
  }.compact
end

#to_individual_hash(session) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/at_protocol/writes.rb', line 45

def to_individual_hash(session)
  case self.action
  when Action::Create
    {
      repo: session.did,
      collection: self.collection.to_s,
      record: self.value,
      rkey: self.rkey || nil,
    }.compact
  when Action::Update
    {
      repo: session.did,
      collection: self.collection.to_s,
      rkey: self.rkey,
      record: self.value,
    }.compact
  when Action::Delete
    {
      repo: session.did,
      collection: self.collection.to_s,
      rkey: self.rkey,
    }.compact
  end
end

#to_procObject



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

def to_proc
  ->session {
    session.xrpc.post.send(self.endpoint_name, **self.to_individual_hash(session)).then do |response|
      if response.is_a?(Numeric)
        response
      elsif response.is_a?(Hash)
        ATProto::Record::StrongRef.new(
          uri: RequestUtils.at_uri(response["uri"]),
          cid: Skyfall::CID.from_json(response["cid"]),
        )
      end
    end
  }
end