Class: SchwabRb::DataObjects::MarketHours

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/schwab_rb/data_objects/market_hours.rb

Defined Under Namespace

Classes: MarketInfo, SessionHours, SessionPeriod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ MarketHours

Returns a new instance of MarketHours.



14
15
16
17
18
19
20
21
22
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 14

def initialize(data)
  @markets = {}
  data.each do |market_type, market_data|
    @markets[market_type] = {}
    market_data.each do |product_key, product_data|
      @markets[market_type][product_key] = MarketInfo.new(product_data)
    end
  end
end

Instance Attribute Details

#marketsObject (readonly)

Returns the value of attribute markets.



6
7
8
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 6

def markets
  @markets
end

Class Method Details

.build(data) ⇒ Object



9
10
11
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 9

def build(data)
  new(data)
end

Instance Method Details

#all_closed?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 102

def all_closed?
  !any_open?
end

#any_open?Boolean

Returns:

  • (Boolean)


96
97
98
99
100
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 96

def any_open?
  @markets.any? do |_, market_data|
    market_data.any? { |_, market_info| market_info.open? }
  end
end

#bondObject



51
52
53
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 51

def bond
  @markets["bond"] || {}
end

#closed_marketsObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 83

def closed_markets
  result = {}
  @markets.each do |market_type, market_data|
    market_data.each do |product_key, market_info|
      unless market_info.open?
        result[market_type] ||= {}
        result[market_type][product_key] = market_info
      end
    end
  end
  result
end

#each(&block) ⇒ Object



118
119
120
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 118

def each(&block)
  each_market(&block)
end

#each_marketObject



106
107
108
109
110
111
112
113
114
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 106

def each_market
  return enum_for(:each_market) unless block_given?

  @markets.each do |market_type, market_data|
    market_data.each do |product_key, market_info|
      yield(market_type, product_key, market_info)
    end
  end
end

#equityObject



35
36
37
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 35

def equity
  @markets["equity"] || {}
end

#find_by_market_type(market_type) ⇒ Object



59
60
61
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 59

def find_by_market_type(market_type)
  @markets[market_type.to_s]
end

#find_market_info(market_type, product_key) ⇒ Object



63
64
65
66
67
68
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 63

def find_market_info(market_type, product_key)
  market_data = find_by_market_type(market_type)
  return nil unless market_data

  market_data[product_key.to_s]
end

#forexObject



47
48
49
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 47

def forex
  @markets["forex"] || {}
end

#futureObject



43
44
45
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 43

def future
  @markets["future"] || {}
end

#market_typesObject



55
56
57
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 55

def market_types
  @markets.keys
end

#open_marketsObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 70

def open_markets
  result = {}
  @markets.each do |market_type, market_data|
    market_data.each do |product_key, market_info|
      if market_info.open?
        result[market_type] ||= {}
        result[market_type][product_key] = market_info
      end
    end
  end
  result
end

#optionObject



39
40
41
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 39

def option
  @markets["option"] || {}
end

#to_hObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/schwab_rb/data_objects/market_hours.rb', line 24

def to_h
  result = {}
  @markets.each do |market_type, market_data|
    result[market_type] = {}
    market_data.each do |product_key, market_info|
      result[market_type][product_key] = market_info.to_h
    end
  end
  result
end