Skip to content

Commit

Permalink
Replace Sinatra with Rack application.
Browse files Browse the repository at this point in the history
  • Loading branch information
tristandunn committed Nov 6, 2024
1 parent 730db76 commit 2419849
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 35 deletions.
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ source "https://rubygems.org"
gemspec

gem "capybara", "3.40.0"
gem "mutex_m", "0.2.0"
gem "puma", "6.4.3"
gem "pusher", "2.0.3"
gem "rake", "13.2.1"
gem "rspec", "3.13.0"
gem "rubocop", "1.66.1"
gem "rubocop", "1.68.0"
gem "rubocop-capybara", "2.21.0"
gem "rubocop-performance", "1.22.1"
gem "rubocop-rake", "0.6.0"
gem "rubocop-rspec", "3.0.5"
gem "selenium-webdriver", "4.25.0"
gem "sinatra", "3.2.0"
gem "rubocop-rspec", "3.2.0"
gem "selenium-webdriver", "4.26.0"
gem "yard", "0.9.37"

group :test do
Expand Down
86 changes: 56 additions & 30 deletions spec/support/application.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
# frozen_string_literal: true

require "sinatra"
require "tilt/erb"

module Sinatra
class Application
set(:root, proc { File.join(File.dirname(__FILE__), "application") })
set(:views, proc { File.join(root, "views") })

disable :logging

get "/" do
erb :index
end

post "/pusher/auth" do
channel = Pusher[params[:channel_name]]
response = channel.authenticate(params[:socket_id], channel_data)

MultiJson.dump(response)
end

protected

def channel_data
return unless /^presence-/.match?(params[:channel_name])

{
user_id: params[:socket_id],
user_info: { name: "Alan Turing" }
}
require "rack"

module PusherFake
module Testing
class Application
def call(env)
request = Rack::Request.new(env)

case request.path
when "/"
index
when "/pusher/auth"
authenticate(request.params)
when %r{\A/javascripts}
asset(request.path)
else
[404, {}, []]
end
end

private

def asset(path)
headers = { "content-type" => "text/javascript" }
root = File.join(File.dirname(__FILE__), "application")
body = File.read(File.join(root, "public", path))

[200, headers, [body]]
end

def authenticate(params)
channel = Pusher[params["channel_name"]]
response = channel.authenticate(params["socket_id"], channel_data(params))
headers = { "Content-Type" => "application/json" }

[200, headers, [MultiJson.dump(response)]]
end

def channel_data(params)
return unless /^presence-/.match?(params["channel_name"])

{
user_id: params["socket_id"],
user_info: { name: "Alan Turing" }
}
end

def index
headers = { "content-type" => "text/html" }
root = File.join(File.dirname(__FILE__), "application")
template = File.read(File.join(root, "views", "index.erb"))
erb = ERB.new(template)
body = erb.result(binding)

[200, headers, [body]]
end
end
end
end
2 changes: 1 addition & 1 deletion spec/support/capybara.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

require "capybara/rspec"

Capybara.app = Sinatra::Application
Capybara.app = PusherFake::Testing::Application.new
Capybara.server = :puma, { Silent: true }
Capybara.default_driver = :selenium_chrome_headless

0 comments on commit 2419849

Please sign in to comment.