Skip to content

Commit dca908e

Browse files
committed
got stock-ticker working
1 parent babefe3 commit dca908e

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

ruby/examples/misc/stock-tickers/app/hyperstack/components/app.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class App < HyperComponent
1212
INPUT(placeholder: 'enter a stock symbol').on(:key_down) do |evt|
1313
next unless evt.key_code == 13
1414
mutate @symbols << evt.target.value
15+
evt.target.value = ''
1516
end
1617
end
1718
end

ruby/examples/misc/stock-tickers/app/hyperstack/components/display_ticker.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class DisplayTicker < HyperComponent
22
param :symbol
3-
before_mount { @ticker = StockTicker.new(params.symbol) }
3+
before_mount { @ticker = StockTicker.new(params.symbol, 10.seconds) }
44
render(DIV) do
55
"#{params.symbol.upcase} current price: #{@ticker.price}"
66
end
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
class StockTicker
22
include Hyperstack::State::Observable
33

4+
state_reader :price
5+
attr_reader :symbol
6+
7+
def initialize(symbol, update_interval = 5.minutes)
8+
@symbol = symbol
9+
fetch
10+
every(update_interval) { fetch }
11+
end
12+
413
def fetch
14+
puts "fetching #{@symbol}"
515
HTTP.get("https://api.iextrading.com/1.0/stock/#{@symbol}/delayed-quote").then do |response|
616
mutate @price = response.json[:delayedPrice]
717
end
818
end
919

10-
def initialize(symbol, update_interval = 5.minutes)
11-
@symbol = symbol
12-
fetch
13-
every(update_interval) { fetch }
20+
before_unmount do
21+
# just to demonstrate use of before_unmount - this is not needed
22+
# but if there were some other cleanups you needed you could put them here
23+
# however intervals (every) delays (after) and websocket receivers are all
24+
# cleaned up automatically
25+
puts "cancelling #{@symbol}"
1426
end
1527

16-
state_reader :price
17-
attr_reader :symbol
1828
end

ruby/examples/misc/stock-tickers/app/views/layouts/application.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html>
33
<head>
4-
<title>SmokeTest</title>
4+
<title>Stock Tickers Example</title>
55
<%= csrf_meta_tags %>
66
<%= csp_meta_tag %>
77

ruby/examples/misc/stock-tickers/config/initializers/hyperstack.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Hyperstack.configuration do |config|
22
config.prerendering = :on
3-
config.import 'hyperstack-jquery'
3+
config.import 'jquery', client_only: true
4+
config.import 'hyperstack/component/jquery', client_only: true
45
config.import 'browser/interval'
56
config.import 'active_support'
67
config.import 'hyperstack/hotloader', client_only: true if Rails.env.development?

0 commit comments

Comments
 (0)