Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates oven page automatically when cookie is ready #6

Merged
merged 2 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Owner Author

@jchayan jchayan Sep 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoded Oven ID


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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes RSpec to stop working for some reason I don't fully understand

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See issue #9 for more information

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])
Copy link
Owner Author

@jchayan jchayan Sep 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security issue here: A User is able to query the Cookie status in other Users Ovens

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