Class: SQA::DataFrame::Data

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

Overview

Data class to store stock metadata

This class holds metadata about a stock including its ticker symbol, name, exchange, data source, technical indicators, and company overview.

Examples:

Creating from named parameters

data = SQA::DataFrame::Data.new(
  ticker: 'AAPL',
  source: :alpha_vantage,
  indicators: { rsi: [30, 70], sma: [20, 50] }
)

Creating from hash (JSON)

json_data = JSON.parse(File.read('stock_data.json'))
data = SQA::DataFrame::Data.new(json_data)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_hash = nil, ticker: nil, name: nil, exchange: nil, source: :alpha_vantage, indicators: {}, overview: {}) ⇒ Data

Initializes stock metadata.

Can be called in two ways:

1. With a hash: SQA::DataFrame::Data.new(hash) - for JSON deserialization
2. With keyword args: SQA::DataFrame::Data.new(ticker: 'AAPL', source: :alpha_vantage, ...)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sqa/data_frame/data.rb', line 52

def initialize(data_hash = nil, ticker: nil, name: nil, exchange: nil, source: :alpha_vantage, indicators: {}, overview: {})
  if data_hash.is_a?(Hash) && ticker.nil?
    # Initialize from hash (JSON deserialization) - called as: new(hash)
    @ticker = data_hash['ticker'] || data_hash[:ticker]
    @name = data_hash['name'] || data_hash[:name]
    @exchange = data_hash['exchange'] || data_hash[:exchange]
    @source = data_hash['source'] || data_hash[:source] || source
    @indicators = data_hash['indicators'] || data_hash[:indicators] || {}
    @overview = data_hash['overview'] || data_hash[:overview] || {}

    # Convert source to symbol if it's a string
    @source = @source.to_sym if @source.is_a?(String)
  else
    # Initialize from named parameters - called as: new(ticker: 'AAPL', ...)
    @ticker = ticker
    @name = name
    @exchange = exchange
    @source = source
    @indicators = indicators
    @overview = overview
  end
end

Instance Attribute Details

#exchangeString?



36
# File 'lib/sqa/data_frame/data.rb', line 36

attr_accessor :ticker, :name, :exchange, :source, :indicators, :overview

#indicatorsHash



36
# File 'lib/sqa/data_frame/data.rb', line 36

attr_accessor :ticker, :name, :exchange, :source, :indicators, :overview

#nameString?



36
# File 'lib/sqa/data_frame/data.rb', line 36

attr_accessor :ticker, :name, :exchange, :source, :indicators, :overview

#overviewHash



36
# File 'lib/sqa/data_frame/data.rb', line 36

attr_accessor :ticker, :name, :exchange, :source, :indicators, :overview

#sourceSymbol



36
# File 'lib/sqa/data_frame/data.rb', line 36

attr_accessor :ticker, :name, :exchange, :source, :indicators, :overview

#tickerString?



36
37
38
# File 'lib/sqa/data_frame/data.rb', line 36

def ticker
  @ticker
end

Instance Method Details

#to_hHash

Convert to hash



85
86
87
88
89
90
91
92
93
94
# File 'lib/sqa/data_frame/data.rb', line 85

def to_h
  {
    ticker: @ticker,
    name: @name,
    exchange: @exchange,
    source: @source,
    indicators: @indicators,
    overview: @overview
  }
end

#to_json(*args) ⇒ String

Serialize to JSON string



78
79
80
# File 'lib/sqa/data_frame/data.rb', line 78

def to_json(*args)
  to_h.to_json(*args)
end

#to_sString Also known as: inspect

String representation



99
100
101
# File 'lib/sqa/data_frame/data.rb', line 99

def to_s
  "#{@ticker || 'Unknown'} (#{@exchange || 'N/A'}) via #{@source}"
end