Class: SQA::Ticker

Inherits:
Object
  • Object
show all
Defined in:
lib/sqa/ticker.rb

Overview

sqa/lib/sqa/ticker.rb

Uses the 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

Constant Summary collapse

FILENAME_PREFIX =
"dumbstockapi"
CONNECTION =
Faraday.new(url: "https://dumbstockapi.com")
@@data =
{}

Class Method Summary collapse

Class Method Details

.dataObject



65
# File 'lib/sqa/ticker.rb', line 65

def self.data           = @@data.empty? ? load : @@data

.download(country = "US") ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sqa/ticker.rb', line 19

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

.loadObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sqa/ticker.rb', line 32

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

.load_from_csv(csv_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/sqa/ticker.rb', line 52

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

.lookup(ticker) ⇒ Object



66
# File 'lib/sqa/ticker.rb', line 66

def self.lookup(ticker) = data[ticker.upcase]

.valid?(ticker) ⇒ Boolean

Returns:

  • (Boolean)


67
# File 'lib/sqa/ticker.rb', line 67

def self.valid?(ticker) = data.has_key?(ticker.upcase)