Class: Nostr::Filter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ids: nil, kinds: nil, authors: nil, since: nil, limit: nil, search: nil, **params) ⇒ Filter

Returns a new instance of Filter.


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/filter.rb', line 15

def initialize(ids: nil, kinds: nil, authors: nil, since: nil, limit: nil, search: nil, **params)
  @id = id
  @kinds = kinds
  @authors = authors
  @tags = tags
  @since = since.nil? ? nil : since.to_i
  @limit = limit
  @search = search
  @until = params[:until].nil? ? nil : params[:until].to_i # this is an hack to permit the use of the 'until' param, since it is a reserved word

  # Handle additional parameters with a-zA-Z names
  params.each do |key, value|
    if key.to_s.match?(/\A[a-zA-Z]\z/)
      instance_variable_set("@#{key}", value)
    end
  end
end

Instance Attribute Details

#authorsObject (readonly)

Returns the value of attribute authors.


6
7
8
# File 'lib/filter.rb', line 6

def authors
  @authors
end

#idObject (readonly)

Returns the value of attribute id.


4
5
6
# File 'lib/filter.rb', line 4

def id
  @id
end

#kindsObject (readonly)

Returns the value of attribute kinds.


5
6
7
# File 'lib/filter.rb', line 5

def kinds
  @kinds
end

#limitObject (readonly)

Returns the value of attribute limit.


10
11
12
# File 'lib/filter.rb', line 10

def limit
  @limit
end

#searchObject (readonly)

Returns the value of attribute search.


11
12
13
# File 'lib/filter.rb', line 11

def search
  @search
end

#sinceObject (readonly)

Returns the value of attribute since.


8
9
10
# File 'lib/filter.rb', line 8

def since
  @since
end

#tagsObject (readonly)

Returns the value of attribute tags.


7
8
9
# File 'lib/filter.rb', line 7

def tags
  @tags
end

#untilObject (readonly)

Returns the value of attribute until.


9
10
11
# File 'lib/filter.rb', line 9

def until
  @until
end

Instance Method Details

#to_hObject


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/filter.rb', line 33

def to_h
  result = {
    id: @id,
    authors: @authors,
    kinds: @kinds,
    since: @since,
    until: @until,
    limit: @limit,
    search: @search
  }.compact

  ('a'..'z').each do |char|
    var_value = instance_variable_get("@#{char}")
    result["##{char}"] = var_value unless var_value.nil?
  end
  ('A'..'Z').each do |char|
    var_value = instance_variable_get("@#{char}")
    result["##{char}"] = var_value unless var_value.nil?
  end

  result
end