Class: Nostr::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/nostr/filter.rb

Overview

A filter determines what events will be sent in a subscription.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ Filter

Instantiates a new Filter

of these

Examples:

Nostr::Filter.new(
  ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
  authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
  kinds: [0, 1, 2],
  since: 1230981305,
  until: 1292190341,
  e: ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
  p: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']
)

Parameters:

  • kwargs (Hash)

Options Hash (**kwargs):

  • ids (Array<String>, nil)

    A list of event ids

  • authors (Array<String>, nil)

    A list of pubkeys, the pubkey of an event must be one

  • kinds (Array<Integer>, nil)

    A list of a kind numbers

  • e (Array<String>, nil)

    A list of event ids that are referenced in an “e” tag

  • p (Array<String, nil>)

    A list of pubkeys that are referenced in a “p” tag

  • since (Integer, nil)

    A timestamp, events must be newer than this to pass

  • until (Integer, nil)

    A timestamp, events must be older than this to pass

  • limit (Integer, nil)

    Maximum number of events to be returned in the initial query



120
121
122
123
124
125
126
127
128
129
# File 'lib/nostr/filter.rb', line 120

def initialize(**kwargs)
  @ids = kwargs[:ids]
  @authors = kwargs[:authors]
  @kinds = kwargs[:kinds]
  @e = kwargs[:e]
  @p = kwargs[:p]
  @since = kwargs[:since]
  @until = kwargs[:until]
  @limit = kwargs[:limit]
end

Instance Attribute Details

#authorsArray<String>? (readonly)

A list of pubkeys, the pubkey of an event must be one of these

Examples:

filter.authors # => ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']

Returns:

  • (Array<String>, nil)


26
27
28
# File 'lib/nostr/filter.rb', line 26

def authors
  @authors
end

#eArray<String>? (readonly)

A list of event ids that are referenced in an “e” tag

Examples:

filter.e # => ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2']

Returns:

  • (Array<String>, nil)


48
49
50
# File 'lib/nostr/filter.rb', line 48

def e
  @e
end

#idsArray<String>? (readonly)

A list of event ids

Examples:

filter.ids # => ['c24881c305c5cfb7c1168be7e9b0e150', '35deb2612efdb9e13e8b0ca4fc162341']

Returns:

  • (Array<String>, nil)


15
16
17
# File 'lib/nostr/filter.rb', line 15

def ids
  @ids
end

#kindsArray<Integer>? (readonly)

A list of a kind numbers

Examples:

filter.kinds # => [0, 1, 2]

Returns:

  • (Array<Integer>, nil)


37
38
39
# File 'lib/nostr/filter.rb', line 37

def kinds
  @kinds
end

#limitInteger? (readonly)

Maximum number of events to be returned in the initial query

Examples:

filter.limit # => 420

Returns:

  • (Integer, nil)


92
93
94
# File 'lib/nostr/filter.rb', line 92

def limit
  @limit
end

#pArray<String>? (readonly)

A list of pubkeys that are referenced in a “p” tag

Examples:

filter.p # => ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7']

Returns:

  • (Array<String>, nil)


59
60
61
# File 'lib/nostr/filter.rb', line 59

def p
  @p
end

#sinceInteger? (readonly)

A timestamp, events must be newer than this to pass

Examples:

filter.since # => 1230981305

Returns:

  • (Integer, nil)


70
71
72
# File 'lib/nostr/filter.rb', line 70

def since
  @since
end

#untilInteger? (readonly)

A timestamp, events must be older than this to pass

Examples:

filter.until # => 1292190341

Returns:

  • (Integer, nil)


81
82
83
# File 'lib/nostr/filter.rb', line 81

def until
  @until
end

Instance Method Details

#==(other) ⇒ Boolean

Compares two filters. Returns true if all attributes are equal and false otherwise

Examples:

filter == filter # => true

Returns:

  • (Boolean)

    True if all attributes are equal and false otherwise



171
172
173
# File 'lib/nostr/filter.rb', line 171

def ==(other)
  to_h == other.to_h
end

#to_hHash

Converts the filter to a hash, removing all empty attributes

Examples:

filter.to_h # =>
{
   ids: ['c24881c305c5cfb7c1168be7e9b0e150'],
   authors: ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
   kinds: [0, 1, 2],
   '#e': ['7bdb422f254194ae4bb86d354c0bd5a888fce233ffc77dceb3e844ceec1fcfb2'],
   '#p': ['000000001c5c45196786e79f83d21fe801549fdc98e2c26f96dcef068a5dbcd7'],
   since: 1230981305,
   until: 1292190341
}

Returns:

  • (Hash)

    The filter as a hash.



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nostr/filter.rb', line 149

def to_h
  {
    ids:,
    authors:,
    kinds:,
    '#e': e,
    '#p': p,
    since:,
    until: self.until,
    limit:
  }.compact
end