Add convenience methods to append event and pubkey references
This commit is contained in:
parent
4c9c35520d
commit
0df4dbb979
@ -138,6 +138,34 @@ module Nostr
|
||||
@content = content
|
||||
end
|
||||
|
||||
# Adds a reference to an event id as an 'e' tag
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Adding a reference to a pubkey
|
||||
# event_id = '189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408'
|
||||
# event.add_event_reference(event_id)
|
||||
#
|
||||
# @param event_id [String] 32-bytes hex-encoded event id.
|
||||
#
|
||||
# @return [Array<String>] The event's updated list of tags
|
||||
#
|
||||
def add_event_reference(event_id) = tags.push(['e', event_id])
|
||||
|
||||
# Adds a reference to a pubkey as a 'p' tag
|
||||
#
|
||||
# @api public
|
||||
#
|
||||
# @example Adding a reference to a pubkey
|
||||
# pubkey = '48df4af6e240ac5f7c5de89bf5941b249880be0e87d03685b178ccb1a315f52e'
|
||||
# event.add_pubkey_reference(pubkey)
|
||||
#
|
||||
# @param pubkey [String] 32-bytes hex-encoded public key.
|
||||
#
|
||||
# @return [Array<String>] The event's updated list of tags
|
||||
#
|
||||
def add_pubkey_reference(pubkey) = tags.push(['p', pubkey])
|
||||
|
||||
# Signs an event with the user's private key
|
||||
#
|
||||
# @api public
|
||||
|
@ -3,25 +3,37 @@ module Nostr
|
||||
attr_reader pubkey: String
|
||||
attr_reader created_at: Integer
|
||||
attr_reader kind: Integer
|
||||
attr_reader tags: Array[String]
|
||||
attr_reader tags: Array[Array[String]]
|
||||
attr_reader content: String
|
||||
attr_accessor id: String?|nil
|
||||
attr_accessor sig: String?|nil
|
||||
|
||||
def initialize: (pubkey: String, kind: Integer, content: String, ?created_at: Integer, ?tags: Array[String], ?id: String|nil, ?sig: String|nil) -> void
|
||||
def serialize: -> [Integer, String, Integer, Integer, Array[String], String]
|
||||
def initialize: (
|
||||
pubkey: String,
|
||||
kind: Integer,
|
||||
content: String,
|
||||
?created_at: Integer,
|
||||
?tags: Array[Array[String]],
|
||||
?id: String|nil,
|
||||
?sig: String|nil
|
||||
) -> void
|
||||
|
||||
def serialize: -> [Integer, String, Integer, Integer, Array[Array[String]], String]
|
||||
|
||||
def to_h: -> {
|
||||
id: String?|nil,
|
||||
pubkey: String,
|
||||
created_at: Integer,
|
||||
kind: Integer,
|
||||
tags: Array[String],
|
||||
tags: Array[Array[String]],
|
||||
content: String,
|
||||
sig: String?|nil
|
||||
}
|
||||
def ==: (Event other) -> bool
|
||||
|
||||
def sign:(String) -> Event
|
||||
|
||||
def add_event_reference: (String) -> Array[Array[String]]
|
||||
def add_pubkey_reference: (String) -> Array[Array[String]]
|
||||
end
|
||||
end
|
||||
|
@ -97,6 +97,54 @@ RSpec.describe Nostr::Event do
|
||||
end
|
||||
end
|
||||
|
||||
describe '#add_event_reference' do
|
||||
let(:event) do
|
||||
described_class.new(
|
||||
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
|
||||
kind: Nostr::EventKind::TEXT_NOTE,
|
||||
tags: [
|
||||
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
|
||||
],
|
||||
content: 'Your feedback is appreciated, now pay $8'
|
||||
)
|
||||
end
|
||||
|
||||
it 'appends a reference to an event to the event tags' do
|
||||
event.add_event_reference('189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408')
|
||||
|
||||
expect(event.tags).to eq(
|
||||
[
|
||||
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e],
|
||||
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#add_pubkey_reference' do
|
||||
let(:event) do
|
||||
described_class.new(
|
||||
pubkey: 'ccf9fdf3e1466d7c20969c71ec98defcf5f54aee088513e1b73ccb7bd770d460',
|
||||
kind: Nostr::EventKind::TEXT_NOTE,
|
||||
tags: [
|
||||
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408]
|
||||
],
|
||||
content: 'Your feedback is appreciated, now pay $8'
|
||||
)
|
||||
end
|
||||
|
||||
it 'appends a reference to a pubkey to the event tags' do
|
||||
event.add_pubkey_reference('472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e')
|
||||
|
||||
expect(event.tags).to eq(
|
||||
[
|
||||
%w[e 189df012cfff8a075785b884bd702025f4a7a37710f581c4ac9d33e24b585408],
|
||||
%w[p 472f440f29ef996e92a186b8d320ff180c855903882e59d50de1b8bd5669301e]
|
||||
]
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#content' do
|
||||
it 'exposes the event content' do
|
||||
expect(event.content).to eq('Your feedback is appreciated, now pay $8')
|
||||
|
Loading…
x
Reference in New Issue
Block a user