Skip to content

Commit

Permalink
Updates oven page automatically when cookie is ready
Browse files Browse the repository at this point in the history
- Puts cookie retrieval markup in separate partial
- Adds SSE support on ovens controller
- Adds SSE listener on the client side

Issue: #5
  • Loading branch information
jchayan committed Sep 12, 2020
1 parent 62137cf commit 63c9fe2
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 4 deletions.
11 changes: 11 additions & 0 deletions app/assets/javascripts/ovens/show.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
document.addEventListener('DOMContentLoaded', () => {
let cookieStatus, eventSource;

cookieStatus = document.querySelector('.cookie-status');
eventSource = new EventSource('/ovens/2/progress');

eventSource.addEventListener('message', event => {
cookieStatus.innerHTML = event.data;
eventSource.close();
});
})
18 changes: 18 additions & 0 deletions app/controllers/ovens_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class OvensController < ApplicationController
include ActionController::Live
SSE_RETRY_TIME = 5000.freeze

before_action :authenticate_user!

def index
Expand All @@ -16,4 +19,19 @@ def empty
end
redirect_to @oven, alert: 'Oven emptied!'
end

def progress
@oven = Oven.find(params[:id])

This comment has been minimized.

Copy link
@jchayan

jchayan Sep 12, 2020

Author Owner

Security issue here: A user is able to query the cookie status of other users

sse = SSE.new(response.stream)
response.headers['Content-Type'] = 'text/event-stream'

return sse.close if @oven.cookie.ready? == false

template = render_to_string(partial: 'cookie.ready', formats: [:html])
sse.write(template, retry: SSE_RETRY_TIME)
rescue IOError
Rails.Logger.error(IOError)
ensure
sse.close if sse.present?
end
end
2 changes: 2 additions & 0 deletions app/views/ovens/_cookie.ready.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(Your Cookie is Ready)
= button_to "Retrieve Cookie", empty_oven_path, class: 'button tiny'
7 changes: 4 additions & 3 deletions app/views/ovens/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
Cookie in oven:
- if @oven.cookie
1 cookie with #{show_cookie_filling(@oven.cookie)}
- if @oven.cookie.ready?
(Your Cookie is Ready)
= button_to "Retrieve Cookie", empty_oven_path, class: 'button tiny'
.cookie-status
- if @oven.cookie.ready?
= render 'ovens/cookie.ready'
- else
None

%br

= link_to "Prepare Cookie", new_oven_cookies_path(@oven), class: 'button'
= javascript_include_tag "ovens/show"
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

config.allow_concurrency = true

# Do not eager load code on boot.
config.eager_load = false

Expand Down
2 changes: 2 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Code is not reloaded between requests.
config.cache_classes = true

config.allow_concurrency = true

# Eager load code on boot. This eager loads most of Rails and
# your application in memory, allowing both threaded web servers
# and those relying on copy on write to perform better.
Expand Down
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# and recreated between test runs. Don't rely on the data there!
config.cache_classes = true

config.allow_concurrency = true

# Do not eager load code on boot. This avoids loading your whole application
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w[admin.js admin.css]
Rails.application.config.assets.precompile += %w[vendor/modernizr]
Rails.application.config.assets.precompile += %w[vendor/modernizr ovens/show]
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
resources :ovens do
resource :cookies
member do
get :progress
post :empty
end
end
Expand Down

0 comments on commit 63c9fe2

Please sign in to comment.