Class: Netbout::Search

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

Overview

Search.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(iri, token, query) ⇒ Search

Returns a new instance of Search.



32
33
34
35
36
# File 'lib/netbout/search.rb', line 32

def initialize(iri, token, query)
  @iri = iri
  @token = token
  @query = query
end

Instance Method Details

#eachObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/netbout/search.rb', line 46

def each
  entry = @iri.append('/search').add(q: @query).add(limit: '10')
  offset = 0
  loop do
    rsp = Netbout::Http.new(entry.over(offset: offset), @token).get
    json = JSON.parse(rsp.response_body)
    seen = 0
    json.each do |h|
      yield Netbout::Message.new(@iri, @token, h['id'])
      seen += 1
    end
    offset += seen
    break if seen.zero?
  end
end

#to_aObject



38
39
40
41
42
43
44
# File 'lib/netbout/search.rb', line 38

def to_a
  array = []
  each do |m|
    array << m
  end
  array
end