Class: Nostrb::Filter
- Inherits:
-
Object
- Object
- Nostrb::Filter
- Defined in:
- lib/nostrb/filter.rb
Constant Summary collapse
- TAG =
/\A#([a-zA-Z])\z/
Instance Attribute Summary collapse
-
#authors ⇒ Object
readonly
Returns the value of attribute authors.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#kinds ⇒ Object
readonly
Returns the value of attribute kinds.
-
#limit ⇒ Object
Returns the value of attribute limit.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
- #add_authors(*pubkeys) ⇒ Object
- #add_ids(*event_ids) ⇒ Object
- #add_kinds(*kinds) ⇒ Object
- #add_tag(letter, list) ⇒ Object
-
#initialize ⇒ Filter
constructor
A new instance of Filter.
-
#match?(valid) ⇒ Boolean
Input Ruby hash as returned from SignedEvent.validate!.
- #since(hsh = nil) ⇒ Object
- #since=(int) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #until(hsh = nil) ⇒ Object
- #until=(int) ⇒ Object
Constructor Details
#initialize ⇒ Filter
Returns a new instance of Filter.
64 65 66 67 68 69 70 71 72 |
# File 'lib/nostrb/filter.rb', line 64 def initialize @ids = [] @authors = [] @kinds = [] @tags = {} @since = nil @until = nil @limit = nil end |
Instance Attribute Details
#authors ⇒ Object (readonly)
Returns the value of attribute authors.
62 63 64 |
# File 'lib/nostrb/filter.rb', line 62 def @authors end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
62 63 64 |
# File 'lib/nostrb/filter.rb', line 62 def ids @ids end |
#kinds ⇒ Object (readonly)
Returns the value of attribute kinds.
62 63 64 |
# File 'lib/nostrb/filter.rb', line 62 def kinds @kinds end |
#limit ⇒ Object
Returns the value of attribute limit.
62 63 64 |
# File 'lib/nostrb/filter.rb', line 62 def limit @limit end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
62 63 64 |
# File 'lib/nostrb/filter.rb', line 62 def @tags end |
Class Method Details
.ago(hsh) ⇒ Object
25 26 27 |
# File 'lib/nostrb/filter.rb', line 25 def self.ago(hsh) Time.now.to_i - Seconds.process(hsh) end |
.ingest(hash) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nostrb/filter.rb', line 29 def self.ingest(hash) f = Filter.new if ids = hash.delete("ids") f.add_ids(*ids) end if = hash.delete("authors") f.(*) end if kinds = hash.delete("kinds") f.add_kinds(*kinds) end if since = hash.delete("since") f.since = since end if _until = hash.delete("until") f.until = _until end if limit = hash.delete("limit") f.limit = limit end # anything left in hash should only be single letter tags hash.each { |tag, ary| if matches = tag.match(TAG) f.add_tag(matches[1], ary) else warn "unmatched tag: #{tag}" end } f end |
Instance Method Details
#add_authors(*pubkeys) ⇒ Object
82 83 84 |
# File 'lib/nostrb/filter.rb', line 82 def (*pubkeys) @authors += pubkeys.each { |pubkey| Nostrb.pubkey!(pubkey) } end |
#add_ids(*event_ids) ⇒ Object
78 79 80 |
# File 'lib/nostrb/filter.rb', line 78 def add_ids(*event_ids) @ids += event_ids.each { |id| Nostrb.id!(id) } end |
#add_kinds(*kinds) ⇒ Object
86 87 88 |
# File 'lib/nostrb/filter.rb', line 86 def add_kinds(*kinds) @kinds += kinds.each { |k| Nostrb.kind!(k) } end |
#add_tag(letter, list) ⇒ Object
90 91 92 93 |
# File 'lib/nostrb/filter.rb', line 90 def add_tag(letter, list) @tags[Nostrb.txt!(letter, length: 1)] = Nostrb.ary!(list, max: 99).each { |s| Nostrb.txt!(s) } end |
#match?(valid) ⇒ Boolean
Input
Ruby hash as returned from SignedEvent.validate!
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/nostrb/filter.rb', line 111 def match?(valid) return false if !@ids.empty? and !@ids.include?(valid["id"]) return false if !@authors.empty? and !@authors.include?(valid["pubkey"]) return false if !@kinds.empty? and !@kinds.include?(valid["kind"]) return false if !@since.nil? and @since > valid["created_at"] return false if !@until.nil? and @until < valid["created_at"] if !@tags.empty? = valid["tags"] @tags.each { |letter, ary| tag_match = false .each { |(tag, val)| next if tag_match if tag == letter return false if !ary.include?(val) tag_match = true end } return false unless tag_match } end true end |
#since(hsh = nil) ⇒ Object
95 |
# File 'lib/nostrb/filter.rb', line 95 def since(hsh = nil) = hsh.nil? ? @since : (@since = Filter.ago(hsh)) |
#since=(int) ⇒ Object
96 97 98 |
# File 'lib/nostrb/filter.rb', line 96 def since=(int) @since = int.nil? ? nil : Nostrb.int!(int) end |
#to_h ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/nostrb/filter.rb', line 134 def to_h h = Hash.new h["ids"] = @ids if !@ids.empty? h["authors"] = @authors if !@authors.empty? h["kinds"] = @kinds if !@kinds.empty? @tags.each { |letter, ary| h['#' + letter.to_s] = ary if !ary.empty? } h["since"] = @since unless @since.nil? h["until"] = @until unless @until.nil? h["limit"] = @limit unless @limit.nil? h.freeze end |
#to_s ⇒ Object
74 75 76 |
# File 'lib/nostrb/filter.rb', line 74 def to_s self.to_h.to_s end |