Skip to content

Instantly share code, notes, and snippets.

@lcjury
Created June 6, 2021 20:34
Show Gist options
  • Save lcjury/9549b0ede2ce04eca103f619ad7199cc to your computer and use it in GitHub Desktop.
Save lcjury/9549b0ede2ce04eca103f619ad7199cc to your computer and use it in GitHub Desktop.
Get yahoo finance data using async gem
# frozen_string_literal: true
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
gem "async", "~> 1.29"
gem "async-http", "~> 0.56.3"
gem "nokogiri", "~> 1.11"
#!/usr/bin/env ruby
# required gems: "async", "async-http", "nokogiri"
require 'async'
require 'async/barrier'
require 'async/http/internet'
require 'nokogiri'
require 'uri'
STOCKS = ["GLOB", "^IXIC", "^GSPC", "^RUT", "CL=F", "GC=F", "SI=F", "EURUSD=X", "^TNX", "^VIX", "GBPUSD=X", "JPY=X", "GBPUSD=X", "^CMC200", "^FTSE", "^N225"]
Async do
internet = Async::HTTP::Internet.new
barrier = Async::Barrier.new
STOCKS.each do |stock|
barrier.async do
response = internet.get "https://finance.yahoo.com/quote/#{URI.encode_www_form_component(stock)}"
doc = Nokogiri::HTML(response.read)
value = doc.css('.smartphone_Mt\(6px\) [data-reactid="32"]').first.text.gsub(',', '').to_f
puts "#{stock}: #{value}"
end
#The following line would make each request syncronous
#barrier.wait
end
ensure
internet&.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment