class Finviz::QuotesFetcher

Fetch the details of the security

Attributes

tickers[R]

Public Class Methods

dangerous_methods() click to toggle source
# File lib/finviz/quotes_fetcher.rb, line 7
def self.dangerous_methods
  @dangerous_methods ||= OpenStruct.instance_methods
    .map(&:to_s)
    .select {|m| m.length <=5 }
    .reject {|m| m =~ /!|\?|\>|\<|\=|_|\[|\]/ }
end
new(tickers: []) click to toggle source
# File lib/finviz/quotes_fetcher.rb, line 14
def initialize(tickers: [])
  @tickers = Array(tickers)
end

Public Instance Methods

call() click to toggle source
# File lib/finviz/quotes_fetcher.rb, line 20
def call
  Quotes.new.tap do |result|
    all_pages.each do |page|
      page.html.css("#ticker").zip(page.html.css(".snapshot-table2")).each do |(ticker_xpath, table_xpath)|
        result.add_quote_from_xpath(ticker_xpath, table_xpath)
      end
    end

    self.class.dangerous_methods.each { |m| result.instance_eval("undef :#{m}") }
  end
end

Private Instance Methods

all_pages() click to toggle source
# File lib/finviz/quotes_fetcher.rb, line 34
def all_pages
  @all_pages ||= Crawler.call(paths: paths)
end
paths() click to toggle source
# File lib/finviz/quotes_fetcher.rb, line 38
def paths
  @paths ||= tickers
             .each_slice(Finviz.config.quotes_fetcher.max_tickers_per_page)
             .map { |slice| uri slice }
end
uri(tickers_array) click to toggle source
# File lib/finviz/quotes_fetcher.rb, line 44
def uri(tickers_array)
  query = CGI.unescape(URI.encode_www_form({ t: tickers_array.join(",") }))
  URI::HTTPS.build(host: "finviz.com", path: "/quote.ashx", query: query)
end