Sha256: 07b1d06f7db3f5116359e3a750233584daced14909dc7995c816cd1bc8257768

Contents?: true

Size: 1.55 KB

Versions: 10

Compression:

Stored size: 1.55 KB

Contents

# sqa/lib/sqa/ticker.rb
#
# Uses the https://dumbstockapi.com/ website to download a CSV file
#
# The CSV files have names like this:
#   "dumbstockapi-2023-09-21T16 39 55.165Z.csv"
#
# which has this header:
#   ticker,name,is_etf,exchange
#
# Not using the is_etf columns
#
class SQA::Ticker
  FILENAME_PREFIX = "dumbstockapi"
  CONNECTION      = Faraday.new(url: "https://dumbstockapi.com")
  @@data          = {}


  def self.download(country="US")
    response = CONNECTION.get("/stock?format=csv&countries=#{country.upcase}").to_hash

    if 200 == response[:status]
      filename = response[:response_headers]["content-disposition"].split('=').last.gsub('"','')
      out_path = Pathname.new(SQA.config.data_dir) + filename
      out_path.write response[:body]
    end

    response[:status]
  end


  def self.load
    tries = 0
    found = false

     until(found || tries >= 3) do
      files     = Pathname.new(SQA.config.data_dir).children.select{|c| c.basename.to_s.start_with?(FILENAME_PREFIX)}.sort
      if files.empty?
        download
        tries += 1
      else
        found = true
      end
    end

    raise "NoDataError" if files.empty?

    load_from_csv files.last
  end


  def self.load_from_csv(csv_path)
    CSV.foreach(csv_path, headers: true) do |row|
      @@data[row["ticker"]] = {
        name:     row["name"],
        exchange: row["exchange"]
      }
    end

    @@data
  end



  def self.data           = @@data.empty? ? load : @@data
  def self.lookup(ticker) = data[ticker.upcase]
  def self.valid?(ticker) = data.has_key?(ticker.upcase)
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
sqa-0.0.24 lib/sqa/ticker.rb
sqa-0.0.22 lib/sqa/ticker.rb
sqa-0.0.21 lib/sqa/ticker.rb
sqa-0.0.20 lib/sqa/ticker.rb
sqa-0.0.19 lib/sqa/ticker.rb
sqa-0.0.18 lib/sqa/ticker.rb
sqa-0.0.17 lib/sqa/ticker.rb
sqa-0.0.15 lib/sqa/ticker.rb
sqa-0.0.14 lib/sqa/ticker.rb
sqa-0.0.13 lib/sqa/ticker.rb