lib/flammarion/engraving.rb in flammarion_rails-0.2.1 vs lib/flammarion/engraving.rb in flammarion_rails-0.2.2
- old
+ new
@@ -3,25 +3,24 @@
include Revelator
include RecognizePath
attr_accessor :on_disconnect, :on_connect, :sockets, :request, :status, :headers, :response
+ PROTOCOL = /^.*:\/{2}(:\d{0,4})?/i
+
# Creates a new Engraving (i.e., a new display window)
# @option options [Proc] :on_connect Called when the display window is
# connected (i.e., displayed)
# @option options [Proc] :on_disconnect Called when the display windows is
# disconnected (i.e., closed)
- # @option options [Boolean] :exit_on_disconnect (false) Will call +exit+
- # when the widow is closed if this option is true.
# @raise {SetupError} if chrome is not set up correctly and
# and Flammarion is unable to display the engraving.
def initialize(**options)
@chrome = OpenStruct.new
@sockets = []
@on_connect = options[:on_connect]
@on_disconnect = options[:on_disconnect]
- @exit_on_disconnect = options.fetch(:exit_on_disconnect, false)
start_server
@window_id = @@server.register_window(self)
open_a_window(options) unless options[:no_window]
wait_for_a_connection unless options[:no_wait]
@@ -33,21 +32,21 @@
sleep 1 until @sockets.empty?
end
def disconnect(ws)
@sockets.delete ws
- exit 0 if @exit_on_disconnect
+ exit 0
@on_disconnect.call if @on_disconnect
end
def process_message(msg)
params = JSON.parse(msg).with_indifferent_access
action = params.delete(:action) || 'page'
dispatch(params)
if status == 302
- dispatch(url: headers['Location'].sub(/^.*:\/{2}(:\d{0,4})?/i, ''), session: response.request.session)
+ dispatch(url: headers['Location'].sub(PROTOCOL, ''), session: response.request.session)
render(action: 'page', body: response.body)
elsif headers['Content-Transfer-Encoding'] == 'binary'
filename = headers['Content-Disposition'].sub(/.*filename=/, '').gsub(/(^"|"$)/, '')
render(action: 'file', name: filename)
render(response.body)
@@ -65,15 +64,16 @@
def dispatch(params)
session = params.delete(:session)
url = params.delete(:url)
uri = URI.parse(url)
query_params = Rack::Utils.parse_nested_query(uri.query)
+ request_params = {}
if params.key?(:form)
params[:method] = 'post'
- params[params.delete(:button)] = ''
- params.merge!(Rack::Utils.parse_nested_query(params.delete(:form)))
+ request_params = Rack::Utils.parse_nested_query(params.delete(:form))
+ request_params[params.delete(:button)] = ''
end
if params.key?(:_method)
params[:method] = params[:_method]
end
http_method = (params[:method] ||= :get).to_s.upcase!
@@ -86,12 +86,18 @@
controller_name = "#{path_params[:controller].underscore.camelize}Controller"
controller = ActiveSupport::Dependencies.constantize(controller_name)
action = path_params[:action] || 'index'
request_env = {
'rack.input' => '',
+ 'QUERY_STRING' => uri.query,
'REQUEST_METHOD' => http_method,
- 'action_dispatch.request.parameters' => params.merge!(path_params),
+ 'REQUEST_PATH' => uri.path,
+ 'REQUEST_URI' => url,
+ 'PATH_INFO' => uri.path,
+ 'action_dispatch.request.query_parameters' => query_params,
+ 'action_dispatch.request.request_parameters' => request_params,
'action_dispatch.request.path_parameters' => path_params,
+ 'action_dispatch.request.parameters' => params.merge!(request_params).merge!(path_params),
}
request_env['rack.session'] = session if session
self.request = ActionDispatch::Request.new(request_env)
response = controller.make_response! request