Created
November 2, 2011 17:29
-
-
Save MadBomber/1334298 to your computer and use it in GitHub Desktop.
A Sinatra process for setting the value of global debug variables during the execution of a process.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################################## | |
## debug_web_service.rb | |
## A sinatra web application provides an embedded web service for setting | |
## global variables that include 'debug' in their name | |
## | |
## Usage: | |
## Inside of an event-machine run loop (if Thin is used) or in a new Thread do this ... | |
## | |
## DebugWebService::Gui.run!( :ip => '0.0.0.0', :port => 4567 ) | |
## | |
## Use whatever IP and PORT seem right for you. No need to pass parameters if | |
## you want to use the default IP and PORT for example: | |
## | |
## Thread.new { DebugWebService::Gui.run! } | |
# | |
require 'sinatra' | |
require 'haml' | |
module DebugWebService | |
class Gui < Sinatra::Base | |
############################################################################### | |
## Sinatra Configuration | |
# static files normally come from ./public | |
set :public_folder, File.dirname(__FILE__) | |
# dynamic views are normally rendered out of ./views | |
set :views, File.dirname(__FILE__) | |
################################################## | |
# returns an array of global variables whose names | |
# contain the case insensitive string 'debug' | |
def global_debug_variables | |
global_variables.select { |var_name| var_name.to_s.downcase.include?('debug') } | |
end | |
############################# | |
## Main Application Routes ## | |
############################# | |
########################################## | |
## Root index | |
## displays the values of global debug variables | |
get '/' do | |
@my_debugs = global_debug_variables.sort | |
haml :index | |
end | |
##################################################### | |
## Change the values of selected/deselected variables | |
post '/debug/change/?' do | |
# params only contains the items that were selected | |
# which means we turn everything off first ... | |
global_debug_variables.each do |k| | |
eval("#{k}=false") | |
end | |
# ... then turn on the selected items. | |
params.keys.each do |k| | |
eval("#{k}=true") | |
end | |
redirect back | |
end | |
################################################## | |
## Change the values of all variables | |
post '/debug/change/all/:new_value' do |new_value| | |
global_debug_variables.each do |k| | |
eval("#{k}=#{new_value}") | |
end | |
redirect back | |
end | |
end ## end of class Gui < Sinatra::Base | |
end ## end of module DebugWebService | |
__END__ | |
# Put the rest of this into a seperate file: index.haml within the same directory | |
# as the main file above. | |
%html{:xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", :lang => "en"} | |
%body | |
%h1 Debug Web Services | |
%hr{:size => 5} | |
%p | |
A checked box | |
indicates that the value of the global variable is true. | |
An unchecked box means the value of the global variable is false. | |
%form{:method => :post, :action => "/debug/change/" } | |
- @my_debugs.each do |debug_name| | |
- if eval("#{debug_name.to_s}") | |
%input{:type => :checkbox, :name => debug_name, :checked => true } | |
- else | |
%input{:type => :checkbox, :name => debug_name } | |
= debug_name | |
%br | |
%p | |
%input{:type => :submit, :value => "Submit Changes"} | |
%table | |
%tr | |
%td | |
%form{:method => :post, :action => "/debug/change/all/true" } | |
%input{:type => :submit, :value => "All On (true)"} | |
%td | |
%form{:method => :post, :action => "/debug/change/all/false" } | |
%input{:type => :submit, :value => "All Off (false)"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment