Class: Finviz::Crawler

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

Overview

Async way to fetch the data

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths: []) ⇒ Crawler

Returns a new instance of Crawler.



17
18
19
# File 'lib/finviz/crawler.rb', line 17

def initialize(paths: [])
  @paths = Array(paths)
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



21
22
23
# File 'lib/finviz/crawler.rb', line 21

def paths
  @paths
end

Class Method Details

.call(*args) ⇒ Object



12
13
14
# File 'lib/finviz/crawler.rb', line 12

def call(*args)
  new(*args).tap(&:fetch).result
end

Instance Method Details

#fetchObject



23
24
25
26
27
28
29
30
# File 'lib/finviz/crawler.rb', line 23

def fetch
  async do
    paths.each do |path|
      response = internet.get(path.to_s, headers)
      result << OpenStruct.new(path: path, html: Nokogiri::HTML(response.read))
    end
  end
end

#resultObject



32
33
34
# File 'lib/finviz/crawler.rb', line 32

def result
  @result ||= []
end