Skip to content

Commit

Permalink
Merge pull request #6 from jchayan/issue-5
Browse files Browse the repository at this point in the history
Issue 5
  • Loading branch information
jchayan authored Sep 12, 2020
2 parents 62137cf + 6797989 commit be98387
Show file tree
Hide file tree
Showing 10 changed files with 79 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])
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: 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
18 changes: 18 additions & 0 deletions spec/controllers/ovens_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,22 @@
end

end

describe 'GET progress' do
context "when not authenticated" do
before { sign_in nil }

it "blocks access" do
the_request
expect(response).to redirect_to new_user_session_path
end
end

context "when authenticated" do
before { get :progress }
it { expect(response.headers['Content-Type']).to eq("text/event-stream") }
after {response.stream.close unless response.stream.closed? }
end
end

end
20 changes: 20 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,23 @@
end
=end
end

# Trying to debug what's wrong with RSpec and ActionController::Live
# puts "rspec pid: #{Process.pid}"
#
# trap 'USR1' do
# threads = Thread.list
#
# puts
# puts "=" * 80
# puts "Received USR1 signal; printing all #{threads.count} thread backtraces."
#
# threads.each do |thr|
# description = thr == Thread.main ? "Main thread" : thr.inspect
# puts
# puts "#{description} backtrace: "
# puts thr.backtrace.join("\n")
# end
#
# puts "=" * 80
# end

0 comments on commit be98387

Please sign in to comment.