Class: SQA::Ticker
- Inherits:
-
Object
- Object
- SQA::Ticker
- Defined in:
- lib/sqa/ticker.rb
Overview
sqa/lib/sqa/ticker.rb
Stock ticker symbol validation and lookup using the dumbstockapi.com service. Downloads and caches a CSV file containing ticker symbols, company names, and exchanges.
Constant Summary collapse
- FILENAME_PREFIX =
Returns Prefix for downloaded CSV filenames.
"dumbstockapi"- CONNECTION =
Returns Connection to dumbstockapi.com.
Faraday.new(url: "https://dumbstockapi.com")
Class Method Summary collapse
-
.data ⇒ Hash{String => Hash}
Returns the cached ticker data, loading it if necessary.
-
.download(country = "US") ⇒ Integer
Downloads ticker data from dumbstockapi.com and saves to data directory.
-
.load ⇒ Hash{String => Hash}
Loads ticker data from cached CSV or downloads if not available.
-
.load_from_csv(csv_path) ⇒ Hash{String => Hash}
Loads ticker data from a specific CSV file.
-
.lookup(ticker) ⇒ Hash?
Looks up information for a specific ticker symbol.
-
.reset! ⇒ Hash
Resets the cached ticker data.
-
.valid?(ticker) ⇒ Boolean
Checks if a ticker symbol is valid (exists in the data).
Class Method Details
.data ⇒ Hash{String => Hash}
Returns the cached ticker data, loading it if necessary.
92 93 94 95 |
# File 'lib/sqa/ticker.rb', line 92 def data @data ||= {} @data.empty? ? load : @data end |
.download(country = "US") ⇒ Integer
Downloads ticker data from dumbstockapi.com and saves to data directory.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sqa/ticker.rb', line 31 def 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 |
.load ⇒ Hash{String => Hash}
Loads ticker data from cached CSV or downloads if not available. Retries download up to 3 times if no cached file exists.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sqa/ticker.rb', line 47 def 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? begin download rescue StandardError => e warn "Warning: Could not download ticker list: #{e.message}" if $VERBOSE end tries += 1 else found = true end end if files.empty? warn "Warning: No ticker validation data available. Proceeding without validation." if $VERBOSE return {} end load_from_csv files.last end |
.load_from_csv(csv_path) ⇒ Hash{String => Hash}
Loads ticker data from a specific CSV file.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sqa/ticker.rb', line 77 def load_from_csv(csv_path) @data ||= {} CSV.foreach(csv_path, headers: true) do |row| @data[row["ticker"]] = { name: row["name"], exchange: row["exchange"] } end @data end |
.lookup(ticker) ⇒ Hash?
Looks up information for a specific ticker symbol.
106 107 108 109 |
# File 'lib/sqa/ticker.rb', line 106 def lookup(ticker) return nil if ticker.nil? || ticker.to_s.empty? data[ticker.to_s.upcase] end |
.reset! ⇒ Hash
Resets the cached ticker data. Useful for testing to force a fresh load.
129 130 131 |
# File 'lib/sqa/ticker.rb', line 129 def reset! @data = {} end |
.valid?(ticker) ⇒ Boolean
Checks if a ticker symbol is valid (exists in the data).
120 121 122 123 |
# File 'lib/sqa/ticker.rb', line 120 def valid?(ticker) return false if ticker.nil? || ticker.to_s.empty? data.key?(ticker.to_s.upcase) end |